Example usage for com.google.gson JsonElement getAsString

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

Introduction

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

Prototype

public String getAsString() 

Source Link

Document

convenience method to get this element as a string value.

Usage

From source file:com.kurento.kmf.connector.ThriftConnectorJsonRpcHandler.java

License:Open Source License

private void internalEventJsonRpc(String request) {
    try {//from   w w w  .j a va  2  s. c o m

        LOG.debug("<-* {}", request.trim());

        Request<JsonObject> requestObj = JsonUtils.fromJsonRequest(request, JsonObject.class);

        JsonElement subsJsonElem = requestObj.getParams().get("value").getAsJsonObject().get("subscription");

        if (subsJsonElem == null) {
            LOG.error("Received event wihthout subscription: {}", request);
            return;
        }

        String subscription = subsJsonElem.getAsString().trim();
        Session session = subscriptions.get(subscription);
        if (session == null) {
            LOG.error("Unknown subscription: \"{}\"", subscriptions);
            LOG.info("Subscriptions\n");
            return;
        }

        try {

            session.sendNotification("onEvent", requestObj.getParams());

        } catch (Exception e) {
            LOG.error("Exception while sending event", e);
        }

    } catch (Exception e) {
        LOG.error("Exception processing server event", e);
    }
}

From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java

License:Open Source License

private static String extractSessionId(JsonObject jsonObject, String memberName) {
    JsonElement responseJson = jsonObject.get(memberName);

    if (responseJson != null && responseJson.isJsonObject()) {

        JsonObject responseJsonObject = (JsonObject) responseJson;

        JsonElement sessionIdJson = responseJsonObject.remove(SESSION_ID_PROPERTY);

        if (sessionIdJson != null && !(sessionIdJson instanceof JsonNull)) {
            return sessionIdJson.getAsString();
        }//from  ww  w.  ja  v  a 2 s .  c  o m
    }
    return null;
}

From source file:com.learn.mobile.library.dmobi.helper.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (type.getRawType() != baseType) {
        return null;
    }//  w w  w  . j  ava2 s  . co  m

    final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>();
    final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>();
    for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) {
        TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue()));
        labelToDelegate.put(entry.getKey(), delegate);
        subtypeToDelegate.put(entry.getValue(), delegate);
    }

    return new TypeAdapter<R>() {
        @Override
        public R read(JsonReader in) throws IOException {
            JsonElement jsonElement = Streams.parse(in);
            // JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName);
            // Do not remove typeFieldName
            JsonElement labelJsonElement = jsonElement.getAsJsonObject().get(typeFieldName);

            if (labelJsonElement == null) {
                throw new JsonParseException("cannot deserialize " + baseType
                        + " because it does not define a field named " + typeFieldName);
            }
            String label = labelJsonElement.getAsString();
            @SuppressWarnings("unchecked") // registration requires that subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label);
            if (delegate == null) {
                throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label
                        + "; did you forget to register a subtype?");
            }
            return delegate.fromJsonTree(jsonElement);
        }

        @Override
        public void write(JsonWriter out, R value) throws IOException {
            Class<?> srcType = value.getClass();
            String label = subtypeToLabel.get(srcType);
            @SuppressWarnings("unchecked") // registration requires that subtype extends T
            TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType);
            if (delegate == null) {
                throw new JsonParseException(
                        "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?");
            }
            JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject();
            if (jsonObject.has(typeFieldName)) {
                throw new JsonParseException("cannot serialize " + srcType.getName()
                        + " because it already defines a field named " + typeFieldName);
            }
            JsonObject clone = new JsonObject();
            clone.add(typeFieldName, new JsonPrimitive(label));
            for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) {
                clone.add(e.getKey(), e.getValue());
            }
            Streams.write(clone, out);
        }
    }.nullSafe();
}

From source file:com.lion328.xenonlauncher.minecraft.launcher.json.data.type.DependencyNameTypeAdapter.java

License:Open Source License

@Override
public DependencyName deserialize(JsonElement jsonElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    try {//w  w w  .  j a va 2s  .com
        return new DependencyName(jsonElement.getAsString());
    } catch (IllegalArgumentException e) {
        throw new JsonParseException(e);
    }
}

From source file:com.lithium.luces.Luces.java

License:Apache License

@Override
public Luces mapping(String typename, JsonObject mapping) {
    if (log.isDebugEnabled()) {
        log.debug("Adding mapping for type " + typename);
    }// ww  w .j ava  2s  .c o  m
    if (log.isTraceEnabled()) {
        log.trace("Sending mapping: " + new GsonBuilder().setPrettyPrinting().create().toJson(mapping));
    }

    if (null == typename || null == mapping) {
        if (errIfMappingNull) {
            throw new IllegalStateException(
                    String.format("%1$s cannot be set to null", typename == null ? "Type" : "Mapping"));
        }
        log.warn("Setting mapping and type to null, no primitive type conversion will be done");
        typeName = null;
        typeMap = null;
    } else {
        typeName = typename;
        typeMap = new HashMap<>();
        JsonObject workingJson = mapping.getAsJsonObject(typename);
        if (null == workingJson) {
            throw new NoSuchElementException(typename + " type not present or misnamed in mapping");
        }
        // TODO account for nesting
        workingJson = workingJson.getAsJsonObject("properties");
        for (Entry<String, JsonElement> entry : workingJson.entrySet()) {
            JsonElement typeElt = entry.getValue().getAsJsonObject().get("type");
            if (null == typeElt) {
                throw new NoSuchElementException(
                        "Invalid mapping: No type defined for " + entry.getKey() + " field.");
            }
            ParseType parseType;
            try {
                parseType = ParseType.valueOf(typeElt.getAsString().toUpperCase());
            } catch (UnsupportedOperationException ex) {
                throw new UnsupportedOperationException(
                        "Invalid Mapping: Type defined is not a string: " + typeElt.toString());
            } catch (IllegalArgumentException illegal) {
                throw new UnsupportedOperationException(
                        "The " + typeElt.getAsString() + " type is not supported for conversion");
            }
            typeMap.put(entry.getKey(), parseType);
        }
    }
    return this;
}

From source file:com.luan.thermospy.android.core.pojo.ServerStatusSerializer.java

License:Open Source License

@Override
public ServerStatus deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String typeInt = json.getAsString();
    return ServerStatus.parseString(typeInt);
}

From source file:com.luorrak.ouroboros.api.JsonParser.java

License:Open Source License

public String getCatalogFilename(JsonObject catalogThreadJson) {
    JsonElement fileName = catalogThreadJson.get(CATALOG_FILENAME);
    return fileName != null ? fileName.getAsString() : null;
}

From source file:com.luorrak.ouroboros.api.JsonParser.java

License:Open Source License

public String getCatalogSub(JsonObject catalogThreadJson) {
    JsonElement sub = catalogThreadJson.get(CATALOG_SUB);
    return sub != null ? sub.getAsString() : null;
}

From source file:com.luorrak.ouroboros.api.JsonParser.java

License:Open Source License

public String getCatalogCom(JsonObject catalogThreadJson) {
    JsonElement com = catalogThreadJson.get(CATALOG_COM);
    return com != null ? com.getAsString() : null;
}

From source file:com.luorrak.ouroboros.api.JsonParser.java

License:Open Source License

public String getCatalogTim(JsonObject catalogThreadJson) {
    JsonElement tim = catalogThreadJson.get(CATALOG_TIM);
    return tim != null ? tim.getAsString() : null;
}