Best way to use Jackson JsonNodeFactory

前端 未结 4 409
北恋
北恋 2021-01-30 22:34

I\'m using Jackson to build a custom JSON object. Is the correct way of going about this?

It seems to work well (and the output is correct) but I may be missing the way

4条回答
  •  温柔的废话
    2021-01-30 22:36

    This works, although intention is that it's factory that creates instances. But most commonly you just access all of it using ObjectMapper, like:

    ObjectMapper mapper = new ObjectMapper();
    ObjectNode dataTable = mapper.createObjectNode();
    ArrayNode aa = dataTable.putArray("aaData");
    

    The main reason for separate JsonNodeFactory is to allow you to create custom node types (usually sub-classes of standard instances); and then configure ObjectMapper to use different factory. For convenience, ArrayNode and ObjectNode do have reference to a factory instance, which is used with "putArray" and other methods that need to create new nodes.

提交回复
热议问题