1111import java .util .*;
1212import java .util .function .Function ;
1313import java .util .function .IntFunction ;
14+ import java .util .stream .Collectors ;
1415import java .util .stream .Stream ;
1516
1617/**
@@ -60,15 +61,19 @@ public static JsonNode parse(String json) {
6061 * @return The normalized JsonNode
6162 */
6263 public static JsonNode normalize (Object value ) {
63- return switch (value ) {
64- case null -> NullNode .getInstance ();
65- case JsonNode jsonNode -> jsonNode ;
66- case Optional <?> optional -> normalize (optional .orElse (null ));
67- case Stream <?> stream -> normalize (stream .toList ());
68- default -> value .getClass ().isArray ()
69- ? normalizeArray (value )
70- : normalizeWithStrategy (value );
71- };
64+ if (value == null ) {
65+ return NullNode .getInstance ();
66+ } else if (value instanceof JsonNode ) {
67+ return (JsonNode ) value ;
68+ } else if (value instanceof Optional <?>) {
69+ return normalize (((Optional <?>) value ).orElse (null ));
70+ } else if (value instanceof Stream <?>) {
71+ return normalize (((Stream <?>) value ).collect (Collectors .toList ()));
72+ } else if (value .getClass ().isArray ()) {
73+ return normalizeArray (value );
74+ } else {
75+ return normalizeWithStrategy (value );
76+ }
7277 }
7378
7479 /**
@@ -87,17 +92,25 @@ private static JsonNode normalizeWithStrategy(Object value) {
8792 * Returns null if the value is not a primitive type.
8893 */
8994 private static JsonNode tryNormalizePrimitive (Object value ) {
90- return switch (value ) {
91- case String string -> TextNode .valueOf (string );
92- case Boolean bool -> BooleanNode .valueOf (bool );
93- case Integer integer -> IntNode .valueOf (integer );
94- case Long longVal -> LongNode .valueOf (longVal );
95- case Double doubleVal -> normalizeDouble (doubleVal );
96- case Float floatVal -> normalizeFloat (floatVal );
97- case Short shortVal -> ShortNode .valueOf (shortVal );
98- case Byte byteVal -> IntNode .valueOf (byteVal );
99- case null , default -> null ;
100- };
95+ if (value instanceof String ) {
96+ return TextNode .valueOf ((String ) value );
97+ } else if (value instanceof Boolean ) {
98+ return BooleanNode .valueOf ((Boolean ) value );
99+ } else if (value instanceof Integer ) {
100+ return IntNode .valueOf ((Integer ) value );
101+ } else if (value instanceof Long ) {
102+ return LongNode .valueOf ((Long ) value );
103+ } else if (value instanceof Double ) {
104+ return normalizeDouble ((Double ) value );
105+ } else if (value instanceof Float ) {
106+ return normalizeFloat ((Float ) value );
107+ } else if (value instanceof Short ) {
108+ return ShortNode .valueOf ((Short ) value );
109+ } else if (value instanceof Byte ) {
110+ return IntNode .valueOf ((Byte ) value );
111+ } else {
112+ return null ;
113+ }
101114 }
102115
103116 /**
@@ -144,11 +157,13 @@ private static Optional<JsonNode> tryConvertToLong(Double value) {
144157 * Returns null if the value is not a big number type.
145158 */
146159 private static JsonNode tryNormalizeBigNumber (Object value ) {
147- return switch (value ) {
148- case BigInteger bigInt -> normalizeBigInteger (bigInt );
149- case BigDecimal bigDec -> DecimalNode .valueOf (bigDec );
150- case null , default -> null ;
151- };
160+ if (value instanceof BigInteger ) {
161+ return normalizeBigInteger ((BigInteger ) value );
162+ } else if (value instanceof BigDecimal ) {
163+ return DecimalNode .valueOf ((BigDecimal ) value );
164+ } else {
165+ return null ;
166+ }
152167 }
153168
154169 /**
@@ -167,16 +182,23 @@ private static JsonNode normalizeBigInteger(BigInteger value) {
167182 * Returns null if the value is not a temporal type.
168183 */
169184 private static JsonNode tryNormalizeTemporal (Object value ) {
170- return switch (value ) {
171- case LocalDateTime v -> formatTemporal (v , DateTimeFormatter .ISO_LOCAL_DATE_TIME );
172- case LocalDate v -> formatTemporal (v , DateTimeFormatter .ISO_LOCAL_DATE );
173- case LocalTime v -> formatTemporal (v , DateTimeFormatter .ISO_LOCAL_TIME );
174- case ZonedDateTime v -> formatTemporal (v , DateTimeFormatter .ISO_ZONED_DATE_TIME );
175- case OffsetDateTime v -> formatTemporal (v , DateTimeFormatter .ISO_OFFSET_DATE_TIME );
176- case Instant instant -> TextNode .valueOf (instant .toString ());
177- case java .util .Date date -> TextNode .valueOf (date .toInstant ().toString ());
178- case null , default -> null ;
179- };
185+ if (value instanceof LocalDateTime ) {
186+ return formatTemporal ((LocalDateTime ) value , DateTimeFormatter .ISO_LOCAL_DATE_TIME );
187+ } else if (value instanceof LocalDate ) {
188+ return formatTemporal ((LocalDate ) value , DateTimeFormatter .ISO_LOCAL_DATE );
189+ } else if (value instanceof LocalTime ) {
190+ return formatTemporal ((LocalTime ) value , DateTimeFormatter .ISO_LOCAL_TIME );
191+ } else if (value instanceof ZonedDateTime ) {
192+ return formatTemporal ((ZonedDateTime ) value , DateTimeFormatter .ISO_ZONED_DATE_TIME );
193+ } else if (value instanceof OffsetDateTime ) {
194+ return formatTemporal ((OffsetDateTime ) value , DateTimeFormatter .ISO_OFFSET_DATE_TIME );
195+ } else if (value instanceof Instant ) {
196+ return TextNode .valueOf (((Instant ) value ).toString ());
197+ } else if (value instanceof java .util .Date ) {
198+ return TextNode .valueOf (((java .util .Date ) value ).toInstant ().toString ());
199+ } else {
200+ return null ;
201+ }
180202 }
181203
182204 /**
@@ -191,11 +213,13 @@ private static <T> JsonNode formatTemporal(T temporal, DateTimeFormatter formatt
191213 * Returns null if the value is not a collection type.
192214 */
193215 private static JsonNode tryNormalizeCollection (Object value ) {
194- return switch (value ) {
195- case Collection <?> collection -> normalizeCollection (collection );
196- case Map <?, ?> map -> normalizeMap (map );
197- case null , default -> null ;
198- };
216+ if (value instanceof Collection <?>) {
217+ return normalizeCollection ((Collection <?>) value );
218+ } else if (value instanceof Map <?, ?>) {
219+ return normalizeMap ((Map <?, ?>) value );
220+ } else {
221+ return null ;
222+ }
199223 }
200224
201225 /**
@@ -232,18 +256,36 @@ private static JsonNode tryNormalizePojo(Object value) {
232256 * Normalizes arrays to ArrayNode.
233257 */
234258 private static JsonNode normalizeArray (Object array ) {
235- return switch (array ) {
236- case int [] arr -> buildArrayNode (arr .length , i -> IntNode .valueOf (arr [i ]));
237- case long [] arr -> buildArrayNode (arr .length , i -> LongNode .valueOf (arr [i ]));
238- case double [] arr -> buildArrayNode (arr .length , i -> normalizeDoubleElement (arr [i ]));
239- case float [] arr -> buildArrayNode (arr .length , i -> normalizeFloatElement (arr [i ]));
240- case boolean [] arr -> buildArrayNode (arr .length , i -> BooleanNode .valueOf (arr [i ]));
241- case byte [] arr -> buildArrayNode (arr .length , i -> IntNode .valueOf (arr [i ]));
242- case short [] arr -> buildArrayNode (arr .length , i -> ShortNode .valueOf (arr [i ]));
243- case char [] arr -> buildArrayNode (arr .length , i -> TextNode .valueOf (String .valueOf (arr [i ])));
244- case Object [] arr -> buildArrayNode (arr .length , i -> normalize (arr [i ]));
245- case null , default -> MAPPER .createArrayNode ();
246- };
259+ if (array instanceof int []) {
260+ int [] arr = (int []) array ;
261+ return buildArrayNode (arr .length , i -> IntNode .valueOf (arr [i ]));
262+ } else if (array instanceof long []) {
263+ long [] arr = (long []) array ;
264+ return buildArrayNode (arr .length , i -> LongNode .valueOf (arr [i ]));
265+ } else if (array instanceof double []) {
266+ double [] arr = (double []) array ;
267+ return buildArrayNode (arr .length , i -> normalizeDoubleElement (arr [i ]));
268+ } else if (array instanceof float []) {
269+ float [] arr = (float []) array ;
270+ return buildArrayNode (arr .length , i -> normalizeFloatElement (arr [i ]));
271+ } else if (array instanceof boolean []) {
272+ boolean [] arr = (boolean []) array ;
273+ return buildArrayNode (arr .length , i -> BooleanNode .valueOf (arr [i ]));
274+ } else if (array instanceof byte []) {
275+ byte [] arr = (byte []) array ;
276+ return buildArrayNode (arr .length , i -> IntNode .valueOf (arr [i ]));
277+ } else if (array instanceof short []) {
278+ short [] arr = (short []) array ;
279+ return buildArrayNode (arr .length , i -> ShortNode .valueOf (arr [i ]));
280+ } else if (array instanceof char []) {
281+ char [] arr = (char []) array ;
282+ return buildArrayNode (arr .length , i -> TextNode .valueOf (String .valueOf (arr [i ])));
283+ } else if (array instanceof Object []) {
284+ Object [] arr = (Object []) array ;
285+ return buildArrayNode (arr .length , i -> normalize (arr [i ]));
286+ } else {
287+ return MAPPER .createArrayNode ();
288+ }
247289 }
248290
249291 /**
0 commit comments