/*
* Copyright 2012 Harlan Noonkester
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.couchace.jackson;
import com.couchace.core.api.injectable.MissingInjectableResponse;
import com.couchace.jackson.internal.CouchJacksonModel;
import com.couchace.jackson.internal.CouchJacksonSerializerIntrospector;
import com.couchace.jackson.internal.CustomJacksonInjectableValues;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
/**
* User: harlan
* Date: 7/21/12
* Time: 11:15 PM
*/
public class DefaultObjectMapper extends ObjectMapper {
public DefaultObjectMapper(ObjectMapper objectMapper) {
super(objectMapper);
init(new CustomJacksonInjectableValues(MissingInjectableResponse.RETURN_NULL));
}
public DefaultObjectMapper(Module... modules) {
init(new CustomJacksonInjectableValues(MissingInjectableResponse.RETURN_NULL), modules);
}
public DefaultObjectMapper(InjectableValues injectableValues, Module... modules) {
init(injectableValues, modules);
}
protected void init(InjectableValues injectableValues, Module... modules) {
// Stops LocalDateTime from being rendered as array.
configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
// Pretty printing
configure(SerializationFeature.INDENT_OUTPUT, true);
// Fail on unknown
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
// Register CouchJacksonModule
registerModule(new CouchJacksonModel());
// Register additional modules.
if (modules != null) {
for (Module module : modules) {
registerModule(module);
}
}
// Configure CustomInjectableValues that will not fail on missing injectable values.
setInjectableValues(injectableValues);
// Annotation introspect used to exclude revisions and attachments
setAnnotationIntrospectors(new CouchJacksonSerializerIntrospector(), new JacksonAnnotationIntrospector());
}
public void noErrorOnUnknownFilter() {
// Configure an empty FilterProvider so it will not complain when a filter is not found (such as Couch).
this.setFilters(new SimpleFilterProvider().setFailOnUnknownId(false));
}
@Override
public ObjectMapper copy() {
_checkInvalidCopy(DefaultObjectMapper.class);
return new DefaultObjectMapper(this);
}
}