1. 程式人生 > >Java-很深我只知其一-ObjectMapper類

Java-很深我只知其一-ObjectMapper類

  • ObjectMapper類,是jackson包下一個類,java針對處理物件與json之間關係轉換用的
  • 不推薦使用。使用其他構造來代替; 注意,可以設定序列化工廠
  • 類中引入物件
  •     private static final JavaType JSON_NODE_TYPE = TypeFactory.type(JsonNode.class);
        protected static final ClassIntrospector<? extends BeanDescription> DEFAULT_INTROSPECTOR;
        protected static final AnnotationIntrospector DEFAULT_ANNOTATION_INTROSPECTOR;
        protected static final VisibilityChecker<?> STD_VISIBILITY_CHECKER;
        protected final JsonFactory _jsonFactory;
        protected TypeResolverBuilder<?> _defaultTyper;
        protected VisibilityChecker<?> _visibilityChecker;
        protected SubtypeResolver _subtypeResolver;
        protected ClassLoader _valueClassLoader;
        protected SerializationConfig _serializationConfig;
        protected SerializerProvider _serializerProvider;
        protected SerializerFactory _serializerFactory;
        protected DeserializationConfig _deserializationConfig;
        protected DeserializerProvider _deserializerProvider;
        protected final ConcurrentHashMap<JavaType, JsonDeserializer<Object>> _rootDeserializers;
  • 類中構造方法
  • public ObjectMapper() {
            this((JsonFactory)null, (SerializerProvider)null, (DeserializerProvider)null);
        }
    
        public ObjectMapper(JsonFactory jf) {
            this(jf, (SerializerProvider)null, (DeserializerProvider)null);
        }
    
        /** @deprecated */
        @Deprecated
        public ObjectMapper(SerializerFactory sf) {
            this((JsonFactory)null, (SerializerProvider)null, (DeserializerProvider)null);
            this.setSerializerFactory(sf);
        }
    
        public ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp) {
            this(jf, sp, dp, (SerializationConfig)null, (DeserializationConfig)null);
        }
    
        public ObjectMapper(JsonFactory jf, SerializerProvider sp, DeserializerProvider dp, SerializationConfig sconfig, DeserializationConfig dconfig) {
            this._rootDeserializers = new ConcurrentHashMap(64, 0.6F, 2);
            this._jsonFactory = (JsonFactory)(jf == null ? new MappingJsonFactory(this) : jf);
            this._visibilityChecker = STD_VISIBILITY_CHECKER;
            this._serializationConfig = sconfig != null ? sconfig : new SerializationConfig(DEFAULT_INTROSPECTOR, DEFAULT_ANNOTATION_INTROSPECTOR, this._visibilityChecker, (SubtypeResolver)null);
            this._deserializationConfig = dconfig != null ? dconfig : new DeserializationConfig(DEFAULT_INTROSPECTOR, DEFAULT_ANNOTATION_INTROSPECTOR, this._visibilityChecker, (SubtypeResolver)null);
            this._serializerProvider = (SerializerProvider)(sp == null ? new StdSerializerProvider() : sp);
            this._deserializerProvider = (DeserializerProvider)(dp == null ? new StdDeserializerProvider() : dp);
            this._serializerFactory = BeanSerializerFactory.instance;
        }

     

  • 需要引入jar包
  • import java.io.IOException;
    import org.codehaus.jackson.JsonParseException;
    import org.codehaus.jackson.map.JsonMappingException;
    import org.codehaus.jackson.map.ObjectMapper;
    import org.codehaus.jackson.map.SerializationConfig;

  • 經常會用到兩個方法
  • private static ObjectMapper objectMapper = new ObjectMapper();
  • String str = objectMapper.writeValueAsString(Object);//obj物件,返回json
  • Object obj = objectMapper.readValue(String, clazz.class);json,類物件,返回類物件
  • public static void main(String args[]){
    
    //建立ObjectMapper物件
          ObjectMapper mapper = new ObjectMapper();
    
    //建立Student實體類
    
    Student student = new Student();
    
    //建立josn格式物件
          String jsonString = "{\"name\":\"Mahesh\", \"age\":21}";
    
          //map json to student
          try {
    
    //json轉實體物件
             Student student = mapper.readValue(jsonString, Student.class);
             System.out.println(student);
    
    //實體物件轉換json
             jsonString = mapper.writeValueAsString(student);
             System.out.println(jsonString);
    
          } catch (JsonParseException e) {
             e.printStackTrace();
          } catch (JsonMappingException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }
       }

 

  • 輸出結果為
  • Student [ name: Mahesh, age: 21 ]
    
    {
    
      "name" : "Mahesh",
    
      "age" : 21
    
    }

chenyb 隨筆記錄,方便自己使用

2018-10-30