public IiifObjectMapper() { this.checkJacksonVersion(); // Don't include null properties this.setSerializationInclusion(Include.NON_NULL); // Both are needed to add `@context` to the top-level object this.disable(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS); this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // Some array fields are unwrapped during serialization if they have only one value this.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); // Register the problem handler this.addHandler(new ProblemHandler()); // Disable writing dates as timestamps this.registerModule(new JavaTimeModule()); this.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // Enable automatic detection of parameter names in @JsonCreators this.registerModule(new ParameterNamesModule()); // Register the module this.registerModule(new IiifModule()); }
public static ObjectMapper objectMapper() { return new ObjectMapper() // Property visibility .setDefaultPropertyInclusion(JsonInclude.Include.ALWAYS) .setDefaultVisibility(JsonAutoDetect.Value.construct(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC)) .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true) .setSerializationInclusion(JsonInclude.Include.NON_NULL) .configure(SerializationFeature.WRITE_NULL_MAP_VALUES, true) // Property naming and order .setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE) // Customised de/serializers // Formats, locals, encoding, binary data .setDateFormat(new SimpleDateFormat("MM/dd/yyyy")) .setDefaultPrettyPrinter(new DefaultPrettyPrinter()) .setLocale(Locale.CANADA); }
@Before public void setUp() throws Exception { mapper = Jackson2ObjectMapperBuilder.json() .featuresToDisable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .build(); mapper.findAndRegisterModules(); }
@Override public void afterPropertiesSet() throws Exception { //Configure the objectMapper ready for use objectMapper = new ObjectMapper(); objectMapper.registerModule(module); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); //or NON_EMPTY? // this is deprecated in jackson 2.9 and there is no straight replacement // https://github.com/FasterXML/jackson-databind/issues/1547 objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); DateFormat DATE_FORMAT_ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); DATE_FORMAT_ISO8601.setTimeZone(TimeZone.getTimeZone("UTC")); objectMapper.setDateFormat(DATE_FORMAT_ISO8601); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); }
@Override public void initialize(final Bootstrap<EndpointConfiguration> bootstrap) { bootstrap.getObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); bootstrap.addBundle(new SwaggerBundle<EndpointConfiguration>() { @Override protected SwaggerBundleConfiguration getSwaggerBundleConfiguration( EndpointConfiguration configuration) { return configuration.swagger; } }); }
@Override public void extendMapper(ObjectMapper mapper) { SimpleModule restModule = new SimpleModule("RestModule", new Version(1, 0, 0, null)); // TODO this probably should be somewhere else, but it can't be in // com.tle.core.jackson // as that would make it dependent on equella i18n restModule.addSerializer(new I18NSerializer()); mapper.registerModule(restModule); mapper.registerModule(new JavaTypesModule()); mapper.registerModule(new RestStringsModule()); mapper.setSerializationInclusion(Include.NON_NULL); // dev mode! if( DebugSettings.isDebuggingMode() ) { mapper.configure(SerializationFeature.INDENT_OUTPUT, true); } mapper.setDateFormat(new ISO8061DateFormatWithTZ()); }
@Test public void testCache() throws Exception { String projectId = "02a70003-e864-464e-b62c-e0ede97deb8c"; DeliveryClient client = new DeliveryClient(projectId); final boolean[] cacheHit = {false}; client.setCacheManager(new CacheManager() { @Override public JsonNode resolveRequest(String requestUri, HttpRequestExecutor executor) throws IOException { Assert.assertEquals("https://deliver.kenticocloud.com/02a70003-e864-464e-b62c-e0ede97deb8c/items/on_roasts", requestUri); cacheHit[0] = true; ObjectMapper objectMapper = new ObjectMapper(); objectMapper.registerModule(new JSR310Module()); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); return objectMapper.readValue(this.getClass().getResourceAsStream("SampleContentItem.json"), JsonNode.class); } }); ContentItemResponse item = client.getItem("on_roasts"); Assert.assertNotNull(item); Assert.assertTrue(cacheHit[0]); }
private JsonMapper() { // calls the default constructor super(); // configures ISO8601 formatter for date without time zone // the used format is 'yyyy-MM-dd' setDateFormat(new SimpleDateFormat(FMT_ISO_LOCAL_DATE)); // enforces to skip null and empty values in the serialized JSON output setSerializationInclusion(JsonInclude.Include.NON_EMPTY); // enforces to skip null references in the serialized output of map configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); // enables serialization failures, when mapper encounters unknown properties names configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); // configures the format to prevent writing of the serialized output for dates // instances as timestamps; any date should be written in ISO format configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); }
public static String objectToString (final Object object) { if (object == null) { return null; } try { final StringWriter stringWriter = new StringWriter(); final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true); objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); objectMapper.writeValue(stringWriter, object); return stringWriter.toString().replaceAll(System.getProperty("line.separator"), ""); } catch (IOException ex) { STRINGUTIL_LOGGER.info("Sorry. had a error on during Object to String. ("+ex.toString()+")"); return null; } }
@Test public void deserializeWithStrategy() throws Exception { ObjectMapper mapper = new ObjectMapper().configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) .setPropertyNamingStrategy(new EsPropertyNamingStrategy( EsDailySnapshotInstance.class, EsInstanceStore.class)) .configure(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING, true); EsDailySnapshotInstance inst = mapper.readValue(doc, EsDailySnapshotInstance.class); Assert.assertEquals("coreapp-webapp-prod-0a018ef5", inst.getName()); Assert.assertEquals("fixed", inst.getLifecycle()); Assert.assertTrue(inst.getLaunchTime() != null); }
public JsonMapper() { // calls the default constructor super(); // configures ISO8601 formatter for date without time zone // the used format is 'yyyy-MM-dd' super.setDateFormat(new SimpleDateFormat(FMT_ISO_LOCAL_DATE)); // enforces to skip null and empty values in the serialized JSON output super.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); // enforces to skip null references in the serialized output of Map super.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); // enables serialization failures, when mapper encounters unknown properties names super.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true); // configures the format to prevent writing of the serialized output for java.util.Date // instances as timestamps. any date should be written in ISO format super.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); }
private void configureerMapper() { // Configuratie this.disable(MapperFeature.AUTO_DETECT_CREATORS); this.disable(MapperFeature.AUTO_DETECT_FIELDS); this.disable(MapperFeature.AUTO_DETECT_GETTERS); this.disable(MapperFeature.AUTO_DETECT_IS_GETTERS); this.disable(MapperFeature.AUTO_DETECT_SETTERS); // Default velden niet als JSON exposen (expliciet annoteren!) setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.NONE); this.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); this.enable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS); // serialization this.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); setSerializationInclusion(JsonInclude.Include.NON_EMPTY); this.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX); // deserialization this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); }
public static ObjectMapper createObjectMapper() { final YAMLFactory yamlFactory = new YAMLFactory() .configure(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID, false) .configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true) .configure(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS, true) .configure(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID, false); ObjectMapper mapper = new ObjectMapper(yamlFactory) .registerModule(new Jdk8Module()) .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .enable(SerializationFeature.INDENT_OUTPUT) .disable(SerializationFeature.WRITE_NULL_MAP_VALUES); for (Step step : ServiceLoader.load(Step.class, YamlHelpers.class.getClassLoader())) { mapper.registerSubtypes(new NamedType(step.getClass(), step.getKind())); } return mapper; }
@Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() { return builder -> { TimeZone tz = TimeZone.getTimeZone("UTC"); DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); df.setTimeZone(tz); builder .failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .dateFormat(df); }; }
public static ObjectMapper createObjectMapper() { final YAMLFactory yamlFactory = new YAMLFactory() .configure(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID, false) .configure(YAMLGenerator.Feature.MINIMIZE_QUOTES, true) .configure(YAMLGenerator.Feature.ALWAYS_QUOTE_NUMBERS_AS_STRINGS, true) .configure(YAMLGenerator.Feature.USE_NATIVE_TYPE_ID, false); ObjectMapper mapper = new ObjectMapper(yamlFactory) .setSerializationInclusion(JsonInclude.Include.NON_EMPTY) .enable(SerializationFeature.INDENT_OUTPUT) .disable(SerializationFeature.WRITE_NULL_MAP_VALUES); for (Step step : ServiceLoader.load(Step.class, YamlHelpers.class.getClassLoader())) { mapper.registerSubtypes(new NamedType(step.getClass(), step.getKind())); } return mapper; }
private void writeTypescriptConfig() throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); ObjectNode root = mapper.createObjectNode(); ObjectNode compilerOptions = root.putObject("compilerOptions"); compilerOptions.put("baseUrl", ""); compilerOptions.put("declaration", true); compilerOptions.put("emitDecoratorMetadata", true); compilerOptions.put("experimentalDecorators", true); compilerOptions.put("module", "es6"); compilerOptions.put("moduleResolution", "node"); compilerOptions.put("sourceMap", true); compilerOptions.put("target", "es5"); ArrayNode typeArrays = compilerOptions.putArray("typeRoots"); typeArrays.add("node_modules/@types"); ArrayNode libs = compilerOptions.putArray("lib"); libs.add("es6"); libs.add("dom"); File outputSourceDir = new File(outputDir, config.getSourceDirectoryName()); File file = new File(outputSourceDir, "tsconfig.json"); file.getParentFile().mkdirs(); mapper.writer().writeValue(file, root); }
/** * Creates a new EclipseParser * * @param sourceFile String of source file to read * @param outJ JSON parsed out * @throws IOException when file can't be opened or errors in reading/writing */ public EclipseParser(String sourceFile, PrintStream outJ, boolean prettyprint) throws IOException { File file = new File(sourceFile); final BufferedReader reader = new BufferedReader(new FileReader(file)); char[] source = IOUtils.toCharArray(reader); reader.close(); this.parser = ASTParser.newParser(AST.JLS8); parser.setSource(source); parser.setKind(ASTParser.K_COMPILATION_UNIT); final JsonFactory jsonF = new JsonFactory(); jG = jsonF.createGenerator(outJ); if (prettyprint) { jG.setPrettyPrinter(new DefaultPrettyPrinter()); mapper.enable(SerializationFeature.INDENT_OUTPUT); } mapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); SimpleModule module = new SimpleModule(); module.addSerializer(ASTNode.class, new NodeSerializer()); mapper.registerModule(module); }
@Override public <T> T map(String json, Class<T> clazz) { if (null == json) { throw new IllegalArgumentException("Can not map empty json"); } ObjectMapper mapper = new ObjectMapper(); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); JsonUnmarshaller jsonUnmarshaller = new FixedDefaultJsonUnmarshaller(mapper); try { return jsonUnmarshaller.unmarshal(clazz, json); } catch (IOException e) { log.warn("Error during mapping", e); return null; } }
public static ObjectMapper createDefaultMapper() { final ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); mapper.configure(SerializationFeature.INDENT_OUTPUT, false); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); SimpleModule module = new SimpleModule(); module.addSerializer(Transaction.class, new TransactionSerializer()); module.addDeserializer(Transaction.class, new TransactionDeserializer()); module.addSerializer(Difficulty.class, new DifficultySerializer()); module.addDeserializer(Difficulty.class, new DifficultyDeserializer()); module.addSerializer(Block.class, new BlockSerializer()); module.addDeserializer(Block.class, new BlockDeserializer()); mapper.registerModule(module); return mapper; }
/** * Initializes the JSON constructor. */ public FloodAttackJsonMapper() { super(); SimpleModule module = new SimpleModule(); module.addSerializer(HttpFloodAttack.class, HttpFloodAttackSerializer.getInstance()); module.addDeserializer(HttpFloodAttack.class, HttpFloodAttackDeserializer.getInstance()); super.registerModule(module); super.enable(SerializationFeature.INDENT_OUTPUT); }
public JacksonContextResolver() throws Exception { this.objectMapper = new ObjectMapper() .registerModule(new Jdk8Module()) .setSerializationInclusion(JsonInclude.Include.NON_ABSENT) .configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true) .configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true); }
public ObjectMapper provideObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); objectMapper.setDateFormat(provideDateFormat()); return objectMapper; }
private RestObjectMapper() { // swagger中要求date使用ISO8601格式传递,这里与之做了功能绑定,这在cse中是没有问题的 setDateFormat(new ISO8601DateFormat()); getFactory().disable(Feature.AUTO_CLOSE_SOURCE); disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); }
private static ObjectMapper createObjectMapper() { final ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setSerializationInclusion(JsonInclude.Include.NON_DEFAULT); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); return mapper; }
private String toJson(final Object object) throws JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); String jsonRep = ""; jsonRep = mapper.writeValueAsString(object); return jsonRep; }
/** * Initialize object mapper. * * @return the object mapper */ protected ObjectMapper initializeObjectMapper() { final ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); return mapper; }
public ObjectMapper objectMapper() { return new ObjectMapper() .registerModule(new Jdk8Module()) .registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES)) .registerModule(new JavaTimeModule()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); }
private void readDataFromFile() throws IOException { File recordFile = getRecordFile(testName); ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); recordedData = mapper.readValue(recordFile, RecordedData.class); System.out.println("Total records " + recordedData.getNetworkCallRecords().size()); }
/** * Serializes the Service Builder element (entity, finder, order, etc) into * a String representing the XML output. * * @return the serialized service builder element * @throws JsonProcessingException if an exception occurs */ @Override public String serialize() throws JsonProcessingException { ObjectMapper xmlMapper = new XmlMapper(); xmlMapper.enable(SerializationFeature.INDENT_OUTPUT); xmlMapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); return xmlMapper.writeValueAsString(_serviceBuilderElement); }
protected Jackson2ObjectMapperBuilder jacksonBuilder() { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.modules(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES), new JavaTimeModule(), new FriendlyIdModule()); builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); builder.simpleDateFormat("yyyy-MM-dd"); builder.indentOutput(true); return builder; }
private static synchronized ObjectMapper getMapper(){ if(MAPPER == null){ ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); mapper.configure(Feature.ALLOW_COMMENTS, true); MAPPER = mapper; } return MAPPER; }
/** * Configure mapper. * * @param mapper the mapper */ protected void configureObjectMapper(final ObjectMapper mapper) { mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false) .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); mapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); mapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.PROTECTED_AND_PUBLIC); if (isDefaultTypingEnabled()) { mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); } mapper.findAndRegisterModules(); }
@Before public void setUp() throws Exception { // needed in order to serialize ZonedDateTime class mapper = Jackson2ObjectMapperBuilder.json() .featuresToDisable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .build(); mapper.findAndRegisterModules(); }
public CSVWriter(Class<T> class1, Class<?> mixin) { mapper = new CsvMapper(); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.setDateFormat(new ISO8601WithMillisFormat()); if (mixin != null) { mapper.addMixIn(class1, mixin); } schema = mapper.schemaFor(class1).withHeader(); }
public void init() { // 排除值为空属性 setSerializationInclusion(JsonInclude.Include.NON_NULL); // 进行缩进输出 configure(SerializationFeature.INDENT_OUTPUT, true); // 将驼峰转为下划线 if (camelCaseToLowerCaseWithUnderscores) { setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); } // 进行日期格式化 if (!StringUtils.isEmpty(dateFormatPattern)) { DateFormat dateFormat = new SimpleDateFormat(dateFormatPattern); setDateFormat(dateFormat); } }
YamlJackson2HttpMessageConverter() { super(new YAMLMapper().enable(Feature.MINIMIZE_QUOTES), MediaType.parseMediaType("application/x-yaml")); this.getObjectMapper().configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false); this.getObjectMapper().configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true); this.getObjectMapper().enable(SerializationFeature.INDENT_OUTPUT); this.getObjectMapper().setSerializationInclusion(Include.NON_NULL); }