0

我正在编写一个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 类?

4

2 回答 2

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;
    }

}

客户端类

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 实例并且属性是最终的(意味着您将始终创建具有相同属性的 ObjectMapper)而不是创建一个方法。

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

第二个问题见建造者模式

为了更好地测试以 Factory 作为接口的变化,这将是有帮助的:

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;
      }
}

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

于 2016-01-11T10:37:48.893 回答
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;
            }
于 2016-01-11T08:32:10.290 回答