0

I have a spring boot app configured with a RabbitMqListener. It has to receive JSON data of the format below: (showing sample)

{ "name" :"abc",  
"key" : "somekey",
"value" : {"data": {"notes": "**foo \u0026 bar"}}** 
}

This data represents some info. which should be used only for read-only processing, and the receiver spring app should receive it as it is(raw form).

What I mean is if I assert value node in spring app with input that was published on queue it should be equal.

This is simply not happening. I always get the value in spring app as foo & bar but I wanted it in raw form without a conversation of \u codes.

I try several approaches,

  1. Jackaon2JsonMessage converter,
  2. passing bytes from Message.getBody() - byte[] to mapper.readValue() in Rabbit handler.
  3. Using JSON-simple, gson libraries

Why is it so tricky to get the data as it is, without any conversion or translation. Do I need to follow an alternative approach?

Any help is appreciated

vvk24
  • 470
  • 5
  • 18
akjain
  • 1,787
  • 3
  • 20
  • 35

1 Answers1

1

Have you tried explicitly enabling the escaping of non-ascii characters on your ObjectMapper?

mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
Martin Devillers
  • 17,293
  • 5
  • 46
  • 88
  • 1
    thanks for your reply, but Its not working.. btw JsonGenerator is deprecated. I created mapper as: mapper=JsonMapper.builder().enable(JsonWriteFeature.ESCAPE_NON_ASCII).build() – akjain Jul 10 '20 at 07:28
  • Hmm now that I read your question again I think your problem lies at the deserializer, and not the serializer. Does this answer help you? https://stackoverflow.com/a/46579378/546967 – Martin Devillers Jul 10 '20 at 07:35
  • 1
    tried that before posting here.. it works for the example given there but for the sample I posted, where the \u00xx chars are nested somewhere, it doesn't work.. may be I need to tweak something.. Its strange why the built-in JsonWriteFeature is not working.. – akjain Jul 10 '20 at 07:39
  • The `JsonWriteFeature` (or deprecated `JsonGenerator`) are settings for the JSON *serializer*, which is responsible for transforming a Java Object Model into JSON. Your problem lies with the *deserializer*, which is responsible for transforming JSON into a Java Object Model. Unfortunately there's no `ESCAPE_NON_ASCII` equivalent for the `JsonReader` part of the equation. I think writing a custom `JsonDeserializer` is probably the best way forward. – Martin Devillers Jul 10 '20 at 07:43