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

com.tectonica.thirdparty.Jackson2 Maven / Gradle / Ivy

Go to download

Set of Java utility classes, all completely independent, to provide lightweight solutions for common situations

There is a newer version: 0.6.1
Show newest version
/*
 * Copyright (C) 2014 Zach Melamed
 * 
 * Latest version available online at https://github.com/zach-m/tectonica-commons
 *
 * 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.tectonica.thirdparty;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
 * Convenience wrapper for Jackson APIs version 2.x. Requires:
 * 
 * 
 * <dependency>
 *     <groupId>com.fasterxml.jackson.core</groupId>
 *     <artifactId>jackson-databind</artifactId>
 * </dependency>
 * 
* * @author Zach Melamed */ public class Jackson2 { private static final ObjectMapper treeMapper = new ObjectMapper(); public static JsonNode parse(String jsonObject) { try { return treeMapper.readValue(jsonObject, JsonNode.class); } catch (Exception e) { throw new RuntimeException(e); } } public static ArrayNode parseArray(String jsonArray) { try { return treeMapper.readValue(jsonArray, ArrayNode.class); } catch (Exception e) { throw new RuntimeException(e); } } // /////////////////////////////////////////////////////////////////// public static String escape(String text) { return new String(JsonStringEncoder.getInstance().quoteAsUTF8(text)); } // /////////////////////////////////////////////////////////////////// private static final ObjectMapper fieldsMapper = createFieldsMapper(); private static final ObjectMapper propsMapper = createPropsMapper(); public static ObjectMapper createFieldsMapper() { ObjectMapper mapper = new ObjectMapper(); // limit to fields only mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); mapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); mapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); // general configuration mapper.setSerializationInclusion(Include.NON_NULL); mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); return mapper; } public static ObjectMapper createPropsMapper() { ObjectMapper mapper = new ObjectMapper(); // limit to props only mapper.setVisibility(PropertyAccessor.FIELD, Visibility.NONE); mapper.setVisibility(PropertyAccessor.GETTER, Visibility.ANY); mapper.setVisibility(PropertyAccessor.SETTER, Visibility.ANY); // general configuration mapper.setSerializationInclusion(Include.NON_NULL); mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); return mapper; } // /////////////////////////////////////////////////////////////////// public static String toJson(Object o, ObjectMapper mapper) { try { return mapper.writeValueAsString(o); } catch (IOException e) { throw new RuntimeException(e); } } public static void toJson(OutputStream os, Object o, ObjectMapper mapper) { try { mapper.writeValue(os, o); } catch (IOException e) { throw new RuntimeException(e); } } public static void toJson(Writer w, Object o, ObjectMapper mapper) { try { mapper.writeValue(w, o); } catch (IOException e) { throw new RuntimeException(e); } } public static T fromJson(String jsonStr, Class clz, ObjectMapper mapper) { try { return mapper.readValue(jsonStr, clz); } catch (IOException e) { throw new RuntimeException(e); } } public static T fromJson(InputStream is, Class clz, ObjectMapper mapper) { try { return mapper.readValue(is, clz); } catch (IOException e) { throw new RuntimeException(e); } } public static T fromJson(Reader r, Class clz, ObjectMapper mapper) { try { return mapper.readValue(r, clz); } catch (IOException e) { throw new RuntimeException(e); } } public static T fromJson(String jsonStr, Class clz, Class

paramClz, ObjectMapper mapper) { try { JavaType jt = mapper.getTypeFactory().constructParametrizedType(clz, clz, paramClz); return mapper.readValue(jsonStr, jt); } catch (IOException e) { throw new RuntimeException(e); } } public static T fromJson(InputStream is, Class clz, Class

paramClz, ObjectMapper mapper) { try { JavaType jt = mapper.getTypeFactory().constructParametrizedType(clz, clz, paramClz); return mapper.readValue(is, jt); } catch (IOException e) { throw new RuntimeException(e); } } public static T fromJson(Reader r, Class clz, Class

paramClz, ObjectMapper mapper) { try { JavaType jt = mapper.getTypeFactory().constructParametrizedType(clz, clz, paramClz); return mapper.readValue(r, jt); } catch (IOException e) { throw new RuntimeException(e); } } // /////////////////////////////////////////////////////////////////// public static String propsToJson(Object o) { return toJson(o, propsMapper); } public static void propsToJson(OutputStream os, Object o) { toJson(os, o, propsMapper); } public static void propsToJson(Writer w, Object o) { toJson(w, o, propsMapper); } public static T propsFromJson(String jsonStr, Class clz) { return fromJson(jsonStr, clz, propsMapper); } public static T propsFromJson(InputStream is, Class clz) { return fromJson(is, clz, propsMapper); } public static T propsFromJson(Reader r, Class clz) { return fromJson(r, clz, propsMapper); } public static T propsFromJson(String jsonStr, Class clz, Class

paramClz) { return fromJson(jsonStr, clz, paramClz, propsMapper); } public static T propsFromJson(InputStream is, Class clz, Class

paramClz) { return fromJson(is, clz, paramClz, propsMapper); } public static T propsFromJson(Reader r, Class clz, Class

paramClz) { return fromJson(r, clz, paramClz, propsMapper); } public static String fieldsToJson(Object o) { return toJson(o, fieldsMapper); } public static void fieldsToJson(OutputStream os, Object o) { toJson(os, o, fieldsMapper); } public static void fieldsToJson(Writer w, Object o) { toJson(w, o, fieldsMapper); } public static T fieldsFromJson(String jsonStr, Class clz) { return fromJson(jsonStr, clz, fieldsMapper); } public static T fieldsFromJson(InputStream is, Class clz) { return fromJson(is, clz, fieldsMapper); } public static T fieldsFromJson(Reader r, Class clz) { return fromJson(r, clz, fieldsMapper); } public static T fieldsFromJson(String jsonStr, Class clz, Class

paramClz) { return fromJson(jsonStr, clz, paramClz, fieldsMapper); } public static T fieldsFromJson(InputStream is, Class clz, Class

paramClz) { return fromJson(is, clz, paramClz, fieldsMapper); } public static T fieldsFromJson(Reader r, Class clz, Class

paramClz) { return fromJson(r, clz, paramClz, fieldsMapper); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy