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

com.tectonica.util.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
package com.tectonica.util;

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.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;

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 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 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);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy