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

no.ssb.lds.api.persistence.json.JsonTools Maven / Gradle / Ivy

There is a newer version: 0.13
Show newest version
package no.ssb.lds.api.persistence.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class JsonTools {

    public static final ObjectMapper mapper = new ObjectMapper();

    public static Map toMap(JsonNode node) {
        return mapper.convertValue(node, new TypeReference>() {
        });
    }

    public static List toList(JsonNode node) {
        return mapper.convertValue(node, new TypeReference() {
        });
    }

    public static JsonNode toJsonNode(Map document) {
        return mapper.convertValue(document, JsonNode.class);
    }

    public static JsonNode toJsonNode(List document) {
        return mapper.convertValue(document, JsonNode.class);
    }

    public static JsonNode toJsonNode(String json) {
        try {
            return mapper.readTree(json);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static String toJson(JsonNode node) {
        try {
            return mapper.writeValueAsString(node);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String toPrettyJson(JsonNode node) {
        try {
            return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy