简体   繁体   中英

Not able to convert Json with @ in field to custom java Object

In my java code,

my BookRequestTO class

@Getter
@Builder
@EqualsAndHashCode
@ToString
@NoArgsConstructor
@AllArgsConstructor

public class BookRequestTO {
    private String id;

    @NotNull(message = Constants.FUNCTION_NULL)
    @Valid
    private BookInfo function;

    private List<String> parameters;
}

my BookInfo class

@Getter
@Builder
@EqualsAndHashCode
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class BookInfo {

    @NotEmpty(message = Constants.TYPE_NULL)
    @JsonProperty(value = "@type")
    private String type;

    @NotEmpty(message = Constants.ACTION_NULL)
    private String name;
}

My goal is to convert json with @ in its field to some custom object

I have tried below 2 approaches:

Approach 1:

import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

JSONObject jsonRequest = new JSONObject();
jsonRequest.put("@type", "education");
jsonRequest.put("name", "Geography");

JSONObject bookRequestToJson = new JSONObject();
bookRequestToJson.put("id", "1234");
bookRequestToJson.put("function", jsonRequest);
bookRequestToJson.put("parameters", new JSONArray());

BookRequestTO bookRequestTO = new ObjectMapper().readValue(bookRequestToJson.toString(), BookRequestTO.class);

System.out.println("BEFORE ObjectWriter: bookRequestTO " + bookRequestTO);

here @type is being ignored and I got only type=education in my response, as you can see below I was expecting it to be @type=education

BEFORE ObjectWriter: bookRequestTO BookRequestTO(id=1234, function=BookInfo(type=education, name=Geography), parameters=[])

Approach 2 with same code:

ObjectWriter ow1 = new ObjectMapper().writer().withDefaultPrettyPrinter();
String request = ow1.writeValueAsString(bookRequestToJson.toString()).replaceAll("\\\\", "");
request = request.substring(1, request.length() - 1);
System.out.println("AFTER ObjectWriter: bookRequestTO " + request);
        
BookRequestTO bookRequestTO1 = new Gson().fromJson(request, BookRequestTO.class);
System.out.println("AFTER Gson: bookRequestTO " + bookRequestTO1);

and after running the code below is the output, here it has ignored the actual type value and it has made it as null

AFTER ObjectWriter: bookRequestTO {"function":{"@type":"education","name":"Geography"},"id":"1234","parameters":[]}
AFTER Gson: bookRequestTO BookRequestTO(id=1234, function=BookInfo(type=null, name=Geography), parameters=[])

Can anyone help with this please? or Is it not possible to have @ in the java custom object.

This is because you are not use Jackson's ObjectMapper to output the BookInfo as a JSON string. In the first attempt, you are using Lombok's generated toString() method, which doesn't recognise the Jackson annotation @JsonProperty .

In your second attempt, you used Jackson successfully to write the JSON string, but the you used GSon to read and write the BookInfo. GSon also doesn't recognise Jackson's @JsonProperty annotation, so it sees the field "type" as just "type" and doesn't recognise the field "@type" when it sees it in the JSON string.

If you want to use GSon, you need to use its own annotation: @SerializedName("@type") .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM