All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.deephacks.westty.tests.JsonUtil Maven / Gradle / Ivy

The newest version!
package org.deephacks.westty.tests;


import org.codehaus.jackson.annotate.JsonAutoDetect;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.introspect.VisibilityChecker;
import org.codehaus.jackson.map.type.CollectionType;

import java.util.List;

public class JsonUtil {

    private static final ObjectMapper mapper = new ObjectMapper();
    static {
        mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setVisibilityChecker(VisibilityChecker.Std.defaultInstance().withFieldVisibility(
                JsonAutoDetect.Visibility.ANY));
    }

    public static List getPorts(String json){
        try {
            CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, Integer.class);
            return mapper.readValue(json, type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toJson(JsonEntity object){
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static JsonEntity fromJson(String json){
        try {
            return mapper.readValue(json, JsonEntity.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String toJsonList(List list){
        try {
            return mapper.writeValueAsString(list);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static List fromJsonList(String json){
        try {
            CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, JsonEntity.class);
            return mapper.readValue(json, type);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy