public static ObjectMapper getStatelessMapper() { if (statelessMapper == null) { statelessMapper = new ObjectMapper(new YAMLFactory()); statelessMapper.addMixInAnnotations(ObjectMeta.class, ObjectMetaMixIn.class); statelessMapper.addMixInAnnotations(ReplicationController.class, ReplicationControllerMixIn.class); } return statelessMapper; }
mapper.addMixInAnnotations(Property.class, PropertyExampleMixIn.class);
/** * Add an additional mixin for serialization with this object mapper. */ public void addMixin(Class target, Class mixin) { mapper.addMixInAnnotations(target, mixin); }
public static ObjectMapper getObjectMapper() { if (mapper == null) { mapper = new ObjectMapper(); mapper.addMixInAnnotations(CreateContainerMetadata.class, FieldsToIgnoreForDeserializationMixin.class); mapper.addMixInAnnotations(CreateContainerOptions.class, FieldsToIgnoreForDeserializationMixin.class); mapper.addMixInAnnotations(CreateContainerOptions.class, FieldsToIgnoreForSerializationMixin.class); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } return mapper; } }
public StreamDispatcher(Queue<String> queue, List<StreamListener> listeners) { this.queue = queue; this.listeners = listeners; pool = Executors.newCachedThreadPool(); objectMapper = new ObjectMapper(); objectMapper.addMixInAnnotations(Tweet.class, TweetMixin.class); objectMapper.addMixInAnnotations(StreamDeleteEvent.class, StreamDeleteEventMixin.class); objectMapper.addMixInAnnotations(StreamWarningEvent.class, StreamWarningEventMixin.class); active = new AtomicBoolean(true); }
@Configuration public class JacksonConfig { @Bean public ObjectMapper mapper() { final ObjectMapper mapper = new ObjectMapper(); mapper.addMixInAnnotations(User.class, UserMixIn.class); return mapper; } }
public static ObjectMapper getStatelessMapper() { if (statelessMapper == null) { statelessMapper = new ObjectMapper(new YAMLFactory()); statelessMapper.addMixInAnnotations(ObjectMeta.class, ObjectMetaMixIn.class); statelessMapper.addMixInAnnotations(ReplicationController.class, ReplicationControllerMixIn.class); } return statelessMapper; }
public static String dumpWithoutRuntimeStateAsYaml(HasMetadata obj) throws JsonProcessingException { ObjectMapper statelessMapper = new ObjectMapper(new YAMLFactory()); statelessMapper.addMixInAnnotations(ObjectMeta.class, ObjectMetaMixIn.class); statelessMapper.addMixInAnnotations(ReplicationController.class, StatelessReplicationControllerMixIn.class); return statelessMapper.writeValueAsString(obj); }
abstract class Mixin<K, V> { public Mixin(@JsonProperty("key") K key, @JsonProperty("value") V value) {} } ... public static <T> T fromJSON(String content, Class<T> clazz) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.addMixInAnnotations(SimpleEntry.class, Mixin.class); return mapper.readValue(content, clazz); }
String json = "{\"bar\":\"Value\"}"; System.out.println(json); ObjectMapper deserializeMapper = new ObjectMapper(); deserializeMapper.addMixInAnnotations(Foo.class, FooMixIn.class); System.out.println(deserializeMapper.readValue(json, Foo.class));
String json = "{\"bar\":\"Value\"}"; ObjectMapper deserializerMapper = new ObjectMapper(); Foo foo = deserializerMapper.readValue(json, Foo.class); System.out.println("Foo object: " + foo); ObjectMapper serializerMapper = new ObjectMapper(); serializerMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); serializerMapper.addMixInAnnotations(Foo.class, FooMixIn.class); System.out.println("JSON: " + serializerMapper.writeValueAsString(foo));
private void addMixInAnnotations(ObjectMapper mapper, String className, Class<?> annotationClass) { try { mapper.addMixInAnnotations(loadClass(className), annotationClass); } catch (ClassNotFoundException e) { log.warn("Class '{}' hasn't been found!", className, e); } }
PojoA pojoA = new PojoA(); pojoA.setB("B value"); System.out.println("Without MixIn:"); ObjectMapper mapper = new ObjectMapper(); System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(pojoA)); System.out.println("With MixIn:"); ObjectMapper mapperWithMixIn = new ObjectMapper(); mapperWithMixIn.addMixInAnnotations(PojoA.class, PojoAMixIn.class); System.out.println(mapperWithMixIn.writerWithDefaultPrettyPrinter().writeValueAsString(pojoA));
public static void main(String[] args) throws IOException { ObjectMapper om = new ObjectMapper(); List<Common> list = new JacksonList<Common>(); list.add(new A()); list.add(new B()); om.addMixInAnnotations(A.class, AMixIn.class); om.addMixInAnnotations(B.class, BMixIn.class); System.out.println(om.writeValueAsString(list)); System.out.println(om.writeValueAsString(new A())); }
private ObjectMapper getJsonMapper() { ObjectMapper mapper = ObjectMapperFactory.createJson(); mapper.configure(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS, false); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); if (options.isJsonExampleValues()) { mapper.addMixInAnnotations(Schema.class, PropertyExampleMixIn.class); } return mapper; }
Data data = new Data(); data.setName("Tom"); Root root = new Root(); root.setData(data); ObjectMapper mapperWithMixIn = new ObjectMapper(); mapperWithMixIn.addMixInAnnotations(Root.class, RootMixIn.class); ObjectMapper mapperDefault = new ObjectMapper(); System.out.println("With MIX-IN"); System.out.println(mapperWithMixIn.writeValueAsString(root)); System.out.println("Default"); System.out.println(mapperDefault.writeValueAsString(root));
public static ObjectMapper patchMapper() { if (patchMapper == null) { patchMapper = new ObjectMapper(); patchMapper.addMixInAnnotations(ObjectMeta.class, ObjectMetaMixIn.class); patchMapper.addMixInAnnotations(Build.class, BuildMixIn.class); patchMapper.setConfig(patchMapper().getSerializationConfig().without(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)); } return patchMapper; }
@Override public void build(Writer writer) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(Include.NON_NULL); mapper.addMixInAnnotations(Object.class, DynamicPropertyFilterMixin.class); FilterProvider filterProvider = new SimpleFilterProvider() .addFilter("dynamicPropertyFilter", SimpleBeanPropertyFilter.serializeAllExcept(getExcludedProperties())); mapper.writer(filterProvider).writeValue(writer, getRootObject()); }
public static void main(String[] args) throws JsonProcessingException { Person p = new Person("Foo","Bar"); p.setAddress("This address is too long"); p.setAge(20); ObjectMapper xmlMapper = new XmlMapper(); xmlMapper.addMixInAnnotations(Person.class, MixIn.class); String xml = xmlMapper.writeValueAsString(p); System.out.println(xml); }
/** * Configure ObjectMapper to filter out fields causing serialization problems */ public SyndEntryJsonTransformer() { mapper = new ObjectMapper(); mapper.addMixInAnnotations(SyndEntry.class,this.getClass()); FilterProvider filterProvider = new SimpleFilterProvider() .addFilter("foreignMarkup filter", SimpleBeanPropertyFilter.serializeAllExcept("foreignMarkup")); mapper.setFilters(filterProvider); }