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

exchange.apexpro.connector.impl.utils.JsonWrapper Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package exchange.apexpro.connector.impl.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import exchange.apexpro.connector.exception.ApexProApiException;

import java.math.BigDecimal;
import java.util.*;

public class JsonWrapper {

    private final JSONObject json;

    public static JsonWrapper parseFromString(String text) {
        try {
            JSONObject jsonObject;
            if (JSON.parse(text) instanceof JSONArray) {
                jsonObject = (JSONObject) JSON.parse("{data:" + text + "}");
            } else {
                jsonObject = (JSONObject) JSON.parse(text);
            }
            if (jsonObject != null) {
                return new JsonWrapper(jsonObject);
            } else {
                throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                        "[Json] Unknown error when parse: " + text);
            }
        } catch (JSONException e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR, "[Json] Fail to parse json: " + text);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR, "[Json] " + e.getMessage());
        }
    }

    public JsonWrapper(JSONObject json) {
        this.json = json;
    }

    private void checkMandatoryField(String name) {
//        if (!json.containsKey(name)) {
//            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
//                    "[Json] Get json item field: " + name + " does not exist");
//        }
    }

    public boolean containKey(String name) {
        return json.containsKey(name);
    }

    public String getString(String name) {
        checkMandatoryField(name);
        try {
            return json.getString(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get string error: " + name + " " + e.getMessage());
        }
    }

    public String getStringOrDefault(String name, String def) {
        if (!containKey(name)) {
            return def;
        }
        return getString(name);
    }

    public Boolean getBoolean(String name) {
        checkMandatoryField(name);
        try {
            return json.getBoolean(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get boolean error: " + name + " " + e.getMessage());
        }
    }

    public Integer getInteger(String name) {
        checkMandatoryField(name);
        try {
            return json.getInteger(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get integer error: " + name + " " + e.getMessage());
        }
    }

    public Integer getIntegerOrDefault(String name, Integer defValue) {
        try {
            if (!containKey(name)) {
                return defValue;
            }
            return json.getInteger(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get integer error: " + name + " " + e.getMessage());
        }
    }

    public Long getLong(String name) {
        checkMandatoryField(name);
        try {
            return json.getLong(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get long error: " + name + " " + e.getMessage());
        }
    }

    public Long getLongOrDefault(String name, long defValue) {
        try {
            if (!containKey(name)) {
                return defValue;
            }
            return json.getLong(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get long error: " + name + " " + e.getMessage());
        }
    }
    public Double getDouble(String name) {
        checkMandatoryField(name);
        try {
            return json.getDouble(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get double error: " + name + " " + e.getMessage());
        }
    }

    public Double getDoubleOrDefault(String name, Double defValue) {
        try {
            if (!containKey(name)) {
                return defValue;
            }
            return json.getDouble(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get double error: " + name + " " + e.getMessage());
        }
    }

    public BigDecimal getBigDecimal(String name) {
        checkMandatoryField(name);
        try {
            return new BigDecimal(json.getBigDecimal(name).stripTrailingZeros().toPlainString());
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get decimal error: " + name + " " + e.getMessage());
        }
    }

    public BigDecimal getBigDecimalOrDefault(String name, BigDecimal defValue) {
        if (!containKey(name)) {
            return defValue;
        }
        try {
            return new BigDecimal(json.getBigDecimal(name).stripTrailingZeros().toPlainString());
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
                    "[Json] Get decimal error: " + name + " " + e.getMessage());
        }
    }

    public JsonWrapper getJsonObject(String name) {
        checkMandatoryField(name);
        return new JsonWrapper(json.getJSONObject(name));
    }

    public JSONObject convert2JsonObject() {
        return this.json;
    }

    public void getJsonObject(String name, Handler todo) {
        checkMandatoryField(name);
        todo.handle(new JsonWrapper(json.getJSONObject(name)));
    }

    public JsonWrapperArray getJsonArray(String name) {
        checkMandatoryField(name);
        JSONArray array = null;
        try {
            array = json.getJSONArray(name);
        } catch (Exception e) {
            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR, "[Json] Get array: " + name + " error");
        }
        if (array == null) {
//            throw new ApexProApiException(ApexProApiException.RUNTIME_ERROR,
//                    "[Json] Array: " + name + " does not exist");
            array = new JSONArray();
        }
        return new JsonWrapperArray(array);
    }

    public JSONObject getJson() {
        return json;
    }

    public List> convert2DictList() {
        List> result = new LinkedList<>();
        Set keys = this.json.keySet();
        keys.forEach((key) -> {
            Map temp = new LinkedHashMap<>();
            temp.put(key, this.getString(key));
            result.add(temp);
        });
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy