@@ -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