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

com.ithit.webdav.integration.utils.SerializationUtils Maven / Gradle / Ivy

There is a newer version: 7.3.10641
Show newest version
package com.ithit.webdav.integration.utils;


import com.google.gson.Gson;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Utility class to perform serialization of objects.
 */
public final class SerializationUtils {

    private SerializationUtils() {
    }

    /**
     * Serializes object to JSON string.
     *
     * @param object Object to serialize.
     * @return String in JSON format
     */
    public static  String serialize(T object) {
        Gson gson = new Gson();
        return gson.toJson(object);
    }

    /**
     * Deserialize JSON string to object list.
     *
     * @param clazz Type of objects in the list to deserialize.
     * @param json  JSON string to deserialize.
     * @return List of objects.
     */
    @SuppressWarnings("unchecked")
    public static  List deserializeList(final Class clazz, final String json) {
        T[] array = (T[]) java.lang.reflect.Array.newInstance(clazz, 1);
        array = new Gson().fromJson(json, (Type) array.getClass());
        if (array == null) {
            return new ArrayList<>();
        }
        return new ArrayList<>(Arrays.asList(array));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy