0

In below Java TestCase I have invalid JOSN ('input' variable Value), meaning its not strict JOSN object. I want mapper.readValue to return input String as is. Below test case print "{StrKey1=StrValue1}" on console. mapper.readValue does not throw an exception but pick first JOSN item and convert it into Map. I have to keep Map.class as Value Type because following logic is depend on that if converted value is Map.

@Test
    public void testInvalidJson() {
        String input = "{\"StrKey1\":\"StrValue1\"},{\"StrKey2\":\"StrValue2\"}";
        ObjectMapper mapper = new ObjectMapper();
        try {
            Object map = mapper.readValue(input, Map.class);
            System.out.println(map);
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

I dont want to set DeserializationFeature.FAIL_ON_TRAILING_TOKENS on ObjectMapper as my inputs are expected to be invalid JSON as well as non strict JSON and those should be converted to String. Even org.json.JSONObject("{"StrKey1":"StrValue1"},{"StrKey2":"StrValue2"}") does not throw an exception.

What changes I should make in above code so that mapper.readValue will return complete input string as output for given input?

1
  • Are you open to use other json libraries? Commented Nov 8, 2022 at 14:03

1 Answer 1

3

You may want to check first whether the json is valid using the below code, The nextToken verifies if the json is valid, if not you can return the input string as is, it is already a string.

public void testInvalidJson() {
        String input = "{\"StrKey1\":\"StrValue1\"},{\"StrKey2\":\"StrValue2\"}";
        JsonParser jp = mapper.getFactory().createParser(input);
        
        try {
            Object map = mapper.readValue(jp, Map.class);
            while (jp.nextToken() != null) {
            //return the source string
            //System.out.println(input);
        }   
            System.out.println(map);
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

jp.nextToken() actually throws 'JsonParseException: Unexpected character (',' (code 44))' . I had handled that exception and returned input String but that fix opened other cases where input JSON string is actually invalid JSON. How do I make sure mentioned JSON doesn't throw exception but Identify its not parsable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.