com.fitbur.docker.client.internal.ObjectMapperProvider.java Source code

Java tutorial

Introduction

Here is the source code for com.fitbur.docker.client.internal.ObjectMapperProvider.java

Source

/*
 * Copyright 2014 Fitbur.
 *
 * 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.fitbur.docker.client.internal;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.PerLookup;
import org.jvnet.hk2.annotations.Service;

/**
 *
 * @author Sharmarke Aden
 */
@Service
public class ObjectMapperProvider implements Factory<ObjectMapper> {

    @PerLookup
    @Override
    public ObjectMapper provide() {
        ObjectMapper mapper = new ObjectMapper();

        mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY)
                .setSerializationInclusion(JsonInclude.Include.NON_NULL);

        mapper.enable(MapperFeature.USE_ANNOTATIONS).enable(MapperFeature.AUTO_DETECT_GETTERS)
                .enable(MapperFeature.AUTO_DETECT_SETTERS).enable(MapperFeature.AUTO_DETECT_IS_GETTERS)
                .enable(MapperFeature.AUTO_DETECT_FIELDS).enable(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS);

        mapper.disable(SerializationFeature.INDENT_OUTPUT).enable(SerializationFeature.WRITE_EMPTY_JSON_ARRAYS)
                .disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

        mapper.disable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);

        mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true)
                .configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, true);

        return mapper;
    }

    @Override
    public void dispose(ObjectMapper mapper) {
    }

}