2016-01-11 63 views
0

我写一个类JsonUtils其中将包含不同的功能序列化和反序列化数据。以通用方式设置ObjectMapper属性?

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类?

+0

您可以使用杰克逊对象映射器,因为它可以方便您所需的操作。 –

+0

@AururRahmanMunna:我使用jackson ObjectMapper本身。你指的是哪一个objectmapper? –

回答

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实例和属性是最后(意味着你将永远创建ObjectMapper具有相同的属性)比作一个方法。

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

第二个问题看Builder Pattern

对于工厂更好的测试变化的界面会有所帮助:

ObjectMapperFactory。类

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

但是,这仅仅是一个variantion。为了您的目的,上面发布的答案已足够。

+0

那么在每个函数中我都必须通过objectmapper实例? –

+0

在这里添加属性,我将不得不将它添加到ObjectMapperProperties和ObjectMapperPropertiesBuilder中。这不是重复吗? –

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

ObjectMapper中有多个属性,就像注册一个模块来添加JsonSerailizer一样,所以这将导致函数的多个参数。所以这会导致很多参数到函数 –

+0

让我们添加一个hashmap作为参数,这可能有助于这里。 –

+0

将ObjectMapper实例传递给每个函数是个好主意吗?因此,函数的每个调用者都会根据其需要 –

相关问题