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

fr.pilato.elasticsearch.crawler.fs.client.JsonUtil Maven / Gradle / Ivy

There is a newer version: 2.5
Show newest version
/*
 * Licensed to David Pilato (the "Author") under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Author licenses this
 * file to you 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 fr.pilato.elasticsearch.crawler.fs.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import fr.pilato.elasticsearch.crawler.fs.meta.MetaParser;
import org.elasticsearch.client.Response;

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

public class JsonUtil {

    public static String serialize(Object object) {
        try {
            return MetaParser.mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static  T deserialize(Response response, Class clazz) {
        try {
            if (response.getEntity() == null) {
                return null;
            }
            return deserialize(response.getEntity().getContent(), clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static  T deserialize(InputStream stream, Class clazz) {
        try {
            return MetaParser.mapper.readValue(stream, clazz);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map asMap(Response response) {
        try {
            if (response.getEntity() == null) {
                return null;
            }
            return asMap(response.getEntity().getContent());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Map asMap(InputStream stream) {
        try {
            return MetaParser.mapper.readValue(stream, new TypeReference>(){});
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    public static Map extractFromPath(Map json, String... path) {
        Map currentObject = json;
        for (String fieldName : path) {
            Object jObject = currentObject.get(fieldName);
            if (jObject == null) {
                throw new RuntimeException("incorrect Json. Was expecting field " + fieldName);
            }
            if (!(jObject instanceof Map)) {
                throw new RuntimeException("incorrect datatype in json. Expected Map and got " + jObject.getClass().getName());
            }
            currentObject = (Map) jObject;
        }
        return currentObject;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy