I am new to JSON. so please help me....
I am trying to write a Rest service that returns a Person object as follows:
PersonJSONAdapter implements JsonSerializer<Person>, JsonDeserializer<Person>
{
public JsonElement serialize(final Person src, final Type typeOfSrc, final JsonSerializationContext context)
{
if (src == null)
return null;
final JsonObject retVal = new JsonObject();
retVal.addProperty("firstname", src.getFirstname());
retVal.addProperty("lastname", src.getLastname());
return retVal;
}
so with this the output of the JSON looks like: { "firstname" : "XXX", "lastname" : "YYY" }
How can I make this one look like “Person” { “firstname”: “XXX”, "lastname":"YYY" }
Thanks for looking...
That's not valid JSON. JSON is only a data representation and has no knowledge of meta data such as type information.
The closest you can get is by explicitly adding the type as an extra property, such as:
{ "@type":"Person", "firstname":"XXX", "lastname":"YYY" }
This means however that you'll have to select the right class when you deserialize it. You might want to check out some libraries, but I think they either require you to specify the class yourself or to add custom annotations to serializable classes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.