com.infinities.keystone4j.utils.JsonUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.infinities.keystone4j.utils.JsonUtils.java

Source

/*******************************************************************************
 * # Copyright 2015 InfinitiesSoft Solutions Inc.
 * #
 * # 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.infinities.keystone4j.utils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair;
import com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module;
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;

public class JsonUtils {

    private static ObjectMapper objectMapper = new ObjectMapper();
    private final static TypeReference<LinkedHashMap<String, String>> mapTypeRef = new TypeReference<LinkedHashMap<String, String>>() {
    };

    static {
        AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
        // if using BOTH JAXB annotations AND Jackson annotations:
        AnnotationIntrospector secondary = new JacksonAnnotationIntrospector();
        objectMapper = objectMapper
                .setAnnotationIntrospector(new AnnotationIntrospectorPair(introspector, secondary));
        objectMapper = objectMapper.setSerializationInclusion(Include.NON_NULL);
        // make deserializer use JAXB annotations (only)
        objectMapper = objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        // objectMapper =
        // objectMapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
        // objectMapper =
        // objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS,
        // false);
        objectMapper.registerModule(new Hibernate4Module());
    }

    public static String toJsonWithoutPrettyPrint(Object object)
            throws JsonGenerationException, JsonMappingException, IOException {
        try {
            objectMapper = objectMapper.disable(SerializationFeature.INDENT_OUTPUT);
            return objectMapper.writeValueAsString(object);
        } finally {
            objectMapper = objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        }
    }

    public static String toJson(Object object) throws JsonGenerationException, JsonMappingException, IOException {
        return objectMapper.writeValueAsString(object);
    }

    public static String toJson(Object object, Class<?> view)
            throws JsonGenerationException, JsonMappingException, IOException {
        return objectMapper.writerWithView(view).writeValueAsString(object);
    }

    public static Map<String, String> readJson(URL url)
            throws JsonParseException, JsonMappingException, IOException {
        Map<String, String> map = objectMapper.readValue(url, mapTypeRef);
        return map;
    }

    public static <T> T readJson(String fromValue, TypeReference<T> toValueType) throws IOException {
        return objectMapper.readValue(fromValue, toValueType);
    }

    public static JsonNode convertToJsonNode(String text) throws JsonProcessingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = mapper.getFactory(); // since 2.1 use
        // mapper.getFactory()
        // instead
        JsonParser jp = factory.createParser(text);
        JsonNode node = mapper.readTree(jp);
        return node;
    }

    public static <T> T readFile(String fileName, Class<T> valueType)
            throws JsonParseException, JsonMappingException, IOException {
        return objectMapper.readValue(new File(fileName), valueType);
    }

    public static <T> T convertValue(Object fromValue, Class<T> toValueType)
            throws JsonParseException, JsonMappingException, IOException {
        return objectMapper.convertValue(fromValue, toValueType);
    }

}