Skip to content

Commit dfb7e1e

Browse files
authored
Update validation of row metadata to allow non-numeric numbers
1 parent 1d15612 commit dfb7e1e

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

driver/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<java.apidoc>http://docs.oracle.com/javase/8/docs/api</java.apidoc>
4848
<maven.deploy.skip>false</maven.deploy.skip>
4949
<netty.version>4.1.118.Final</netty.version>
50-
<jackson.version>2.15.2</jackson.version>
50+
<jackson.version>2.18.2</jackson.version>
5151
<!-- by default, skip tests; tests require a profile -->
5252
<maven.test.skip>true</maven.test.skip>
5353
<javac>javac</javac>

driver/src/main/java/oracle/nosql/driver/values/JsonUtils.java

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -763,26 +763,59 @@ public static String convertBytesToHex(byte[] byteArray) {
763763
* Validates input is a valid JSON construct: object, array, string, number,
764764
* true, false or null. Throws IllegalArgumentException if not valid.
765765
* Multiple JSON Objects are not allowed. Strings must use only double
766-
* quotes (").
766+
* quotes ("). Allows non-numeric values: NaN, Infinity, -Infinity (and -INF).
767767
*/
768768
public static void validateJsonConstruct(String jsonInput) {
769-
try (JsonParser jp = createParserWithOptions(jsonInput, null)) {
769+
JsonOptions options = new JsonOptions()
770+
.setAllowNonNumericNumbers(true);
771+
validateJsonConstruct(jsonInput, options);
772+
}
773+
774+
/**
775+
* Validates input is a valid JSON construct: object, array, string, number,
776+
* true, false or null. Throws IllegalArgumentException if not valid.
777+
* Multiple JSON Objects are not allowed. Strings must use only double
778+
* quotes (").
779+
*/ public static void validateJsonConstruct(String jsonInput,
780+
JsonOptions options) {
781+
782+
try (JsonParser jp = createParserWithOptions(jsonInput, options)) {
783+
int s = 0;
784+
int i = 0;
770785
JsonToken token = jp.nextToken();
771786
if (token == null) {
772-
throw new IllegalArgumentException("Value is not a valid JSON construct.");
787+
throw new IllegalArgumentException(
788+
"Value is not a valid JSON construct.");
789+
} else if (JsonToken.START_OBJECT.equals(token) ||
790+
JsonToken.START_ARRAY.equals(token)) {
791+
s += 1;
792+
} else {
793+
i += ( s == 0 ? 1 : 0);
773794
}
774-
int s = 1;
795+
775796
while (!jp.isClosed()) {
776797
token = jp.nextToken();
777-
if (token != null && JsonToken.START_OBJECT.equals(token)) {
778-
if (s == 0) {
779-
throw new IllegalArgumentException("Multiple JSON " +
780-
"Objects not allowed.");
798+
if (token != null) {
799+
if(JsonToken.FIELD_NAME.equals(token)) {
800+
// skip
801+
} else if (JsonToken.START_OBJECT.equals(token) ||
802+
JsonToken.START_ARRAY.equals(token) ) {
803+
if (s == 0) {
804+
throw new IllegalArgumentException(
805+
"Multiple JSON " +
806+
"Objects not allowed.");
807+
}
808+
s++;
809+
} else if (JsonToken.END_OBJECT.equals(token) ||
810+
JsonToken.END_ARRAY.equals(token)) {
811+
s--;
812+
i += ( s == 0 ? 1 : 0);
813+
} else {
814+
i += ( s == 0 ? 1 : 0);
815+
}
816+
if (i > 1) {
817+
throw new IllegalArgumentException("Multiple top level JSON constructs not allowed");
781818
}
782-
s++;
783-
}
784-
if (token != null && JsonToken.END_OBJECT.equals(token)) {
785-
s--;
786819
}
787820
}
788821
} catch (IOException ioe) {

0 commit comments

Comments
 (0)