@SuppressWarnings("unchecked") private <T> void addKeySerializer(SimpleModule module, Class<?> type, JsonSerializer<?> keySerializer) { module.addKeySerializer((Class<? extends T>) type, (JsonSerializer<T>) keySerializer); } }
@SuppressWarnings("unchecked") private <T> void addKeySerializer(SimpleModule module, Class<?> type, JsonSerializer<?> keySerializer) { module.addKeySerializer((Class<? extends T>) type, (JsonSerializer<T>) keySerializer); } }
@SuppressWarnings("unchecked") private <T> void addKeySerializer(SimpleModule module, Class<?> type, JsonSerializer<?> keySerializer) { module.addKeySerializer((Class<? extends T>) type, (JsonSerializer<T>) keySerializer); } }
@SuppressWarnings("unchecked") private <T> void addKeySerializer(SimpleModule module, Class<?> type, JsonSerializer<?> keySerializer) { module.addKeySerializer((Class<? extends T>) type, (JsonSerializer<T>) keySerializer); } }
ObjectMapper mapper = new ObjectMapper(); SimpleModule simpleModule = new SimpleModule(); simpleModule.addKeySerializer(Class.class, new ClassNameSerializer()); mapper.registerModule(simpleModule); ... mapper.writeValueAsString(treeConfigMap)
String json = "{\"SomeKey\":\"SomeValue\",\"AnotherKey\":\"another value\",\"InnerJson\":" + "{\"TheKey\":\"TheValue\"}}"; SimpleModule simpleModule = new SimpleModule(); simpleModule.addKeySerializer(String.class, new CamelCaseKeySerializer()); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(simpleModule); Map<String, Object> map = mapper.readValue(json, new TypeReference<Map<String, Object>>() {}); String camelCaseJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(map);
final ObjectMapper mapper = new ObjectMapper(); final SimpleModule module = new SimpleModule("myModule", Version.unknownVersion()); module.addKeySerializer(CustomType1.class, new CustomType1Serializer()); mapper.registerModule(module); final MapType type = mapper.getTypeFactory().constructMapType( Map.class, CustomType1.class, CustomType2.class); final Map<CustomType1, CustomType2> map = new HashMap<CustomType1, CustomType2>(4); final ObjectWriter writer = mapper.writerWithType(type); final String json = writer.writeValueAsString(map);
private static ObjectMapper createObjectMapper() { SimpleModule module = new SimpleModule() .addKeySerializer(NodeResolution.class, new NodeResolutionSerializer()) .addDeserializer(NodeResolution.class, new NodeResolutionDeserializer()); return new ObjectMapper() .registerModule(module) .setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE) .setSerializationInclusion(Include.NON_EMPTY) .setVisibility(PropertyAccessor.FIELD, Visibility.ANY); }
public class JacksonQName { public static class QnameSerializer extends JsonSerializer<QName> { @Override public void serialize(QName value, JsonGenerator jgen, SerializerProvider provider) throws IOException { System.out.println("QName serializer works"); jgen.writeFieldName(value.getLocalPart()); } } public static void main(String[] args) throws JsonProcessingException { SimpleModule module = new SimpleModule(); module.addKeySerializer(QName.class, new QnameSerializer()); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(module); Map<QName, String> m = ImmutableMap.of( new QName("http://google.com/", "myName", "pre"), "myValue"); TypeReference<Map<QName, String>> type = new TypeReference<Map<QName, String>>() {}; System.out.println(mapper.writerWithType(type).writeValueAsString(m)); } }
public static SimpleModule getModule() { final SimpleModule module = new SimpleModule(); module.addSerializer(Class.class, (JsonSerializer) new SimpleClassSerializer()); module.addDeserializer(Class.class, new SimpleClassDeserializer()); module.addKeySerializer(Class.class, (JsonSerializer) new SimpleClassKeySerializer()); module.addKeyDeserializer(Class.class, new SimpleClassKeyDeserializer()); return module; }
@Inject public ObjectMapperFactory(MailboxId.Factory mailboxIdFactory, MessageId.Factory messageIdFactory) { SimpleModule mailboxIdModule = new SimpleModule(); mailboxIdModule.addDeserializer(MailboxId.class, new MailboxIdDeserializer(mailboxIdFactory)); mailboxIdModule.addSerializer(MailboxId.class, new MailboxIdSerializer()); mailboxIdModule.addKeyDeserializer(MailboxId.class, new MailboxIdKeyDeserializer(mailboxIdFactory)); mailboxIdModule.addKeySerializer(MailboxId.class, new MailboxIdKeySerializer()); mailboxIdModule.addDeserializer(MessageId.class, new MessageIdDeserializer(messageIdFactory)); mailboxIdModule.addSerializer(MessageId.class, new MessageIdSerializer()); mailboxIdModule.addKeyDeserializer(MessageId.class, new MessageIdKeyDeserializer(messageIdFactory)); mailboxIdModule.addKeySerializer(MessageId.class, new MessageIdKeySerializer()); mailboxIdModule.addKeyDeserializer(Rights.Username.class, new UsernameKeyDeserializer()); mailboxIdModule.addDeserializer(Rights.Right.class, new RightDeserializer()); SimpleModule mdnModule = new SimpleModule(); mailboxIdModule.addDeserializer(DispositionActionMode.class, new MDNActionModeDeserializer()); mailboxIdModule.addDeserializer(DispositionSendingMode.class, new MDNSendingModeDeserializer()); mailboxIdModule.addDeserializer(DispositionType.class, new MDNTypeDeserializer()); mailboxIdModule.setMixInAnnotation(Role.class, RoleMixIn.class); jacksonModules = JACKSON_BASE_MODULES.add(mailboxIdModule) .add(mdnModule) .build(); }
public static ObjectMapper configureObjectMapper(ObjectMapper objectMapper, MessageId.Factory messageIdFactory) { SimpleModule module = new SimpleModule(); module.addDeserializer(MessageUid.class, new MessageUidDeserializer()); module.addKeyDeserializer(MessageUid.class, new MessageUidKeyDeserializer()); module.addSerializer(MessageUid.class, new MessageUidSerializer()); module.addKeySerializer(MessageUid.class, new MessageUidKeySerializer()); module.addSerializer(MessageId.class, new MessageIdSerializer()); module.addDeserializer(MessageId.class, new MessageIdDeserializer(messageIdFactory)); module.addSerializer(QuotaRoot.class, new QuotaRootSerializer()); module.addDeserializer(QuotaRoot.class, new QuotaRootDeserializer()); module.addSerializer(QuotaCount.class, new QuotaCountSerializer()); module.addDeserializer(QuotaCount.class, new QuotaCountDeserializer()); module.addSerializer(QuotaSize.class, new QuotaSizeSerializer()); module.addDeserializer(QuotaSize.class, new QuotaSizeDeserializer()); objectMapper.registerModules(module, new Jdk8Module()); return objectMapper; }