2

I am using writeValueAsString of the ObjectMapper . However, it's giving me a Java String representation so I get:

{"network_id":5000370004610700049753}

instead of

"{\"network_id\":5000370004610700049753}"

which is failing for other services when deserializing. How do I get this kind of serialization with the ObjectMapper?

4
  • did you check this feature ALLOW_UNQUOTED_FIELD_NAMES Commented Oct 24, 2019 at 12:46
  • 4
    Well, the first is a string value with JSON text, as it should be, so that should be what you'd want. Why do you want the second, i.e. that JSON text encoded as a Java or JSON string? Commented Oct 24, 2019 at 12:47
  • The C# service on the other end of the queue fails to deserialize the first one @Andreas Commented Oct 24, 2019 at 12:49
  • So the C# code doesn't want plain JSON, but a JSON text encoded as a JSON string? That makes no sense, but doable, see answer. Commented Oct 24, 2019 at 12:51

1 Answer 1

5

To get the second result, send it through the ObjectMapper again.

Map<String, Object> data = new HashMap<>();
data.put("network_id", new BigInteger("5000370004610700049753"));

ObjectMapper objectMapper = new ObjectMapper();

String plainJson = objectMapper.writeValueAsString(data);
System.out.println(plainJson);

String encodedJson = objectMapper.writeValueAsString(plainJson);
System.out.println(encodedJson);

Output

{"network_id":5000370004610700049753}
"{\"network_id\":5000370004610700049753}"
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I agree this is stupid but not sure what's going on on the other end :(

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.