0

I have a generic class Result<R> and a class JsonResponse

public class JsonResponse {
    private boolean success;
    private Result result; //getter/settter
}

In my api, i am returning the json response for class ProductDeliveryCounter

Constraint constraint= getDeliveryCounter();
JsonResponse returnObject=new JsonResponse();
Result<Constraint> proMap=new Result<Constraint>(constraint);
        returnObject.setResult(proMap);
        
            returnObject.setSuccess(true);        return returnObject;

Output: json result

Query: Is there a way I can change the object in the image to constraint

Expected Output: edited json result

Community
  • 1
  • 1
Aarti Goel
  • 25
  • 3
  • 1
    can you post the code where you parse JsonResponse to the JSON format or send it to the client? – Michal Štefanec Dec 19 '19 at 12:09
  • 1
    Could you please update your question and post whole method? also don't add images instead post the content in text only. – Kapil Dec 19 '19 at 12:10

1 Answers1

1

If you are using Jackson, you can create a customized serializer to achieve this.

Class CustomSerializer

class CustomSerializer extends JsonSerializer<JsonResponse> {
@Override
public void serialize(JsonResponse value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
    jgen.writeStartObject();
    jgen.writeBooleanField("success", value.isSuccess());

    ObjectMapper mapper = new ObjectMapper();
    JsonNode node = mapper.convertValue(value.getResult().getObject(), JsonNode.class);
    JsonNode rootNode = mapper.createObjectNode();
    ((ObjectNode) rootNode).set(value.getResult().getObject().getClass().getSimpleName(), node);
    jgen.writeObjectField("result", rootNode);

    jgen.writeEndObject();
}

}

And then add @JsonSerialize(using = CustomSerializer.class) to your class JsonResponse as follows.

@JsonSerialize(using = CustomSerializer.class)
public class JsonResponse {
    private boolean success;
    private Result<?> result;

    //general getters ans setters
}

Code snippet for verification

Constraint constraint = new Constraint();
constraint.setCutOffTime("2019-12-19T20:59:59.059");
constraint.setCurrentTime("2019-12-19T16:51:08.970");
constraint.setDeliveryPossible(false);

Result<Constraint> result = new Result<>(constraint);
JsonResponse returnObject = new JsonResponse();
returnObject.setSuccess(true);
returnObject.setResult(result);

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(returnObject));

Console output

{"success":true,"result":{"Constraint":{"cutOffTime":"2019-12-19T20:59:59.059","currentTime":"2019-12-19T16:51:08.970","isDeliveryPossible":false}}}

LHCHIN
  • 3,679
  • 2
  • 16
  • 34
  • Thanks, the solution worked. Is there any way to rename object as the name in annotation value over the class as well? – Aarti Goel Dec 23 '19 at 04:32
  • @Aarti Goel Hi, I think this should be another question. BTW, please accept my post as answer if it solved your problem, thanks! – LHCHIN Dec 23 '19 at 05:32
  • @Aarti Goel Thank you! It sounds like that you don't want to rename it with class name? If yes, I think you should create a new question for this. – LHCHIN Dec 23 '19 at 09:37