@@ -41,6 +41,8 @@ public class RowJsonUtils {
4141 //
4242 private static int defaultBufferLimit ;
4343
44+ private static final boolean STREAM_READ_CONSTRAINTS_AVAILABLE = streamReadConstraintsAvailable ();
45+
4446 /**
4547 * Increase the default jackson-databind stream read constraint.
4648 *
@@ -70,6 +72,25 @@ public static void increaseDefaultStreamReadConstraints(int newLimit) {
7072 increaseDefaultStreamReadConstraints (MAX_STRING_LENGTH );
7173 }
7274
75+ private static boolean streamReadConstraintsAvailable () {
76+ try {
77+ Class .forName ("com.fasterxml.jackson.core.StreamReadConstraints" );
78+ return true ;
79+ } catch (ClassNotFoundException e ) {
80+ return false ;
81+ }
82+ }
83+
84+ private static class StreamReadConstraintsHelper {
85+ static void setStreamReadConstraints (JsonFactory jsonFactory , int sizeLimit ) {
86+ com .fasterxml .jackson .core .StreamReadConstraints streamReadConstraints =
87+ com .fasterxml .jackson .core .StreamReadConstraints .builder ()
88+ .maxStringLength (sizeLimit )
89+ .build ();
90+ jsonFactory .setStreamReadConstraints (streamReadConstraints );
91+ }
92+ }
93+
7394 /**
7495 * Creates a thread-safe JsonFactory with custom stream read constraints.
7596 *
@@ -83,16 +104,8 @@ public static void increaseDefaultStreamReadConstraints(int newLimit) {
83104 public static JsonFactory createJsonFactory (int sizeLimit ) {
84105 sizeLimit = Math .max (sizeLimit , MAX_STRING_LENGTH );
85106 JsonFactory jsonFactory = new JsonFactory ();
86- try {
87- // Check if StreamReadConstraints is available (Jackson 2.15+)
88- Class .forName ("com.fasterxml.jackson.core.StreamReadConstraints" );
89- com .fasterxml .jackson .core .StreamReadConstraints streamReadConstraints =
90- com .fasterxml .jackson .core .StreamReadConstraints .builder ()
91- .maxStringLength (sizeLimit )
92- .build ();
93- jsonFactory .setStreamReadConstraints (streamReadConstraints );
94- } catch (ClassNotFoundException e ) {
95- // If the class is not found (i.e., Jackson version < 2.15), do nothing.
107+ if (STREAM_READ_CONSTRAINTS_AVAILABLE ) {
108+ StreamReadConstraintsHelper .setStreamReadConstraints (jsonFactory , sizeLimit );
96109 }
97110 return jsonFactory ;
98111 }
0 commit comments