Example usage for com.google.gson JsonElement toString

List of usage examples for com.google.gson JsonElement toString

Introduction

In this page you can find the example usage for com.google.gson JsonElement toString.

Prototype

@Override
public String toString() 

Source Link

Document

Returns a String representation of this element.

Usage

From source file:com.googleapis.ajax.services.impl.TransliterateLanguageQueryImpl.java

License:Apache License

@Override
public PagedList<TransliterateLanguageResult> list() {
    InputStream jsonContent = null;
    try {//from  www . j  a v a  2s.c  om
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            PagedList<TransliterateLanguageResult> responseList = unmarshallList(response.getAsJsonObject());
            notifyObservers(responseList);
            return responseList;
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.ajax.services.impl.TransliterateLanguageQueryImpl.java

License:Apache License

@Override
public TransliterateLanguageResult singleResult() {
    InputStream jsonContent = null;
    try {/*from  w  ww.j  a  va 2 s  . c o  m*/
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            PagedList<TransliterateLanguageResult> responseList = unmarshallList(response.getAsJsonObject());
            notifyObservers(responseList);
            return responseList.isEmpty() ? null : responseList.get(0);
        }
        throw new GoogleSearchException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleSearchException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.googleapis.maps.services.impl.BaseGoogleMapsApiQuery.java

License:Apache License

@Override
public List<T> list() {
    InputStream jsonContent = null;
    try {//from w  w w  .  j  a v  a 2  s  .com
        jsonContent = callApiGet(apiUrlBuilder.buildUrl());
        JsonElement response = parser.parse(new InputStreamReader(jsonContent, UTF_8_CHAR_SET));
        if (response.isJsonObject()) {
            List<T> responseList = unmarshallList(response.getAsJsonObject());
            notifyObservers(responseList);
            return responseList;
        }
        throw new GoogleMapsException("Unknown content found in response:" + response.toString());
    } catch (Exception e) {
        throw new GoogleMapsException(e);
    } finally {
        closeStream(jsonContent);
    }
}

From source file:com.gst.infrastructure.campaigns.sms.service.SmsCampaignWritePlatformServiceJpaImpl.java

License:Apache License

@Override
public CampaignPreviewData previewMessage(final JsonQuery query) {
    CampaignPreviewData campaignMessage = null;
    this.context.authenticatedUser();
    this.smsCampaignValidator.validatePreviewMessage(query.json());
    // final String smsParams =
    // this.fromJsonHelper.extractJsonObjectNamed("paramValue",
    // query.parsedJson()).getAsString();
    final JsonElement smsParamsElement = this.fromJsonHelper
            .extractJsonObjectNamed(SmsCampaignValidator.paramValue, query.parsedJson());
    String smsParams = smsParamsElement.toString();
    final String textMessageTemplate = this.fromJsonHelper.extractStringNamed("message", query.parsedJson());

    try {/*from  w  w  w . j  ava2s  .co  m*/
        HashMap<String, String> campaignParams = new ObjectMapper().readValue(smsParams,
                new TypeReference<HashMap<String, String>>() {
                });

        HashMap<String, String> queryParamForRunReport = new ObjectMapper().readValue(smsParams,
                new TypeReference<HashMap<String, String>>() {
                });

        List<HashMap<String, Object>> runReportObject = this
                .getRunReportByServiceImpl(campaignParams.get("reportName"), queryParamForRunReport);

        if (runReportObject != null && !runReportObject.isEmpty()) {
            for (HashMap<String, Object> entry : runReportObject) {
                // add string object to campaignParam object
                String textMessage = this.compileSmsTemplate(textMessageTemplate, "SmsCampaign", entry);
                if (!textMessage.isEmpty()) {
                    final Integer totalMessage = runReportObject.size();
                    campaignMessage = new CampaignPreviewData(textMessage, totalMessage);
                    break;
                }
            }
        } else {
            campaignMessage = new CampaignPreviewData(textMessageTemplate, 0);
        }
    } catch (final IOException e) {
        // TODO throw something here
    }
    return campaignMessage;
}

From source file:com.haulmont.restapi.common.RestParseUtils.java

License:Apache License

public Map<String, String> parseParamsJson(String paramsJson) {
    Map<String, String> result = new LinkedHashMap<>();
    if (Strings.isNullOrEmpty(paramsJson))
        return result;

    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = jsonParser.parse(paramsJson).getAsJsonObject();

    for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
        String paramName = entry.getKey();
        JsonElement paramValue = entry.getValue();
        if (paramValue.isJsonNull()) {
            result.put(paramName, null);
        } else if (paramValue.isJsonPrimitive()) {
            result.put(paramName, paramValue.getAsString());
        } else {//from  w  w w  .j  a  v a 2 s.  co  m
            result.put(paramName, paramValue.toString());
        }
    }

    return result;
}

From source file:com.haulmont.restapi.service.QueriesControllerManager.java

License:Apache License

protected Object toObject(Class clazz, String value) throws ParseException {
    if (clazz.isArray()) {
        Class componentType = clazz.getComponentType();
        JsonParser jsonParser = new JsonParser();
        JsonArray jsonArray = jsonParser.parse(value).getAsJsonArray();
        List result = new ArrayList();
        for (JsonElement jsonElement : jsonArray) {
            String stringValue = (jsonElement.isJsonPrimitive() && jsonElement.getAsJsonPrimitive().isString())
                    ? jsonElement.getAsJsonPrimitive().getAsString()
                    : jsonElement.toString();
            Object arrayElementValue = toObject(componentType, stringValue);
            result.add(arrayElementValue);
        }/*  w ww. j a  v  a 2 s  . c o m*/
        return result;
    }
    if (String.class == clazz)
        return value;
    if (Integer.class == clazz || Integer.TYPE == clazz || Byte.class == clazz || Byte.TYPE == clazz
            || Short.class == clazz || Short.TYPE == clazz)
        return Datatypes.getNN(Integer.class).parse(value);
    if (Date.class == clazz) {
        try {
            return Datatypes.getNN(Date.class).parse(value);
        } catch (ParseException e) {
            try {
                return Datatypes.getNN(java.sql.Date.class).parse(value);
            } catch (ParseException e1) {
                return Datatypes.getNN(Time.class).parse(value);
            }
        }
    }
    if (BigDecimal.class == clazz)
        return Datatypes.getNN(BigDecimal.class).parse(value);
    if (Boolean.class == clazz || Boolean.TYPE == clazz)
        return Datatypes.getNN(Boolean.class).parse(value);
    if (Long.class == clazz || Long.TYPE == clazz)
        return Datatypes.getNN(Long.class).parse(value);
    if (Double.class == clazz || Double.TYPE == clazz || Float.class == clazz || Float.TYPE == clazz)
        return Datatypes.getNN(Double.class).parse(value);
    if (UUID.class == clazz)
        return UUID.fromString(value);
    throw new IllegalArgumentException("Parameters of type " + clazz.getName() + " are not supported");
}

From source file:com.haulmont.restapi.service.RestServiceInvoker.java

License:Apache License

private void parseParamsJson(String paramsJson, List<String> paramValuesStr, List<Class> paramTypes) {
    if (Strings.isNullOrEmpty(paramsJson))
        return;//from   ww w.j ava2s  .  c o  m
    JsonParser jsonParser = new JsonParser();
    JsonObject jsonObject = jsonParser.parse(paramsJson).getAsJsonObject();
    int idx = 0;
    while (true) {
        JsonElement nthParam = jsonObject.get("param" + idx);
        if (nthParam == null)
            break;
        if (nthParam.isJsonPrimitive()) {
            paramValuesStr.add(nthParam.getAsString());
        } else {
            paramValuesStr.add(nthParam.toString());
        }

        JsonElement nthParamType = jsonObject.get("param" + idx + "_type");
        if (nthParamType != null) {
            try {
                paramTypes.add(ClassUtils.forName(nthParamType.getAsString(), null));
            } catch (ClassNotFoundException e) {
                throw new RestAPIException("Error on evaluating parameter type", e.getMessage(),
                        HttpStatus.BAD_REQUEST, e);
            }
        }
        idx++;
    }
}

From source file:com.heliosapm.shorthand.instrumentor.shorthand.ShorthandJSONScript.java

License:Open Source License

/**
 * {@inheritDoc}//from  ww  w . j  av  a2s .c om
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
 */
@Override
public ShorthandJSONScript deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    ShorthandJSONScript pre = GsonProvider.getInstance().getNoSerGson().fromJson(json,
            ShorthandJSONScript.class);
    return postDeserialize(pre, json.toString());
}

From source file:com.heroiclabs.sdk.android.util.json.JsonDeserializerUtils.java

License:Apache License

/**
 * Extract a JsonElement member as a String from a JsonObject, if it exists.
 *
 * @param object The JsonObject to extract from.
 * @param memberName The member name to extract.
 * @return A String representation of the corresponding JsonElement.
 *///from  ww w  .j a  va2  s .com
protected static String extractAsString(final @NonNull JsonObject object, final @NonNull String memberName) {
    if (object.has(memberName)) {
        final JsonElement member = object.get(memberName);
        if (member.isJsonNull()) {
            return null;
        }
        return member.toString();
    }
    return null;
}

From source file:com.hp.ov.sdk.adaptors.StorageCapabilitiesDeserializer.java

License:Apache License

@Override
public StorageCapabilities deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    if (json.isJsonArray()) {
        StorageCapabilities storageCapabilities = new StorageCapabilities();

        storageCapabilities.setControllerModes(Lists.newArrayList(ControllerMode.RAID));
        storageCapabilities.setDriveTechnologies(new ArrayList<DriveTechnology>());
        storageCapabilities.setRaidLevels(new ArrayList<RaidLevel>());

        Iterator<JsonElement> iterator = json.getAsJsonArray().iterator();

        while (iterator.hasNext()) {
            JsonElement element = iterator.next();

            if (!element.isJsonPrimitive()) {
                throw new JsonParseException(
                        "Expected a primitive (String) value but found " + element.toString());
            }/*from   w ww .  jav a 2 s  .c o  m*/

            try {
                storageCapabilities.getRaidLevels().add(RaidLevel.valueOf(element.getAsString()));
            } catch (IllegalArgumentException e) {
                throw new JsonParseException("Unknown RAID Level", e);
            }
        }
        return storageCapabilities;
    }
    return new Gson().fromJson(json, StorageCapabilities.class);
}