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

The following examples show how to use com.fasterxml.jackson.databind.ObjectMapper#setTimeZone() . 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: Jackson.java    From spring-boot-plus with Apache License 2.0 6 votes vote down vote up
/**
 * 键按自然顺序格式化输出
 *
 * @param object
 * @param prettyFormat
 * @return
 */
public static String toJsonString(Object object, boolean prettyFormat) {
    if (object == null) {
        return null;
    }
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        // 格式化输出
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, prettyFormat);
        // 键按自然顺序输出
        objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
        // 设置时区
        objectMapper.setTimeZone(timeZone);
        return objectMapper.writeValueAsString(object);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 2
Source File: Jackson.java    From spring-boot-plus with Apache License 2.0 6 votes vote down vote up
/**
 * 键按自然顺序格式化输出
 *
 * @param object
 * @param prettyFormat
 * @return
 */
public static String toJsonStringNonNull(Object object, boolean prettyFormat) {
    if (object == null) {
        return null;
    }
    ObjectMapper objectMapper = new ObjectMapper();
    try {
        // 格式化输出
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, prettyFormat);
        // 键按自然顺序输出
        objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
        // 为空的序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 设置时区
        objectMapper.setTimeZone(timeZone);
        return objectMapper.writeValueAsString(object);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 3
Source File: JacksonObjectMapper.java    From frostmourne with MIT License 6 votes vote down vote up
public static ObjectMapper settingCommonObjectMapper(ObjectMapper objectMapper) {
    objectMapper.setDateFormat(new StdDateFormat());
    objectMapper.setTimeZone(TimeZone.getDefault());

    objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);

    objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
    objectMapper.configure(MapperFeature.INFER_PROPERTY_MUTATORS, false);
    objectMapper.configure(MapperFeature.ALLOW_FINAL_FIELDS_AS_MUTATORS, false);

    return objectMapper;
}
 
Example 4
Source File: PostgreSQLJsonBinaryTypeProgrammaticConfigurationSupplierTest.java    From hibernate-types with Apache License 2.0 6 votes vote down vote up
@Override
protected void additionalProperties(Properties properties) {
    ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT"));
    SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null, null, null));
    simpleModule.addSerializer(new MoneySerializer());
    objectMapper.registerModule(simpleModule);

    JsonBinaryType jsonBinaryType = new JsonBinaryType(objectMapper, Location.class);

    properties.put("hibernate.type_contributors",
        (TypeContributorList) () -> Collections.singletonList(
            (typeContributions, serviceRegistry) ->
                typeContributions.contributeType(
                    jsonBinaryType, "location"
                )
        )
    );
}
 
Example 5
Source File: AbstractAPI.java    From JAVA-HTTP-SDK with MIT License 5 votes vote down vote up
private ObjectMapper initObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    //关闭字段不识别报错
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    //调整默认时区为北京时间
    TimeZone timeZone = TimeZone.getTimeZone("GMT+8");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    dateFormat.setTimeZone(timeZone);
    objectMapper.setDateFormat(dateFormat);
    objectMapper.setTimeZone(timeZone);
    return objectMapper;
}
 
Example 6
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 7
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 8
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 9
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 10
Source File: CustomObjectMapperSupplier.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT"));
    SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null, null, null));
    simpleModule.addSerializer(new MoneySerializer());
    objectMapper.registerModule(simpleModule);
    return objectMapper;
}
 
Example 11
Source File: CustomObjectMapperSupplier.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT"));
    SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null, null, null));
    simpleModule.addSerializer(new MoneySerializer());
    objectMapper.registerModule(simpleModule);
    return objectMapper;
}
 
Example 12
Source File: CustomObjectMapperSupplier.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
    objectMapper.setTimeZone(TimeZone.getTimeZone("GMT"));
    SimpleModule simpleModule = new SimpleModule("SimpleModule", new Version(1, 0, 0, null, null, null));
    simpleModule.addSerializer(new MoneySerializer());
    objectMapper.registerModule(simpleModule);
    return objectMapper;
}
 
Example 13
Source File: Jackson.java    From dubbox-hystrix 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 14
Source File: DefaultJacksonObjectMapperProvider.java    From dubbox-hystrix 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 15
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 16
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 17
Source File: JsonUtilities.java    From openemm with GNU Affero General Public License v3.0 5 votes vote down vote up
public static ObjectMapper getObjectMapper(TimeZone timezone, String dateFormatPattern) {
	ObjectMapper mapper = new ObjectMapper();

	mapper.setTimeZone(timezone);
	mapper.setDateFormat(DateUtilities.getFormat(dateFormatPattern, timezone));
	mapper.registerModule(new LocalDateTimeModule());
	mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

	return mapper;
}