You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
publicfinalclassRawJSONDeserializerextendsJsonDeserializer<RawJSONIndexes> {
@Override@SneakyThrowspublicRawJSONIndexesdeserialize(JsonParserp, DeserializationContextctxt) {
finalintstart = (int) p.getTokenLocation().getCharOffset();
// NOTE: we only care where this objects starts and ends// NOTE: this line handles nested objects & arrays too p.skipChildren();
finalintend = (int) p.getCurrentLocation().getCharOffset();
returnRawJSONIndexes.builder()
.start(start)
.end(end)
.build();
}
}
ObjectMappermapperForRawJSON = ... // get from Dependency Injection or clone another ObjectMapperfinalvarmodule = newSimpleModule()
.addDeserializer(
RawJSONIndexes.class,
newRawJSONDeserializer());
mapperForRawJSON.registerModule(module);
Step-4: Make some dummy JSON to test
// Add extra spaces, line breaks, inconsistent spacing, ... to prove the output wasn't auto-cleaned// NOTE: Nested objects and arrays also workfinalvarexampleJSON = """ { "fooBars": [ { "b": true, "n": 6, "s": "quuz", "myObj":{"a": 3} }, { "b": false, "n": 103, "s": "yarp" , "myArr": ["t"] } ] } """.trim();
Step-5: Parse & Use the partially deserialized JSON