Skip to content

Commit

Permalink
Configure Jackson ObjectMapper once
Browse files Browse the repository at this point in the history
This changes the initialization on demand holder for
ObjectMapper to configure the mapper once. Previously
the mapper was being created once but reconfigured on
each access.

Fix for #76
  • Loading branch information
dehora committed Nov 18, 2015
1 parent 445056d commit b63ed22
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions intercom-java/src/main/java/io/intercom/api/MapperSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,32 @@
public class MapperSupport {

public static ObjectMapper objectMapper() {
final ObjectMapper om = Holder.INSTANCE;
configure(om);
return om;
return Holder.INSTANCE;
}

private static void configure(ObjectMapper objectMapper) {
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
objectMapper.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
objectMapper.registerModule(customAttributeModule());
}
private static class Holder {
private static final ObjectMapper INSTANCE = new Holder().configured(new ObjectMapper());

private static SimpleModule customAttributeModule() {
final SimpleModule customAttributeModule = new SimpleModule(
"IntercomClientModule",
new Version(1, 0, 0, null, "", "")
);
customAttributeModule.addDeserializer(CustomAttribute.class, new CustomAttributeDeserializer());
customAttributeModule.addSerializer(CustomAttribute.class, new CustomAttributeSerializer());
customAttributeModule.addDeserializer(Subscription.Topic.class, new TopicDeserializer());
customAttributeModule.addSerializer(Subscription.Topic.class, new TopicSerializer());
customAttributeModule.addSerializer(Counts.CountItem.class, new CountItemSerializer());
customAttributeModule.addDeserializer(Counts.CountItem.class, new CountItemDeserializer());
return customAttributeModule;
}
private ObjectMapper configured(ObjectMapper om) {
om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
om.configure(SerializationFeature.INDENT_OUTPUT, true);
om.configure(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
om.configure(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS, false);
return om.registerModule(customAttributeModule());
}

private static class Holder {
private static final ObjectMapper INSTANCE = new ObjectMapper();
private SimpleModule customAttributeModule() {
final SimpleModule customAttributeModule = new SimpleModule(
"IntercomClientModule",
new Version(1, 0, 0, null, "", "")
);
customAttributeModule.addDeserializer(CustomAttribute.class, new CustomAttributeDeserializer());
customAttributeModule.addSerializer(CustomAttribute.class, new CustomAttributeSerializer());
customAttributeModule.addDeserializer(Subscription.Topic.class, new TopicDeserializer());
customAttributeModule.addSerializer(Subscription.Topic.class, new TopicSerializer());
customAttributeModule.addSerializer(Counts.CountItem.class, new CountItemSerializer());
customAttributeModule.addDeserializer(Counts.CountItem.class, new CountItemDeserializer());
return customAttributeModule;
}
}
}

0 comments on commit b63ed22

Please sign in to comment.