I have a class with some fields and extra data of type string that will store a json object. I want it to not be deserialized, but after looking for more than an hour, the only thing I found was:
@Expose(deserialize=false)
But It doesn't work.
The following is an example:
public class Employee implements Serializable
{
private static final long serialVersionUID = 1L;
@Expose
public Long id;
@Expose
public String name;
... some more fields
@SerializedName("extraData")
@Expose(deserialize = false)
public String extraData;
}
Then on my service I do the following:
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
Employee emp = gson.fromJson(json, Employee.class);
The object I am receiving from the frontend is:
{
"id":1,
"name": "John Doe",
"extraData": {"someField1":"someValue1","someField2":"someValue2"}
}
And I get this error: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 48 path $.extraData
When I just want to get "extraData" as an string with the value
{"someField1":"someValue1","someField2":"someValue2"}
Other solutions are not accepted because it is the spec i was given.