ObjectMapper属性以通用方式设置?

时间:2016-01-11 07:21:11

标签: java json jackson objectmapper

我正在编写一个类JsonUtils,它将包含不同的函数来序列化和反序列化数据。

 public class JsonUtils {

        private static final ObjectMapper JSON_MAPPER = new ObjectMapper();

        public static String toJsonString(Object obj)  {

            String json = null;

            JSON_MAPPER.setPropertyNamingStrategy(new CustomNamingStrategy());
            JSON_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);

            try {
                System.out.print("OBJECT MAPPER:---> JSON STRING:\n" + JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj));
                json = JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
            } catch (JsonGenerationException e) {
                e.printStackTrace();
            } catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return json;
        }

        public static <T> T toPOJO(String json, Class<T> type){

            JSON_MAPPER.setPropertyNamingStrategy(new CustomNameNamingStrategy());
            System.out.println("TO POJO: Json string " + json);
            try {

                return JSON_MAPPER.readValue(json, type);

            } catch (JsonParseException e) {
                e.printStackTrace();
            } catch (JsonMappingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

现在,我希望通常使用这些功能。例如:有人想调用toJsonString方法,但想使用不同的命名策略转换为json。或者可能想要向ObjectMapper添加一些其他属性,例如注册模块。

目前,函数内部正在设置ObjectMapper属性,因此无法使用新的命名策略或ObjectMapper的不同属性。

JsonUtils的每个用户最初是否都为ObjectMapper设置了自己的属性?或者是一种有效且通用的方法来编写我的Utility类?

2 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

ObjectMapperProperties.java

package example;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

        public class ObjectMapperProperties {

            private PropertyNamingStrategy propertyNamingStrategy;

            private ObjectMapperProperties(final PropertyNamingStrategy propertyNamingStrategy) {
                this.propertyNamingStrategy = propertyNamingStrategy;
            }

            public PropertyNamingStrategy getPropertyNamingStrategy() {
                return propertyNamingStrategy;
            }

            public static class ObjectMapperPropertiesBuilder {

                private PropertyNamingStrategy builderPropertyNamingStrategy;

                public ObjectMapperPropertiesBuilder() {

                }

                public ObjectMapperPropertiesBuilder setPropertyNamingStrategy(final PropertyNamingStrategy builderPropertyNamingStrategy) {
                    this.builderPropertyNamingStrategy = builderPropertyNamingStrategy; 
                    return this;
                }

                public ObjectMapperProperties build() {
                    return new ObjectMapperProperties(builderPropertyNamingStrategy);
                }

            }
    }

ObjectMapperFactory.java

package example;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ObjectMapperFactory {

    public static ObjectMapper getObjectMapper(final ObjectMapperProperties objectMapperProperties) {
        final ObjectMapper result = new ObjectMapper();
        result.setPropertyNamingStrategy(objectMapperProperties.getPropertyNamingStrategy());
        return result;
    }

}

Client.class

package example;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

import example.ObjectMapperProperties.ObjectMapperPropertiesBuilder;

public class Client {

    public static void main(String[] args) {
        ObjectMapperPropertiesBuilder objectMapperPropertiesBuilder = new ObjectMapperPropertiesBuilder();
        objectMapperPropertiesBuilder.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);
        ObjectMapperFactory factory = new ObjectMapperFactory();
        ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper(objectMapperPropertiesBuilder.build());

    }

}

您可以根据需要设置ObjectMapper。它没有意义,而且在已经创建的实例上设置属性两次都容易出错。

private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
JSON_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);

所以下次你需要重置这个属性,但是通过一些工厂创建新的ObjectMapper()是无价的,不易出错

数目:

不会为每次调用通过ObjectMapperFactory创建ObjectMapper的新实例,只需传递ObjectMapperProperties。

public static String toJsonString(Object obj,final ObjectMapperProperties objectMapperProperties)  {

ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper(objectMapperProperties);
}

如果您不想创建新的ObjectMapper实例,并且属性是final(意味着您将始终创建具有相同属性的ObjectMapper),而不是创建方法。

public static String toJsonString(Object obj, ObjectMapper objMapper)  {}

第二个问题见Builder Pattern

为了更好地测试变量,使用Factory as界面会很有帮助:

ObjectMapperFactory.class

public interface ObjectMapperFactory {

    public ObjectMapper getObjectMapper(final ObjectMapperProperties objectMapperProperties) {
}

ObjectMapperFactory的实现

ObjectMapperFactoryImpl.class

package example;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ObjectMapperFactoryImpl implements ObjectMapperFactory {

    public ObjectMapper getObjectMapper(final ObjectMapperProperties objectMapperProperties) {
        final ObjectMapper result = new ObjectMapper();
        result.setPropertyNamingStrategy(objectMapperProperties.getPropertyNamingStrategy());
        return result;
    }

}

并在你的班级

 public class JsonUtils {

      private final ObjectMapperFactory objectMapperFactory;

      public JsonUtils(final ObjectMapperFactory objectMapperFactory) {
              this.objectMapperFactory = objectMapperFactory;
      }
}

但这只是一种变化。为了您的目的,上面发布的答案就足够了。

答案 1 :(得分:0)

你可以使用hashmap并在调用之前,从调用函数首先输入一些像这样的设置值

Map <String, String>settings = new HashMap<String, String>();
        settings.put("CUSTOM_NAMING_PROPERTY", "CAMEL_CASE");

并在函数toJsonString内查看值。

    public static String toJsonString(Object obj,Map settings )  {

                String json = null;
                if(settings.get("CUSTOM_NAMING_PROPERTY")!=null){
                    //put your settings here.......
                }
                /////....... contd.....

                JSON_MAPPER.setSerializationInclusion(Inclusion.NON_NULL);

                try {
                    System.out.print("OBJECT MAPPER:---> JSON STRING:\n" + JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj));
                    json = JSON_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
                } catch (JsonGenerationException e) {
                    e.printStackTrace();
                } catch (JsonMappingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return json;
            }