简体   繁体   中英

How serialize an object as a value to ObjectNode in Jackson?

I'm using jackson to serialize my object as json. I'm using following code:

ObjectMapper mapper = new ObjectMapper();
JsonNodeFactory nodeFactory = new JsonNodeFactory(false);
ObjectNode resNode = new ObjectNode(nodeFactory);

SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(ApplicationVersion.class, new SingleApplicationSerializer());
mapper.registerModule(simpleModule);
JsonNode appObject = mapper.valueToTree(appVersion);

resNode.put("status", true);
resNode.put("appObject", appObject);     //This put method is deprecated.

But I saw the put method with JsonNode overload is deprecated.Why following overload of put method is deprecated:

public JsonNode put(String fieldName, JsonNode value);

What is the alternative for that?


Also I tried this:

String jsonStr = mapper.writeValueAsString(appVersion);
resNode.put("status", true);
resNode.put("appObject", jsonStr);

But this method puts double quote ( " ) around appObject value as a string value.

They added a new method for these actions. ObjectNode.put(String, JsonNode)

Try instead resNode.set("appObject", appObject); .

不推荐使用ObjectNode.put()

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