Java Code Examples for com.fasterxml.jackson.databind.ObjectMapper#disable()

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#disable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestUtils.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static byte[] convertObjectToJsonBytes( Object object ) throws IOException
{
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );
    objectMapper.configure( SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false );
    objectMapper.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
    objectMapper.configure( SerializationFeature.WRAP_EXCEPTIONS, true );
    objectMapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );

    objectMapper.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
    objectMapper.configure( DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true );
    objectMapper.configure( DeserializationFeature.WRAP_EXCEPTIONS, true );

    objectMapper.disable( MapperFeature.AUTO_DETECT_FIELDS );
    objectMapper.disable( MapperFeature.AUTO_DETECT_CREATORS );
    objectMapper.disable( MapperFeature.AUTO_DETECT_GETTERS );
    objectMapper.disable( MapperFeature.AUTO_DETECT_SETTERS );
    objectMapper.disable( MapperFeature.AUTO_DETECT_IS_GETTERS );

    return objectMapper.writeValueAsBytes( object );
}
 
Example 2
Source File: ObjectMapperProvider.java    From jersey-jwt-springsecurity with MIT License 6 votes vote down vote up
private static ObjectMapper createObjectMapper() {

        ObjectMapper mapper = new ObjectMapper();

        mapper.registerModule(new Jdk8Module());
        mapper.registerModule(new JavaTimeModule());
        mapper.registerModule(new ParameterNamesModule());

        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);

        return mapper;
    }
 
Example 3
Source File: DefaultJacksonObjectMapperProvider.java    From dubbox with Apache License 2.0 5 votes vote down vote up
@Override
    public ObjectMapper getObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//            objectMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.setTimeZone(TimeZone.getDefault());
        return objectMapper;
    }
 
Example 4
Source File: ObjectMapperBeanEventListener.java    From micronaut-microservices-poc with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectMapper onCreated(BeanCreatedEvent<ObjectMapper> event) {
    final ObjectMapper mapper = event.getBean();
    mapper.registerModule(new JavaTimeModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    return mapper;
}
 
Example 5
Source File: DataRequestResponse.java    From Sensor-Data-Logger with Apache License 2.0 5 votes vote down vote up
public static DataRequestResponse fromJson(String json) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        DataRequestResponse dataRequestResponse = mapper.readValue(json, DataRequestResponse.class);
        return dataRequestResponse;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}
 
Example 6
Source File: EquipmentMessageSenderTest.java    From c2mon with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void privateTempTestToJSON() {
  try {

    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY);

    File tempOut = new File("temp.json");

    // Test Integer:
    SourceDataTagValue valueToWrite = sdt3.update(new ValueUpdate(1, "test", System.currentTimeMillis()));

    mapper.writeValue(tempOut, valueToWrite);

    SourceDataTagValue readValue = mapper.readValue(tempOut, SourceDataTagValue.class);
    assertTrue(readValue.equals(valueToWrite));

    // Test IntegerArray
    Integer[] integerArray = { 7, 8, 9 };
    valueToWrite = sdt3.update(new ValueUpdate(integerArray, "test", System.currentTimeMillis()));

    mapper.writeValue(tempOut, valueToWrite);

    readValue = mapper.readValue(tempOut, SourceDataTagValue.class);

    // Object Test
    SourceDataTagValue objectValue = sdt1.update(new ValueUpdate("Tach", "test2", System.currentTimeMillis()));
    valueToWrite = sdt3.update(new ValueUpdate(objectValue, "test", System.currentTimeMillis()));

    mapper.writeValue(tempOut, valueToWrite);

    readValue = mapper.readValue(tempOut, SourceDataTagValue.class);
    String blub = mapper.writeValueAsString(123456L);
  }
  catch (IOException e) {
    e.printStackTrace();
  }

}
 
Example 7
Source File: DefaultObjectMapperFactory.java    From nodes with Apache License 2.0 5 votes vote down vote up
public ObjectMapper newSerializerMapper() {
    final ObjectMapper mapper = new ObjectMapper();

    // JDK8+ Time and Date handling / JSR-310
    mapper.registerModule(new JavaTimeModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    mapper.disable(SerializationFeature.WRITE_DATES_WITH_ZONE_ID);
    mapper.disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);

    return mapper;
}
 
Example 8
Source File: GraphTest.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
private Graph readGraphFromFile(String filePath) throws URISyntaxException, IOException {
	ObjectMapper mapper = new ObjectMapper();
	mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
	return mapper.readValue(
			OLSHttpClientTest.getFile(filePath),
			Graph.class);
}
 
Example 9
Source File: TimedJsonStreamParser.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public TimedJsonStreamParser(List<TblColRef> allColumns, Map<String, String> properties) {
    this.allColumns = allColumns;
    if (properties == null) {
        properties = StreamingParser.defaultProperties;
    }

    tsColName = properties.get(PROPERTY_TS_COLUMN_NAME);
    tsParser = properties.get(PROPERTY_TS_PARSER);
    separator = properties.get(PROPERTY_EMBEDDED_SEPARATOR);
    strictCheck = Boolean.parseBoolean(properties.get(PROPERTY_STRICT_CHECK));

    if (!StringUtils.isEmpty(tsParser)) {
        try {
            Class clazz = Class.forName(tsParser);
            Constructor constructor = clazz.getConstructor(Map.class);
            streamTimeParser = (AbstractTimeParser) constructor.newInstance(properties);
        } catch (Exception e) {
            throw new IllegalStateException("Invalid StreamingConfig, tsParser " + tsParser + ", parserProperties " + properties + ".", e);
        }
    } else {
        throw new IllegalStateException("Invalid StreamingConfig, tsParser " + tsParser + ", parserProperties " + properties + ".");
    }
    mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.disable(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
    mapper.enable(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY);

    for (TblColRef col : allColumns) {
        colLowerCaseMap.put(col.getName(), col.getName().toLowerCase(Locale.ROOT));
    }
}
 
Example 10
Source File: Jackson.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private static synchronized void buildDefaultObjectMapper() {
        objectMapper = new ObjectMapper();
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
//            objectMapper.disable(SerializationFeature.FLUSH_AFTER_WRITE_VALUE);
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.setTimeZone(TimeZone.getDefault());
    }
 
Example 11
Source File: TreeMapSerializerTest.java    From beakerx with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void initClassStubData() {
  mapper = new ObjectMapper();
  mapper.disable(MapperFeature.AUTO_DETECT_GETTERS);
  mapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
  mapper.disable(MapperFeature.AUTO_DETECT_FIELDS);
  mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  treeMapSerializer = new TreeMapSerializer();

}
 
Example 12
Source File: Ranker.java    From ltr4l with Apache License 2.0 5 votes vote down vote up
public static Ranker get(Reader reader, Config override, int featLength) {
  String algorithm;
  ObjectMapper mapper = new ObjectMapper();
  mapper.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE);
  try {
    Map model = mapper.readValue(reader, Map.class);
    algorithm = ((String)model.get("algorithm")).toLowerCase();
    reader.reset();
  } catch (IOException ioe) {
    throw new RuntimeException(ioe);
  }
  return get(algorithm, reader, override, featLength);
}
 
Example 13
Source File: ObjectMapperBuilder.java    From moserp with Apache License 2.0 5 votes vote down vote up
public ObjectMapper build() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    mapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
    mapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    registerQuantitySerializer(mapper);
    mapper.registerModules(new MoneyModule(), new JavaTimeModule(), new Jackson2HalModule());

    return mapper;
}
 
Example 14
Source File: DataRequest.java    From Sensor-Data-Logger with Apache License 2.0 5 votes vote down vote up
public static DataRequest fromJson(String json) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        DataRequest dataRequest = mapper.readValue(json, DataRequest.class);
        return dataRequest;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}
 
Example 15
Source File: DataDeserializationMixinTest.java    From hj-t212-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testIgnore(){
    Map<String,String> map = new HashMap<>();
    map.put("ST","31");
    map.put("Flag","N");
    map.put("CP","&&k=v&&");
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
    objectMapper.addMixIn(Data.class,DataDeserializationMixin.class);

    Data data = objectMapper.convertValue(map, Data.class);
    assertNull(data.getDataFlag());
}
 
Example 16
Source File: JsonMapper.java    From dubai with MIT License 5 votes vote down vote up
public JsonMapper(Include include) {
	mapper = new ObjectMapper();
	// 设置输出时包含属性的风格
	if (include != null) {
		mapper.setSerializationInclusion(include);
	}
	// 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
	mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
 
Example 17
Source File: HttpClientSdkConfiguration.java    From kork with Apache License 2.0 5 votes vote down vote up
@Bean
public static SdkFactory httpClientSdkFactory(
    List<OkHttp3ClientFactory> okHttpClientFactories,
    Environment environment,
    Provider<Registry> registry) {

  OkHttpClientConfigurationProperties okHttpClientProperties =
      Binder.get(environment)
          .bind("ok-http-client", Bindable.of(OkHttpClientConfigurationProperties.class))
          .orElse(new OkHttpClientConfigurationProperties());

  OkHttpMetricsInterceptorProperties okHttpMetricsInterceptorProperties =
      Binder.get(environment)
          .bind(
              "ok-http-client.interceptor", Bindable.of(OkHttpMetricsInterceptorProperties.class))
          .orElse(new OkHttpMetricsInterceptorProperties());

  List<OkHttp3ClientFactory> factories = new ArrayList<>(okHttpClientFactories);
  OkHttp3MetricsInterceptor okHttp3MetricsInterceptor =
      new OkHttp3MetricsInterceptor(registry, okHttpMetricsInterceptorProperties.skipHeaderCheck);
  factories.add(new DefaultOkHttp3ClientFactory(okHttp3MetricsInterceptor));

  OkHttp3ClientConfiguration config =
      new OkHttp3ClientConfiguration(okHttpClientProperties, okHttp3MetricsInterceptor);

  // TODO(rz): It'd be nice to make this customizable, but I'm not sure how to do that without
  //  bringing Jackson into the Plugin SDK (quite undesirable).
  ObjectMapper objectMapper = new ObjectMapper();
  objectMapper.registerModule(new Jdk8Module());
  objectMapper.registerModule(new JavaTimeModule());
  objectMapper.registerModule(new KotlinModule());
  objectMapper.disable(READ_DATE_TIMESTAMPS_AS_NANOSECONDS);
  objectMapper.disable(WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
  objectMapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
  objectMapper.disable(FAIL_ON_EMPTY_BEANS);
  objectMapper.setSerializationInclusion(NON_NULL);

  return new HttpClientSdkFactory(
      new CompositeOkHttpClientFactory(factories), environment, objectMapper, config);
}
 
Example 18
Source File: JacksonDateUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSerializingJodaTime_thenCorrect() throws JsonProcessingException {
    final DateTime date = new DateTime(2014, 12, 20, 2, 30, DateTimeZone.forID("Europe/London"));

    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JodaModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

    final String result = mapper.writeValueAsString(date);
    assertThat(result, containsString("2014-12-20T02:30:00.000Z"));
}
 
Example 19
Source File: AppUtils.java    From OpenLabeler with Apache License 2.0 5 votes vote down vote up
public static ObjectMapper createJSONMapper() {
   ObjectMapper mapper = new ObjectMapper();

   // SerializationFeature for changing how JSON is written
   mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
   mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

   // DeserializationFeature for changing how JSON is read as POJOs:
   mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
   mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
   return mapper;
}
 
Example 20
Source File: PolicyElasticRepositoryGetAgentSalesTest.java    From micronaut-microservices-poc with Apache License 2.0 4 votes vote down vote up
private static ObjectMapper objectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JavaTimeModule());
    mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    return mapper;
}