1717
1818package org .photonvision .common .util .file ;
1919
20+ import com .fasterxml .jackson .core .JsonGenerator ;
2021import com .fasterxml .jackson .core .json .JsonReadFeature ;
22+ import com .fasterxml .jackson .databind .DeserializationContext ;
2123import com .fasterxml .jackson .databind .DeserializationFeature ;
2224import com .fasterxml .jackson .databind .ObjectMapper ;
23- import com .fasterxml .jackson .databind .deser .std .StdDeserializer ;
25+ import com .fasterxml .jackson .databind .SerializerProvider ;
26+ import com .fasterxml .jackson .databind .ext .NioPathDeserializer ;
27+ import com .fasterxml .jackson .databind .ext .NioPathSerializer ;
2428import com .fasterxml .jackson .databind .json .JsonMapper ;
2529import com .fasterxml .jackson .databind .jsontype .BasicPolymorphicTypeValidator ;
2630import com .fasterxml .jackson .databind .jsontype .PolymorphicTypeValidator ;
2731import com .fasterxml .jackson .databind .module .SimpleModule ;
28- import com .fasterxml .jackson .databind .ser .std .StdSerializer ;
2932import java .io .File ;
3033import java .io .FileDescriptor ;
3134import java .io .FileOutputStream ;
3235import java .io .IOException ;
36+ import java .net .URI ;
3337import java .nio .file .Path ;
38+ import java .nio .file .Paths ;
3439import java .util .HashMap ;
3540import java .util .Map ;
3641import org .eclipse .jetty .io .EofException ;
3742
3843public class JacksonUtils {
3944 public static class UIMap extends HashMap <String , Object > {}
4045
46+ // Custom Path key deserializer for Maps with Path keys
47+ public static class PathKeySerializer
48+ extends com .fasterxml .jackson .databind .JsonSerializer <Path > {
49+ @ Override
50+ public void serialize (Path value , JsonGenerator gen , SerializerProvider serializers )
51+ throws IOException {
52+ if (value == null ) {
53+ gen .writeNull ();
54+ } else {
55+ gen .writeFieldName (value .toUri ().toString ());
56+ }
57+ }
58+ }
59+
60+ // Custom Path key deserializer for Maps with Path keys
61+ public static class PathKeyDeserializer extends com .fasterxml .jackson .databind .KeyDeserializer {
62+ @ Override
63+ public Object deserializeKey (String key , DeserializationContext ctxt ) throws IOException {
64+ if (key == null || key .isEmpty ()) {
65+ return null ;
66+ }
67+ return Paths .get (URI .create (key ));
68+ }
69+ }
70+
71+ // Helper method to create ObjectMapper with Path serialization support
72+ private static ObjectMapper createObjectMapperWithPathSupport (Class <?> baseType ) {
73+ PolymorphicTypeValidator ptv =
74+ BasicPolymorphicTypeValidator .builder ().allowIfBaseType (baseType ).build ();
75+
76+ SimpleModule pathModule = new SimpleModule ();
77+ pathModule .addSerializer (Path .class , new NioPathSerializer ());
78+ pathModule .addKeySerializer (Path .class , new PathKeySerializer ());
79+ pathModule .addDeserializer (Path .class , new NioPathDeserializer ());
80+ pathModule .addKeyDeserializer (Path .class , new PathKeyDeserializer ());
81+
82+ return JsonMapper .builder ()
83+ .configure (JsonReadFeature .ALLOW_JAVA_COMMENTS , true )
84+ .configure (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
85+ .activateDefaultTyping (ptv , ObjectMapper .DefaultTyping .JAVA_LANG_OBJECT )
86+ .addModule (pathModule )
87+ .build ();
88+ }
89+
4190 public static <T > void serialize (Path path , T object ) throws IOException {
4291 serialize (path , object , true );
4392 }
4493
4594 public static <T > String serializeToString (T object ) throws IOException {
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 ();
95+ ObjectMapper objectMapper = createObjectMapperWithPathSupport (object .getClass ());
5296 return objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (object );
5397 }
5498
5599 public static <T > void serialize (Path path , T object , boolean forceSync ) throws IOException {
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 ();
100+ ObjectMapper objectMapper = createObjectMapperWithPathSupport (object .getClass ());
62101 String json = objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (object );
63102 saveJsonString (json , path , forceSync );
64103 }
65104
66105 public static <T > T deserialize (Map <?, ?> s , Class <T > ref ) throws IOException {
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-
106+ ObjectMapper objectMapper = createObjectMapperWithPathSupport (ref );
76107 return objectMapper .convertValue (s , ref );
77108 }
78109
@@ -81,65 +112,21 @@ public static <T> T deserialize(String s, Class<T> ref) throws IOException {
81112 throw new EofException ("Provided empty string for class " + ref .getName ());
82113 }
83114
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 ();
115+ ObjectMapper objectMapper = createObjectMapperWithPathSupport (ref );
116+ objectMapper .enable (DeserializationFeature .READ_UNKNOWN_ENUM_VALUES_AS_NULL );
93117
94118 return objectMapper .readValue (s , ref );
95119 }
96120
97121 public static <T > T deserialize (Path path , Class <T > ref ) throws IOException {
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 ();
122+ ObjectMapper objectMapper = createObjectMapperWithPathSupport (ref );
106123 File jsonFile = new File (path .toString ());
107124 if (jsonFile .exists () && jsonFile .length () > 0 ) {
108125 return objectMapper .readValue (jsonFile , ref );
109126 }
110127 return null ;
111128 }
112129
113- public static <T > T deserialize (Path path , Class <T > ref , StdDeserializer <T > deserializer )
114- throws IOException {
115- ObjectMapper objectMapper = new ObjectMapper ();
116- SimpleModule module = new SimpleModule ();
117- module .addDeserializer (ref , deserializer );
118- objectMapper .registerModule (module );
119-
120- File jsonFile = new File (path .toString ());
121- if (jsonFile .exists () && jsonFile .length () > 0 ) {
122- return objectMapper .readValue (jsonFile , ref );
123- }
124- return null ;
125- }
126-
127- public static <T > void serialize (Path path , T object , Class <T > ref , StdSerializer <T > serializer )
128- throws IOException {
129- serialize (path , object , ref , serializer , true );
130- }
131-
132- public static <T > void serialize (
133- Path path , T object , Class <T > ref , StdSerializer <T > serializer , boolean forceSync )
134- throws IOException {
135- ObjectMapper objectMapper = new ObjectMapper ();
136- SimpleModule module = new SimpleModule ();
137- module .addSerializer (ref , serializer );
138- objectMapper .registerModule (module );
139- String json = objectMapper .writerWithDefaultPrettyPrinter ().writeValueAsString (object );
140- saveJsonString (json , path , forceSync );
141- }
142-
143130 private static void saveJsonString (String json , Path path , boolean forceSync ) throws IOException {
144131 var file = path .toFile ();
145132 if (file .getParentFile () != null && !file .getParentFile ().exists ()) {
0 commit comments