0

I have a java type:

 class Type {
    String jsonField;
    Object anotherJsonField;
 }

I can successfully decerialize this String value using jackson library via executing

com.fasterxml.jackson.databind.ObjectMapper.readValue(content, Type.class)()

{
  "jsonField": "field",
  "anotherField": {
    "something": "value"
  }
}

I know that anotherField Object (its actual type after decerialization is LinkedHashMap) in the Type object is pointing to another JSON structure in it (AnotherType type). Is there any way to transform this Object value into an object of type AnotherType (java.lang.Object[java.util.LinkedHashMap] -> AnotherType)?

Alex Barysevich
  • 544
  • 1
  • 7
  • 18
  • Perhaps I'm missing the point here, but have you tried casting the value? – Silvio Mayolo Dec 09 '21 at 01:40
  • Yes, I did, and I got this: java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class util.AnotherType – Alex Barysevich Dec 09 '21 at 01:46
  • 2
    Then it is not `AnotherType`. It's a `LinkedHashMap`. No matter how sure you are, the JVM knows what type the thing is better than you do. – Silvio Mayolo Dec 09 '21 at 01:48
  • Makes a lot of sense. Then the question transforms into: https://stackoverflow.com/questions/22870584/convert-from-linkedhashmap-to-json-string Thanks, @SilvioMayolo – Alex Barysevich Dec 09 '21 at 01:52

0 Answers0