I am currently trying to use Jackson to turn an object into a JSON string this was easily done by
public byte[] toJSON(Object obj) throws IOException {
ObjectMapper map = new ObjectMapper();
return map.writeValueAsString(obj).getBytes();
}
Where i run into trouble is when i was to take the array of bytes and turn them into an Object. Currently i have:
public Object toObject(byte[] bytes) throws IOException, ClassNotFoundException {
ObjectMapper map = new ObjectMapper();
return (Object)map.readValue(bytes, Object.class);
}
I successfully convert an object to a JSON string but the Object returned from the toObject method is always a LinkedHashMap instead of the object that was initially turned into the JSON string.
Sorry if i did a poor job communicating my problem but ill try to sum it up simply. I want my code to be able to do the following:
MyClass someObject = new MyClass();
String json = toJSON(someObject);
Object tempObject = toObject(json);
MyClass sameObject = (MyClass) tempObject;
this code currently throws the following:
java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.MyClass
Any help on the matter would be appreciated!
return map.readValue(bytes, YourObject.class);