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

com.qiniu.util.Json Maven / Gradle / Ivy

There is a newer version: 7.17.0
Show newest version
package com.qiniu.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Map;

public final class Json {
    private Json() {
    }

    public static String encode(StringMap map) {
        return new Gson().toJson(map.map());
    }

    public static String encode(Object obj) {
        return new GsonBuilder().serializeNulls().create().toJson(obj);
    }

    public static  T decode(String json, Class classOfT) {
        return new Gson().fromJson(json, classOfT);
    }

    public static  T decode(JsonElement jsonElement, Class clazz) {
        Gson gson = new Gson();
        return gson.fromJson(jsonElement, clazz);
    }

    public static StringMap decode(String json) {
        // CHECKSTYLE:OFF
        Type t = new TypeToken>() {
        }.getType();
        // CHECKSTYLE:ON
        Map x = new Gson().fromJson(json, t);
        return new StringMap(x);
    }

    public static Object[] decodeArray(String json) {
        // CHECKSTYLE:OFF
        Type t = new TypeToken() {
        }.getType();
        // CHECKSTYLE:ON
        return new Gson().fromJson(json, t);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy