How to get the underlying String from a JsonParser (Jackson Json) How to get the underlying String from a JsonParser (Jackson Json) android android

How to get the underlying String from a JsonParser (Jackson Json)


If you have the JsonParser then you can use jsonParser.readValueAsTree().toString().

However, this likely requires that the JSON being parsed is indeed valid JSON.


I had a situation where I was using a custom deserializer, but I wanted the default deserializer to do most of the work, and then using the SAME json do some additional custom work. However, after the default deserializer does its work, the JsonParser object current location was beyond the json text I needed. So I had the same problem as you: how to get access to the underlying json string.

You can use JsonParser.getCurrentLocation.getSourceRef() to get access to the underlying json source. Use JsonParser.getCurrentLocation().getCharOffset() to find the current location in the json source.

Here's the solution I used:

public class WalkStepDeserializer extends StdDeserializer<WalkStep> implements    ResolvableDeserializer {    // constructor, logger, and ResolvableDeserializer methods not shown    @Override    public MyObj deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,            JsonProcessingException {        MyObj myObj = null;        JsonLocation startLocation = jp.getCurrentLocation();        long charOffsetStart = startLocation.getCharOffset();        try {            myObj = (MyObj) defaultDeserializer.deserialize(jp, ctxt);        } catch (UnrecognizedPropertyException e) {            logger.info(e.getMessage());        }        JsonLocation endLocation = jp.getCurrentLocation();        long charOffsetEnd = endLocation.getCharOffset();        String jsonSubString = endLocation.getSourceRef().toString().substring((int)charOffsetStart - 1, (int)charOffsetEnd);        logger.info(strWalkStep);        // Special logic - use JsonLocation.getSourceRef() to get and use the entire Json        // string for further processing        return myObj;    }}

And info about using a default deserializer in a custom deserializer is at How do I call the default deserializer from a custom deserializer in Jackson


5 year late, but this was my solution:

I converted the jsonParser to string

String requestString = jsonParser.readValueAsTree().toString();

Then I converted that string into a JsonParser

JsonFactory factory = new JsonFactory();JsonParser parser  = factory.createParser(requestString);

Then I iterated through my parser

ObjectMapper objectMapper = new ObjectMapper();while(!parser.isClosed()){    JsonToken jsonToken = parser.nextToken();    if(JsonToken.FIELD_NAME.equals(jsonToken)){        String currentName = parser.getCurrentName();        parser.nextToken();        switch (currentName) {            case "someObject":                Object someObject = objectMapper.readValue(parser, Object.class)                //validate someObject                break;        } }

I needed to save the original json string for logging purposes, which is why I did this in the first place. Was a headache to find out, but finally did it and I hope i'm helping someone out :)