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

com.tencentcloudapi.common.CommonRequest Maven / Gradle / Ivy

There is a newer version: 3.1.1108
Show newest version
package com.tencentcloudapi.common;

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

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

public final class CommonRequest extends AbstractModel {
    private static final Gson gson = new GsonBuilder().create();
    private static final Type typeToken = new TypeToken>() {
    }.getType();

    public CommonRequest(String jsonReq) {
        HashMap body = gson.fromJson(jsonReq, typeToken);
        for (Map.Entry entry : body.entrySet()) {
            set(entry.getKey(), entry.getValue());
        }
    }

    @Override
    protected void toMap(HashMap map, String prefix) {
        toMapFromObject(map, prefix, any());
    }

    private void toMapFromObject(HashMap map, String prefix, Object layer) {
        if (layer == null)
            return;

        if (layer instanceof List) {
            if (!prefix.isEmpty()) {
                prefix += ".";
            }
            int i = 0;
            for (Object entry : (List) layer) {
                toMapFromObject(map, prefix + i, entry);
                i++;
            }
        } else if (layer instanceof Map) {
            if (!prefix.isEmpty()) {
                prefix += ".";
            }
            for (Map.Entry entry : ((Map) layer).entrySet()) {
                String key = entry.getKey();
                Object val = entry.getValue();
                toMapFromObject(map, prefix + key, val);
            }
        } else {
            map.put(prefix, layer.toString());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy