Search in sources :

Example 21 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project ff4j by ff4j.

the class FeatureConverter method deserialize.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Feature deserialize(JsonElement json, Type type, JsonDeserializationContext context) {
    String uid = json.getAsJsonObject().get("uid").getAsString();
    Feature feature = new Feature(uid);
    // Property "enable"
    JsonElement enable = json.getAsJsonObject().get("enable");
    if (enable != null) {
        feature.setEnable(enable.getAsBoolean());
    }
    // Description
    JsonElement description = json.getAsJsonObject().get("description");
    if (description != null) {
        feature.setDescription(description.getAsString());
    }
    // Group
    JsonElement group = json.getAsJsonObject().get("group");
    if (group != null) {
        feature.setGroup(group.getAsString());
    }
    // Permissions
    JsonElement permissions = json.getAsJsonObject().get("permissions");
    if (permissions != null) {
        Set<String> auths = gson.fromJson(permissions, new TypeToken<HashSet<String>>() {
        }.getType());
        feature.setPermissions(auths);
    }
    // Flipping strategy
    JsonElement flippingStrategy = json.getAsJsonObject().get("flippingStrategy");
    if (flippingStrategy != null && !flippingStrategy.isJsonNull()) {
        Map<String, ?> flippingStrategyParameters = gson.fromJson(flippingStrategy, new TypeToken<HashMap<String, ?>>() {
        }.getType());
        String flippingStrategyType = flippingStrategyParameters.get("type").toString();
        Map<String, String> initParams = (Map<String, String>) flippingStrategyParameters.get("initParams");
        // Adding flipping strategy
        feature.setFlippingStrategy(MappingUtil.instanceFlippingStrategy(uid, flippingStrategyType, initParams));
    }
    // Custom properties
    JsonElement customProperties = json.getAsJsonObject().get("customProperties");
    if (customProperties != null && !customProperties.isJsonNull()) {
        Map<String, LinkedTreeMap<String, Object>> map = new Gson().fromJson(customProperties, new TypeToken<com.google.gson.internal.LinkedTreeMap<String, LinkedTreeMap<String, Object>>>() {
        }.getType());
        for (Entry<String, LinkedTreeMap<String, Object>> element : map.entrySet()) {
            LinkedTreeMap<String, Object> propertyValues = element.getValue();
            // Getting values
            String pName = (String) propertyValues.get("name");
            String pType = (String) propertyValues.get("type");
            String pValue = (String) propertyValues.get("value");
            String desc = (String) propertyValues.get("description");
            Object fixedValues = propertyValues.get("fixedValues");
            Set pFixedValues = fixedValues != null ? new HashSet((Collection) fixedValues) : null;
            // Creating property with it
            Property<?> property = PropertyFactory.createProperty(pName, pType, pValue, desc, pFixedValues);
            feature.addProperty(property);
        }
    }
    return feature;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) Gson(com.google.gson.Gson) Feature(org.ff4j.core.Feature) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) HashSet(java.util.HashSet)

Example 22 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project bisq-core by bisq-network.

the class FeeProvider method getFees.

public Tuple2<Map<String, Long>, Map<String, Long>> getFees() throws IOException {
    String json = httpClient.requestWithGET("getFees", "User-Agent", "bisq/" + Version.VERSION + ", uid:" + httpClient.getUid());
    // noinspection unchecked
    LinkedTreeMap<String, Object> linkedTreeMap = new Gson().fromJson(json, LinkedTreeMap.class);
    Map<String, Long> tsMap = new HashMap<>();
    tsMap.put("bitcoinFeesTs", ((Double) linkedTreeMap.get("bitcoinFeesTs")).longValue());
    Map<String, Long> map = new HashMap<>();
    try {
        // noinspection unchecked
        LinkedTreeMap<String, Double> dataMap = (LinkedTreeMap<String, Double>) linkedTreeMap.get("dataMap");
        Long btcTxFee = dataMap.get("btcTxFee").longValue();
        Long ltcTxFee = dataMap.get("ltcTxFee").longValue();
        Long dashTxFee = dataMap.get("dashTxFee").longValue();
        map.put("BTC", btcTxFee);
        map.put("LTC", ltcTxFee);
        map.put("DASH", dashTxFee);
    } catch (Throwable t) {
        log.error(t.toString());
        t.printStackTrace();
    }
    return new Tuple2<>(tsMap, map);
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) HashMap(java.util.HashMap) Tuple2(bisq.common.util.Tuple2) Gson(com.google.gson.Gson)

Example 23 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project bisq-core by bisq-network.

the class PriceProvider method getAll.

public Tuple2<Map<String, Long>, Map<String, MarketPrice>> getAll() throws IOException {
    Map<String, MarketPrice> marketPriceMap = new HashMap<>();
    String json = httpClient.requestWithGET("getAllMarketPrices", "User-Agent", "bisq/" + Version.VERSION + ", uid:" + httpClient.getUid());
    // noinspection unchecked
    LinkedTreeMap<String, Object> map = new Gson().fromJson(json, LinkedTreeMap.class);
    Map<String, Long> tsMap = new HashMap<>();
    tsMap.put("btcAverageTs", ((Double) map.get("btcAverageTs")).longValue());
    tsMap.put("poloniexTs", ((Double) map.get("poloniexTs")).longValue());
    tsMap.put("coinmarketcapTs", ((Double) map.get("coinmarketcapTs")).longValue());
    // noinspection unchecked
    List<LinkedTreeMap<String, Object>> list = (ArrayList<LinkedTreeMap<String, Object>>) map.get("data");
    list.forEach(treeMap -> {
        try {
            final String currencyCode = (String) treeMap.get("currencyCode");
            final double price = (double) treeMap.get("price");
            // json uses double for our timestampSec long value...
            final long timestampSec = MathUtils.doubleToLong((double) treeMap.get("timestampSec"));
            marketPriceMap.put(currencyCode, new MarketPrice(currencyCode, price, timestampSec, true));
        } catch (Throwable t) {
            log.error(t.toString());
            t.printStackTrace();
        }
    });
    return new Tuple2<>(tsMap, marketPriceMap);
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Tuple2(bisq.common.util.Tuple2)

Example 24 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project components by Talend.

the class MarketoBaseRESTClient method executePostRequest.

public MarketoRecordResult executePostRequest(JsonObject inputJson) throws MarketoException {
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("POST");
        // "application/json"
        urlConn.setRequestProperty(REQUEST_PROPERTY_CONTENT_TYPE, REQUEST_VALUE_APPLICATION_JSON);
        // content-type is
        // required.
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
        urlConn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
        wr.write(inputJson.toString());
        wr.flush();
        wr.close();
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            InputStreamReader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            LinkedTreeMap ltm = (LinkedTreeMap) gson.fromJson(reader, Object.class);
            MarketoRecordResult mkr = new MarketoRecordResult();
            mkr.setRequestId(REST + "::" + ltm.get("requestId"));
            mkr.setSuccess(Boolean.parseBoolean(ltm.get("success").toString()));
            return mkr;
        } else {
            LOG.error("POST request failed: {}", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("GET request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult)

Example 25 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project components by Talend.

the class MarketoBaseRESTClient method getToken.

public void getToken() throws MarketoException {
    try {
        URL basicURI = new URL(endpoint);
        current_uri = // 
        new StringBuilder(basicURI.getProtocol()).append(// 
        "://").append(// 
        basicURI.getHost()).append(// 
        API_PATH_IDENTITY_OAUTH_TOKEN).append(// 
        fmtParams("client_id", userId)).append(fmtParams("client_secret", secretKey));
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("GET");
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_APPLICATION_JSON);
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            Reader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            LinkedTreeMap js = (LinkedTreeMap) gson.fromJson(reader, Object.class);
            Object ac = js.get("access_token");
            if (ac != null) {
                accessToken = ac.toString();
                LOG.debug("MarketoRestExecutor.getAccessToken GOT token");
            } else {
                LinkedTreeMap err = (LinkedTreeMap) ((ArrayList) js.get(FIELD_ERRORS)).get(0);
                throw new MarketoException(REST, err.get("code").toString(), err.get("message").toString());
            }
        } else {
            throw new MarketoException(REST, responseCode, "Marketo Authentication failed! Please check your " + "setting!");
        }
    } catch (ProtocolException | SocketTimeoutException | SocketException e) {
        LOG.error("AccessToken error: {}.", e.getMessage());
        throw new MarketoException(REST, "Marketo Authentication failed : " + e.getMessage());
    } catch (IOException e) {
        LOG.error("AccessToken error: {}.", e.getMessage());
        throw new MarketoException(REST, "Marketo Authentication failed : " + e.getMessage());
    }
}
Also used : ProtocolException(java.net.ProtocolException) SocketException(java.net.SocketException) InputStreamReader(java.io.InputStreamReader) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) SocketTimeoutException(java.net.SocketTimeoutException) JsonObject(com.google.gson.JsonObject) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)30 Gson (com.google.gson.Gson)15 ArrayList (java.util.ArrayList)10 JsonObject (com.google.gson.JsonObject)6 HashMap (java.util.HashMap)6 InputStreamReader (java.io.InputStreamReader)5 URL (java.net.URL)5 List (java.util.List)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 LSCustomErrorStrategy (org.ballerinalang.langserver.common.LSCustomErrorStrategy)3 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)3 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)3 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)3 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)3 NonNull (android.support.annotation.NonNull)2 Tuple2 (bisq.common.util.Tuple2)2 JsonElement (com.google.gson.JsonElement)2 StatusCallback (com.instructure.canvasapi2.StatusCallback)2