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

de.alpharogroup.gson.JsonStringToObjectExtensions Maven / Gradle / Ivy

Go to download

Useful extensions and utilities for convert java object to json and back with gson

The newest version!
/**
 * The MIT License
 *
 * Copyright (C) 2015 Asterios Raptis
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package de.alpharogroup.gson;

import com.google.gson.Gson;
import de.alpharogroup.gson.factory.GsonFactory;
import de.alpharogroup.gson.factory.TypeFactory;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * The class {@link JsonStringToObjectExtensions} converts json strings to java object and java
 * collections.
 */
public final class JsonStringToObjectExtensions
{

	private JsonStringToObjectExtensions()
	{
	}

	/**
	 * Transforms the given json string into a java map object
	 *
	 * @param         the generic type of keys
	 * @param         the generic type of values
	 * @param jsonString the json string
	 * @param keyType    the class type of the key
	 * @param valueType  the class type of the value
	 * @return the map
	 */
	public static  Map toMapObject(final String jsonString, Class keyType,
		Class valueType)
	{
		Objects.requireNonNull(jsonString);
		Objects.requireNonNull(keyType);
		Objects.requireNonNull(valueType);
		return toMapObject(jsonString, keyType, valueType, GsonFactory.DEFAULT_GSON);
	}

	/**
	 * Transforms the given json string into a java map object
	 *
	 * @param         the generic type of keys
	 * @param         the generic type of values
	 * @param jsonString the json string
	 * @param keyType    the class type of the key
	 * @param valueType  the class type of the value
	 * @param gson       the gson object
	 * @return the map
	 */
	public static  Map toMapObject(final String jsonString, Class keyType,
		Class valueType, final Gson gson)
	{
		Objects.requireNonNull(jsonString);
		Objects.requireNonNull(keyType);
		Objects.requireNonNull(valueType);
		Objects.requireNonNull(gson);
		return gson.fromJson(jsonString, TypeFactory.newMapTypeToken(keyType, valueType));
	}

	/**
	 * Transforms the given json string into a java object.
	 *
	 * @param         the generic type of the return type
	 * @param jsonString the json string
	 * @param clazz      the clazz of the generic type
	 * @return the object
	 */
	public static  T toObject(final String jsonString, final Class clazz)
	{
		Objects.requireNonNull(jsonString);
		Objects.requireNonNull(clazz);
		return toObject(jsonString, clazz, GsonFactory.DEFAULT_GSON);
	}

	/**
	 * Transforms the given json string into a java object.
	 *
	 * @param         the generic type of the return type
	 * @param jsonString the json string
	 * @param clazz      the clazz of the generic type
	 * @param gson       the gson object
	 * @return the object
	 */
	public static  T toObject(final String jsonString, final Class clazz, final Gson gson)
	{
		Objects.requireNonNull(jsonString);
		Objects.requireNonNull(clazz);
		Objects.requireNonNull(gson);
		return gson.fromJson(jsonString, clazz);
	}

	/**
	 * Transforms the given json string into a java object {@link Collection}
	 *
	 * @param              the generic type of the return type
	 * @param jsonString      the json string
	 * @param collectionClass the collection class
	 * @param elementClass    the element class
	 * @return the list with the java objects.
	 */
	public static  Collection toObjectCollection(final String jsonString,
		@SuppressWarnings("rawtypes") final Class collectionClass,
		final Class elementClass)
	{
		Objects.requireNonNull(jsonString);
		Objects.requireNonNull(collectionClass);
		Objects.requireNonNull(elementClass);
		return toObjectCollection(jsonString, collectionClass, elementClass,
			GsonFactory.DEFAULT_GSON);
	}

	/**
	 * Transforms the given json string into a java object {@link Collection}
	 *
	 * @param              the generic type of the return type
	 * @param jsonString      the json string
	 * @param collectionClass the collection class
	 * @param elementClass    the element class
	 * @param gson            the gson object
	 * @return the list with the java objects
	 */
	public static  Collection toObjectCollection(final String jsonString,
		@SuppressWarnings("rawtypes") final Class collectionClass,
		final Class elementClass, final Gson gson)
	{
		Objects.requireNonNull(jsonString);
		Objects.requireNonNull(collectionClass);
		Objects.requireNonNull(elementClass);
		return gson.fromJson(jsonString,
			TypeFactory.newCollectionTypeToken(collectionClass, elementClass));
	}

	/**
	 * Transforms the given json string into a java object {@link List}
	 *
	 * @param           the generic type of the return type
	 * @param jsonString   the json string
	 * @param elementClass the element class of the generic type
	 * @return the list with the java objects
	 */
	public static  List toObjectList(final String jsonString, final Class elementClass)
	{
		Objects.requireNonNull(jsonString);
		Objects.requireNonNull(elementClass);
		return (List)toObjectCollection(jsonString, List.class, elementClass);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy