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

com.statsig.internal.GsonUtil Maven / Gradle / Ivy

There is a newer version: 0.28.0
Show newest version
package com.statsig.internal;

import com.google.gson.*;

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

public class GsonUtil {
    private static final Gson gson = new GsonBuilder()
            .registerTypeAdapter(Map.class, new MapDeserializer())
            .create();

    public static Gson getGson() {
        return gson;
    }

     static class MapDeserializer implements JsonDeserializer> {

         @Override
         public Map deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
             Map map = new HashMap<>();
             try {
                 JsonObject obj = json.getAsJsonObject();

                 for (Map.Entry entry : obj.entrySet()) {
                     map.put(entry.getKey(), entry.getValue());
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
             return map;
         }
     }

    public static String getString(Map value, String key, String defaultValue) {
        JsonElement res = value.get(key);
        return (res != null && res.isJsonPrimitive() && res.getAsJsonPrimitive().isString()) ? res.getAsString() : defaultValue;
    }

    public static boolean getBoolean(Map value, String key, boolean defaultValue) {
        JsonElement res = value.get(key);
        return (res != null && res.isJsonPrimitive() && res.getAsJsonPrimitive().isBoolean()) ? res.getAsBoolean() : defaultValue;
    }

    public static double getDouble(Map value, String key, double defaultValue) {
        JsonElement res = value.get(key);
        return (res != null && res.isJsonPrimitive() && res.getAsJsonPrimitive().isNumber()) ? res.getAsDouble() : defaultValue;
    }

    public static int getInt(Map value, String key, int defaultValue) {
        JsonElement res = value.get(key);
        return (res != null && res.isJsonPrimitive() && res.getAsJsonPrimitive().isNumber()) ? res.getAsInt() : defaultValue;
    }

    public static long getLong(Map value, String key, long defaultValue) {
        JsonElement res = value.get(key);
        return (res != null && res.isJsonPrimitive() && res.getAsJsonPrimitive().isNumber()) ? res.getAsLong() : defaultValue;
    }

    public static Object[] getArray(Map value, String key, Object[] defaultValue) {
        JsonElement res = value.get(key);
        if (res != null && res.isJsonArray()) {
            JsonArray jsonArray = res.getAsJsonArray();
            Object[] array = new Object[jsonArray.size()];
            for (int i = 0; i < jsonArray.size(); i++) {
                array[i] = jsonArray.get(i);
            }
            return array;
        }
        return defaultValue;
    }

    public static Map getMap(Map value, String key, Map defaultValue) {
        JsonElement res = value.get(key);
        if (res != null && res.isJsonObject()) {
            JsonObject jsonObject = res.getAsJsonObject();
            Map map = new HashMap<>();
            for (Map.Entry entry : jsonObject.entrySet()) {
                map.put(entry.getKey(), entry.getValue());
            }
            return map;
        }
        return defaultValue;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy