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

com.github.dreamhead.moco.util.Jsons Maven / Gradle / Ivy

Go to download

Moco is an easy setup stub framework, mainly focusing on testing and integration.

There is a newer version: 1.5.0
Show newest version
package com.github.dreamhead.moco.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import com.github.dreamhead.moco.MocoException;
import com.google.common.collect.ImmutableList;
import com.google.common.io.CharStreams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;

import static com.google.common.collect.ImmutableList.of;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.lang.String.format;

public final class Jsons {
    private static final Logger logger = LoggerFactory.getLogger(Jsons.class);

    private static final TypeFactory DEFAULT_FACTORY = TypeFactory.defaultInstance();
    private static final ObjectMapper DEFAULT_MAPPER = new ObjectMapper();

    public static String toJson(final Object value) {
        try {
            return DEFAULT_MAPPER.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            throw new MocoException(e);
        }
    }

    public static String toJson(final Map map) {
        try {
            return DEFAULT_MAPPER.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            throw new MocoException(e);
        }
    }

    public static  T toObject(final String value, final Class clazz) {
        try {
            return DEFAULT_MAPPER.readValue(value, clazz);
        } catch (IOException e) {
            throw new MocoException(e);
        }
    }

    public static  T toObject(final InputStream value, final Class clazz) {
        try {
            return DEFAULT_MAPPER.readValue(value, clazz);
        } catch (IOException e) {
            throw new MocoException(e);
        }
    }

    public static  T toObject(final Reader value, final Class clazz) {
        try {
            return DEFAULT_MAPPER.readValue(value, clazz);
        } catch (IOException e) {
            throw new MocoException(e);
        }
    }

    public static  ImmutableList toObjects(final String value, final Class elementClass) {
        return toObjects(new ByteArrayInputStream(value.getBytes()), elementClass);
    }

    public static  ImmutableList toObjects(final InputStream stream, final Class elementClass) {
        return toObjects(of(stream), elementClass);
    }

    public static  ImmutableList toObjects(final ImmutableList streams,
                                                 final Class elementClass) {
        final CollectionType type = DEFAULT_FACTORY.constructCollectionType(List.class, elementClass);
        return streams.stream()
                .flatMap(Jsons.toObject(type))
                .collect(toImmutableList());
    }

    private static  Function> toObject(final CollectionType type) {
        return input -> {
            try (InputStream actual = input) {
                String text = CharStreams.toString(new InputStreamReader(actual));
                return DEFAULT_MAPPER.>readValue(text, type).stream();
            } catch (UnrecognizedPropertyException e) {
                logger.info("Unrecognized field: {}", e.getMessage());
                throw new MocoException(format("Unrecognized field [ %s ], please check!", e.getPropertyName()));
            } catch (JsonMappingException e) {
                logger.info("{} {}", e.getMessage(), e.getPathReference());
                throw new MocoException(e);
            } catch (IOException e) {
                throw new MocoException(e);
            }
        };
    }

    public static void writeToFile(final File file, final Object value) {
        ObjectWriter writer = DEFAULT_MAPPER.writerWithDefaultPrettyPrinter();
        try {
            writer.writeValue(file, value);
        } catch (IOException e) {
            throw new MocoException(e);
        }
    }

    public static void writeToFile(final Path file, final Object value) {
        writeToFile(file.toFile(), value);
    }

    private Jsons() {
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy