0

I am marshalling an object into JSON that has JSON in a String property (String actionsJSON). This was causing that JSON to be escaped, so I am using Jackson annotation @JsonRawValue to get the JSON in that property not be escaped. The problem is that when I unmarshal it back into a String property (the reverse process) Jackson processes the JSON (which has a JSON array) and throws an error as the java class property is just a String:

Can not deserialize instance of java.lang.String out of START_ARRAY token

How can I make Jackson just copy the content of that property without trying to process it so I have JSON code inside the String property as I had in the original object?

(I have tried @JsonRawValue in the target class, ... @JsonSerialize(using = ToStringSerializer.class, as = StringSerializer.class) but error is still there.

icordoba
  • 1,834
  • 2
  • 33
  • 60
  • Do you generate the content of `actionsJSON`? Any chance you could keep it as an `Object` and let Jackson serialize it? – GuiSim Dec 30 '16 at 17:03
  • I generate it manually into the String as is dynamic content not related to Java properties, that is why I need @JsonRawValue so Jackson does not re-convert it into JSON twice (escaping it) when marshalling. The problem is not really serialising but when I deserialise it back into the String property. – icordoba Dec 30 '16 at 17:05
  • @icordoba the problem is that @JsonRawValue is only for serialization, not deserialization. Using an `Object` instead of a String and letting Jackson serialize (and deserialize) would fix the issue. – GuiSim Dec 30 '16 at 17:06
  • Check out http://stackoverflow.com/questions/4783421/how-can-i-include-raw-json-in-an-object-using-jackson and http://stackoverflow.com/questions/8137060/jackson-deserialize-variable-as-json-string – GuiSim Dec 30 '16 at 17:06
  • My problem is probably the same one as this one: http://stackoverflow.com/questions/27911591/can-not-deserialize-instance-of-java-lang-string-out-of-start-array-token – icordoba Dec 30 '16 at 17:06
  • I have also tried changing from String to Object in the target class as @GuiSim suggested. Even though I wanted to avoid removing the String type, at least I don't get an error now as I get a generic ArrayList into it without any error. Anyway, my goal is just getting the JSON as text into the String. – icordoba Dec 30 '16 at 17:11
  • possible duplicate of http://stackoverflow.com/questions/4783421/how-can-i-include-raw-json-in-an-object-using-jackson – Sharon Ben Asher Jan 04 '17 at 07:21

1 Answers1

0

You should look at JsonDeserlalize and not JsonSerialize. A nice example at Deserialize JSON to string in raw format using Jackson And more detailed information at http://www.baeldung.com/jackson-annotations

Nech
  • 331
  • 3
  • 11