欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jsonson GetStarted

程序员文章站 2022-06-01 07:49:47
...

1、 home page

Jackson home page

jackson: JSON library for java

java对象序列化和json字符串反序列化工具;

json字符串反序列化生成Java对象, 支持各种类型的数据类型绑定, 支持基本数据类型及其包装类, String类型, 时间类型, 数组、集合类型等;

jsonson GetStarted

databind
ObjectMapper注册类型映射器;
ObjectMapper / ObjectReader / ObjectWriter

JsonFactory

ObjectCodec
codec编解码器
Object that implements conversion functionality between Java objects and JSON content.

ObjectMapper extends ObjectCodec

JsonParser
defines public API for reading JSON content.
读取json content;
由JsonFactory创建;

JsonGenerator 序列化pojos
defines public API for writing JSON content.
生成一个json content;
由JsonFactory创建;

一个JsonFactory包括:
ObjectMapper
JSONPaser
JSONGenerator

关注点应该在databind

2、jackson-docs

jackson-docs

Jackson Core: Streaming: JsonFactory.Feature; JsonParser.Feature; JsonGenerator.Feature

Jackson Annotations: This page lists all general-purpose Jackson 2.0 annotations, grouped by functionality.

易百教程

3、api


  final ObjectMapper mapper = new ObjectMapper(); // can use static singleton, inject: just make sure to reuse!
  MyValue value = new MyValue();
  // ... and configure
  File newState = new File("my-stuff.json");
  mapper.writeValue(newState, value); // writes JSON serialization of MyValue instance
  // or, read
  MyValue older = mapper.readValue(new File("my-older-stuff.json"), MyValue.class);

  // Or if you prefer JSON Tree representation:
  JsonNode root = mapper.readTree(newState);
  // and find values by, for example, using a {@link com.fasterxml.jackson.core.JsonPointer} expression:
  int age = root.at("/personal/age").getValueAsInt(); 

4、 databind

日期、枚举自定义转换

在ObjectMapper中定义module; 指定字段类型的序列化和反序列化;

 /**
     * Method for registering a module that can extend functionality
     * provided by this mapper; for example, by adding providers for
     * custom serializers and deserializers.
     * 
     * @param module Module to register
     */
    public ObjectMapper registerModule(Module module)
    {
        if (isEnabled(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS)) {
            Object typeId = module.getTypeId();
            if (typeId != null) {
                if (_registeredModuleTypes == null) {
                    // plus let's keep them in order too, easier to debug or expose
                    // in registration order if that matter
                    _registeredModuleTypes = new LinkedHashSet<Object>();
                }
                // try adding; if already had it, should skip
                if (!_registeredModuleTypes.add(typeId)) {
                    return this;
                }
            }
        }
        
   

5、注解

相关标签: jackson