1717
1818package org .photonvision .common .util .file ;
1919
20- import com .fasterxml .jackson .core .JsonGenerator ;
21- import com .fasterxml .jackson .core .JsonParser ;
2220import com .fasterxml .jackson .core .json .JsonReadFeature ;
23- import com .fasterxml .jackson .databind .DeserializationContext ;
2421import com .fasterxml .jackson .databind .DeserializationFeature ;
25- import com .fasterxml .jackson .databind .JsonDeserializer ;
26- import com .fasterxml .jackson .databind .JsonSerializer ;
2722import com .fasterxml .jackson .databind .ObjectMapper ;
28- import com .fasterxml .jackson .databind .SerializerProvider ;
2923import com .fasterxml .jackson .databind .deser .std .StdDeserializer ;
3024import com .fasterxml .jackson .databind .json .JsonMapper ;
3125import com .fasterxml .jackson .databind .jsontype .BasicPolymorphicTypeValidator ;
3731import java .io .FileOutputStream ;
3832import java .io .IOException ;
3933import java .nio .file .Path ;
40- import java .nio .file .Paths ;
4134import java .util .HashMap ;
4235import java .util .Map ;
4336import org .eclipse .jetty .io .EofException ;
4437
4538public class JacksonUtils {
4639 public static class UIMap extends HashMap <String , Object > {}
4740
48- // Custom Path serializer that outputs just the path string without file:/ prefix
49- public static class PathSerializer extends JsonSerializer <Path > {
50- @ Override
51- public void serialize (Path value , JsonGenerator gen , SerializerProvider serializers )
52- throws IOException {
53- if (value == null ) {
54- gen .writeNull ();
55- } else {
56- gen .writeString (value .toString ());
57- }
58- }
59- }
60-
61- // Custom Path deserializer that reads path strings
62- public static class PathDeserializer extends JsonDeserializer <Path > {
63- @ Override
64- public Path deserialize (JsonParser p , DeserializationContext ctxt ) throws IOException {
65- String pathString = p .getValueAsString ();
66- if (pathString == null || pathString .isEmpty ()) {
67- return null ;
68- }
69-
70- // Handle case where old serialized data might still have file:/ prefix
71- if (pathString .startsWith ("file:/" )) {
72- pathString = pathString .substring (6 ); // Remove "file:/" prefix
73- }
74-
75- return Paths .get (pathString );
76- }
77- }
78-
79- // Custom Path key deserializer for Maps with Path keys
80- public static class PathKeyDeserializer extends com .fasterxml .jackson .databind .KeyDeserializer {
81- @ Override
82- public Object deserializeKey (String key , DeserializationContext ctxt ) throws IOException {
83- if (key == null || key .isEmpty ()) {
84- return null ;
85- }
86-
87- // Handle case where old serialized data might still have file:/ prefix
88- if (key .startsWith ("file:/" )) {
89- key = key .substring (6 ); // Remove "file:/" prefix
90- }
91-
92- return Paths .get (key );
93- }
94- }
95-
96- // Helper method to create ObjectMapper with Path serialization support
97- private static ObjectMapper createObjectMapperWithPathSupport (Class <?> baseType ) {
98- PolymorphicTypeValidator ptv =
99- BasicPolymorphicTypeValidator .builder ().allowIfBaseType (baseType ).build ();
100-
101- SimpleModule pathModule = new SimpleModule ();
102- pathModule .addSerializer (Path .class , new PathSerializer ());
103- pathModule .addDeserializer (Path .class , new PathDeserializer ());
104- pathModule .addKeyDeserializer (Path .class , new PathKeyDeserializer ());
105-
106- return JsonMapper .builder ()
107- .configure (JsonReadFeature .ALLOW_JAVA_COMMENTS , true )
108- .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
109- .activateDefaultTyping (ptv , ObjectMapper .DefaultTyping .JAVA_LANG_OBJECT )
110- .addModule (pathModule )
111- .build ();
112- }
113-
11441 public static <T > void serialize (Path path , T object ) throws IOException {
11542 serialize (path , object , true );
11643 }
11744
11845 public static <T > String serializeToString (T object ) throws IOException {
119- ObjectMapper objectMapper = createObjectMapperWithPathSupport (object .getClass ());
46+ PolymorphicTypeValidator ptv =
47+ BasicPolymorphicTypeValidator .builder ().allowIfBaseType (object .getClass ()).build ();
48+ ObjectMapper objectMapper =
49+ JsonMapper .builder ()
50+ .activateDefaultTyping (ptv , ObjectMapper .DefaultTyping .JAVA_LANG_OBJECT )
51+ .build ();
12052 return objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (object );
12153 }
12254
12355 public static <T > void serialize (Path path , T object , boolean forceSync ) throws IOException {
124- ObjectMapper objectMapper = createObjectMapperWithPathSupport (object .getClass ());
56+ PolymorphicTypeValidator ptv =
57+ BasicPolymorphicTypeValidator .builder ().allowIfBaseType (object .getClass ()).build ();
58+ ObjectMapper objectMapper =
59+ JsonMapper .builder ()
60+ .activateDefaultTyping (ptv , ObjectMapper .DefaultTyping .JAVA_LANG_OBJECT )
61+ .build ();
12562 String json = objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (object );
12663 saveJsonString (json , path , forceSync );
12764 }
12865
12966 public static <T > T deserialize (Map <?, ?> s , Class <T > ref ) throws IOException {
130- ObjectMapper objectMapper = createObjectMapperWithPathSupport (ref );
67+ PolymorphicTypeValidator ptv =
68+ BasicPolymorphicTypeValidator .builder ().allowIfBaseType (ref ).build ();
69+ ObjectMapper objectMapper =
70+ JsonMapper .builder ()
71+ .configure (JsonReadFeature .ALLOW_JAVA_COMMENTS , true )
72+ .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
73+ .activateDefaultTyping (ptv , ObjectMapper .DefaultTyping .JAVA_LANG_OBJECT )
74+ .build ();
75+
13176 return objectMapper .convertValue (s , ref );
13277 }
13378
@@ -136,14 +81,28 @@ public static <T> T deserialize(String s, Class<T> ref) throws IOException {
13681 throw new EofException ("Provided empty string for class " + ref .getName ());
13782 }
13883
139- ObjectMapper objectMapper = createObjectMapperWithPathSupport (ref );
140- objectMapper .enable (DeserializationFeature .READ_UNKNOWN_ENUM_VALUES_AS_NULL );
84+ PolymorphicTypeValidator ptv =
85+ BasicPolymorphicTypeValidator .builder ().allowIfBaseType (ref ).build ();
86+ ObjectMapper objectMapper =
87+ JsonMapper .builder ()
88+ .configure (JsonReadFeature .ALLOW_JAVA_COMMENTS , true )
89+ .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
90+ .enable (DeserializationFeature .READ_UNKNOWN_ENUM_VALUES_AS_NULL )
91+ .activateDefaultTyping (ptv , ObjectMapper .DefaultTyping .JAVA_LANG_OBJECT )
92+ .build ();
14193
14294 return objectMapper .readValue (s , ref );
14395 }
14496
14597 public static <T > T deserialize (Path path , Class <T > ref ) throws IOException {
146- ObjectMapper objectMapper = createObjectMapperWithPathSupport (ref );
98+ PolymorphicTypeValidator ptv =
99+ BasicPolymorphicTypeValidator .builder ().allowIfBaseType (ref ).build ();
100+ ObjectMapper objectMapper =
101+ JsonMapper .builder ()
102+ .configure (JsonReadFeature .ALLOW_JAVA_COMMENTS , true )
103+ .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
104+ .activateDefaultTyping (ptv , ObjectMapper .DefaultTyping .JAVA_LANG_OBJECT )
105+ .build ();
147106 File jsonFile = new File (path .toString ());
148107 if (jsonFile .exists () && jsonFile .length () > 0 ) {
149108 return objectMapper .readValue (jsonFile , ref );
@@ -156,12 +115,6 @@ public static <T> T deserialize(Path path, Class<T> ref, StdDeserializer<T> dese
156115 ObjectMapper objectMapper = new ObjectMapper ();
157116 SimpleModule module = new SimpleModule ();
158117 module .addDeserializer (ref , deserializer );
159-
160- // Add Path support to custom deserializer case as well
161- module .addSerializer (Path .class , new PathSerializer ());
162- module .addDeserializer (Path .class , new PathDeserializer ());
163- module .addKeyDeserializer (Path .class , new PathKeyDeserializer ());
164-
165118 objectMapper .registerModule (module );
166119
167120 File jsonFile = new File (path .toString ());
@@ -182,12 +135,6 @@ public static <T> void serialize(
182135 ObjectMapper objectMapper = new ObjectMapper ();
183136 SimpleModule module = new SimpleModule ();
184137 module .addSerializer (ref , serializer );
185-
186- // Add Path support to custom serializer case as well
187- module .addSerializer (Path .class , new PathSerializer ());
188- module .addDeserializer (Path .class , new PathDeserializer ());
189- module .addKeyDeserializer (Path .class , new PathKeyDeserializer ());
190-
191138 objectMapper .registerModule (module );
192139 String json = objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (object );
193140 saveJsonString (json , path , forceSync );
0 commit comments