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

com.sigopt.net.APIResource Maven / Gradle / Ivy

There is a newer version: 6.3.0
Show newest version
package com.sigopt.net;

import com.google.gson.*;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class APIResource extends APIObject {

    static final JsonParser jsonParser = new JsonParser();
    static final Gson gson = new GsonBuilder()
        .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
        .create();

    public static  T constructFromJson(String json, Class  klass) {
        return gson.fromJson(preProcessJson(json), klass);
    }

    public static  T constructListFromJson(String json, Type typeOfT) {
        return (T) gson.fromJson(preProcessJson(json), typeOfT);
    }

    static String preProcessJson(String json) {
        JsonElement jsonElement = jsonParser.parse(json);
        for(Map.Entry entry : jsonElement.getAsJsonObject().getAsJsonObject("response").entrySet()) {
            return entry.getValue().toString();
        }
        // This shouldn't happen.
        return json;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy