public SerializationConfig getSerializationConfig() { return mapper.getSerializationConfig(); }
private JsonFactory createJsonFactory() { final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.getSerializationConfig().disable( SerializationConfig.Feature.FLUSH_AFTER_WRITE_VALUE ); JsonFactory factory = new JsonFactory( objectMapper ) { @Override protected JsonGenerator _createUTF8JsonGenerator( OutputStream out, IOContext ctxt ) { final int bufferSize = 1024 * 8; Utf8Generator gen = new Utf8Generator( ctxt, _generatorFeatures, _objectCodec, out, new byte[bufferSize], 0, true ); if ( _characterEscapes != null ) { gen.setCharacterEscapes( _characterEscapes ); } return gen; } }; factory.disable( JsonGenerator.Feature.FLUSH_PASSED_TO_STREAM ); return factory; }
private static ObjectMapper createCombinedObjectMapper() { final Pair combinedIntrospector = createJaxbJacksonAnnotationIntrospector(); final ObjectMapper result = new ObjectMapper(); result.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true); result.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true); result.setDeserializationConfig(result.getDeserializationConfig().withAnnotationIntrospector(combinedIntrospector)); result.setSerializationConfig(result.getSerializationConfig().withAnnotationIntrospector(combinedIntrospector)); return result; }
objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() .withGetterVisibility(JsonAutoDetect.Visibility.NONE) .withSetterVisibility(JsonAutoDetect.Visibility.NONE));
public void printJson(final SessionState.LogHelper console) { try { waitForReturnSessionEvent(); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false); // serialize json based on field annotations only objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig().getDefaultVisibilityChecker() .withSetterVisibility(JsonAutoDetect.Visibility.NONE)); String wmContextJson = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this); console.printInfo(""); console.printInfo(WM_EVENTS_TITLE); console.printInfo(SEPARATOR); console.printInfo(wmContextJson); console.printInfo(SEPARATOR); console.printInfo(""); } catch (Exception e) { LOG.warn("Unable to serialize WMContext to json.", e); } }
public SerializationConfig getSerializationConfig() { return mapper.getSerializationConfig(); }
public static ObjectMapper getMapper() { if(mapper == null) { mapper = new ObjectMapper(); SerializationConfig config = mapper .getSerializationConfig() .withSerializationInclusion(Inclusion.NON_EMPTY) .without(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS); mapper.setSerializationConfig(config); } return mapper; } }
/** * Write the actual JSON content to the stream. * @param stream the output stream to use * @param value the value to be rendered, as returned from {@link #filterModel} * @param jsonPrefix the prefix for this view's JSON output * (as indicated through {@link #setJsonPrefix}/{@link #setPrefixJson}) * @throws IOException if writing failed */ protected void writeContent(OutputStream stream, Object value, String jsonPrefix) throws IOException { JsonGenerator generator = this.objectMapper.getJsonFactory().createJsonGenerator(stream, this.encoding); // A workaround for JsonGenerators not applying serialization features // https://github.com/FasterXML/jackson-databind/issues/12 if (this.objectMapper.getSerializationConfig().isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) { generator.useDefaultPrettyPrinter(); } if (jsonPrefix != null) { generator.writeRaw(jsonPrefix); } this.objectMapper.writeValue(generator, value); }
@Override protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType()); JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding); // A workaround for JsonGenerators not applying serialization features // https://github.com/FasterXML/jackson-databind/issues/12 if (this.objectMapper.getSerializationConfig().isEnabled(SerializationConfig.Feature.INDENT_OUTPUT)) { jsonGenerator.useDefaultPrettyPrinter(); } try { if (this.jsonPrefix != null) { jsonGenerator.writeRaw(this.jsonPrefix); } this.objectMapper.writeValue(jsonGenerator, object); } catch (JsonProcessingException ex) { throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex); } }
public JacksonJsonProvider create(Iterable<? extends Module> modules) { ObjectMapper mapper = new ObjectMapper(); /* This is what MapperConfigurator would do to a default ObjectMapper */ AnnotationIntrospector intr = AnnotationIntrospector.pair(new JacksonAnnotationIntrospector(), new JaxbAnnotationIntrospector()); mapper.setDeserializationConfig(mapper.getDeserializationConfig().withAnnotationIntrospector(intr)); mapper.setSerializationConfig(mapper.getSerializationConfig().withAnnotationIntrospector(intr)); /* In the absence of a specific annotation for @JsonSerialize(include), ignore null fields when serializing */ mapper.setSerializationInclusion(Inclusion.NON_NULL); for (Module module : modules) { mapper.registerModule(module); } mapper.registerModule(new GuavaIterableCapableModule()); JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider(mapper, JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS); // Make sure we only rely on annotations for de-/serialization provider.configure(SerializationConfig.Feature.AUTO_DETECT_GETTERS, false); provider.configure(SerializationConfig.Feature.AUTO_DETECT_FIELDS, false); provider.configure(DeserializationConfig.Feature.AUTO_DETECT_SETTERS, false); provider.configure(DeserializationConfig.Feature.AUTO_DETECT_FIELDS, false); return provider; } }
@Override public ObjectMapper getObject() throws Exception { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); return objectMapper; }
protected static String toJson(Object object) throws IOException { SerializationConfig serializationConfig = OBJECT_MAPPER.getSerializationConfig(); serializationConfig.set(SerializationConfig.Feature.INDENT_OUTPUT, true); StringWriter sw = new StringWriter(); OBJECT_MAPPER.writeValue(sw, object); sw.append('\n'); return sw.toString(); }
/** * Utility function good for testing things. Prints out the json tree of the * JmxProcess. */ public static void printJson(JmxProcess process) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.getSerializationConfig().set(Feature.WRITE_NULL_MAP_VALUES, false); System.out.println(mapper.writeValueAsString(process)); }
private JsonFactory createJsonFactory() { JsonFactory jsonFactory = new JsonFactory(); ObjectMapper mapper = new ObjectMapper(jsonFactory); mapper.getSerializationConfig().setAnnotationIntrospector(new JacksonAnnotationIntrospector()); mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); jsonFactory.setCodec(mapper); return jsonFactory; }
private String toJacksonJson(ConnectorResponseModel connectorResponse) throws IOException { final ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL); objectMapper.setVisibilityChecker( objectMapper.getSerializationConfig().getDefaultVisibilityChecker(). withFieldVisibility(JsonAutoDetect.Visibility.NON_PRIVATE)); return objectMapper.writeValueAsString(connectorResponse); }
private String serialize(Object obj) throws IOException { ObjectMapper mapper = new ObjectMapper() .enableDefaultTyping(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE); mapper.getSerializationConfig().addMixInAnnotations(ExtMap.class, JsonExtMapMixIn.class); return mapper.writeValueAsString(obj); }
private static ObjectMapper createObjectMapper() { ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector primaryIntrospector = new JacksonAnnotationIntrospector(); AnnotationIntrospector secondaryIntrospector = new JaxbAnnotationIntrospector(); AnnotationIntrospector introspector = new AnnotationIntrospector.Pair(primaryIntrospector, secondaryIntrospector); mapper.getDeserializationConfig().withAnnotationIntrospector(introspector); mapper.getSerializationConfig().withAnnotationIntrospector(introspector); return mapper; }
public static ObjectMapper createObjectMapperWithIntroSpectors() { ObjectMapper mapper = new ObjectMapper(); AnnotationIntrospector primaryIntrospector = new JacksonAnnotationIntrospector(); AnnotationIntrospector secondaryIntrospector = new JaxbAnnotationIntrospector(); AnnotationIntrospector introspector = new AnnotationIntrospector.Pair(primaryIntrospector, secondaryIntrospector); mapper.getDeserializationConfig().withAnnotationIntrospector(introspector); mapper.getSerializationConfig().withAnnotationIntrospector(introspector); return mapper; }
public static ObjectMapper createDefaultObjectMapper() { final ObjectMapper mapper = new ObjectMapper(); final AnnotationIntrospector introspectorPair = AnnotationIntrospector.pair( new JacksonAnnotationIntrospector(), new JaxbAnnotationIntrospector()); mapper.setDeserializationConfig(mapper.getDeserializationConfig().withAnnotationIntrospector(introspectorPair)); mapper.setSerializationConfig(mapper.getSerializationConfig().withAnnotationIntrospector(introspectorPair)); return mapper; } }
public OpenStackObjectMapperProvider() { objectMapper = new ObjectMapper(); objectMapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true); objectMapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true); objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); AnnotationIntrospector introspector = new JacksonAnnotationIntrospector(); objectMapper.setDeserializationConfig(objectMapper.getDeserializationConfig().withAnnotationIntrospector(introspector)); objectMapper.setSerializationConfig(objectMapper.getSerializationConfig().withSerializationInclusion(Inclusion.NON_NULL) .withAnnotationIntrospector(introspector)); }