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.jkoolcloud.jesl.net.syslogd.SyslogSend.java

License:Open Source License

private static void jsonSyslog(SyslogIF syslog, SendOptions sendOptions, String line)
        throws InterruptedException {
    JsonElement jelement = jparser.parse(line);
    JsonObject jobject = jelement.getAsJsonObject();
    long offset_usec = jobject.get("offset.usec").getAsLong();
    String facility = jobject.get("facility").getAsString();
    String level = jobject.get("level").getAsString();
    String msg = jobject.get("msg").getAsString();
    JsonElement appl = jobject.get("appl");

    if (appl != null) {
        StructuredSyslogMessageProcessor mpr = new StructuredSyslogMessageProcessor(appl.getAsString());
        mpr.setProcessId(jobject.get("pid").getAsString());
        syslog.setStructuredMessageProcessor(mpr);
        syslog.getConfig().setUseStructuredData(true);
    } else {// www.  j a v  a2s. c  o  m
        syslog.getConfig().setUseStructuredData(false);
    }
    if (!sendOptions.quiet) {
        if (!syslog.getConfig().isUseStructuredData()) {
            System.out.println("Sending(" + offset_usec + ")(" + syslog.getConfig().isUseStructuredData()
                    + "): " + facility + "." + level + " \"" + msg + "\"");
        } else {
            System.out.println("Sending(" + offset_usec + ")(" + syslog.getConfig().isUseStructuredData()
                    + "): " + facility + "." + level + "." + appl.getAsString() + "."
                    + jobject.get("pid").getAsString() + " \"" + msg + "\"");
        }
    }
    Thread.sleep(offset_usec / 1000);

    syslog.getConfig().setFacility(facility);
    syslog.log(SyslogUtility.getLevel(level), msg);
}

From source file:com.johnny.gank.core.http.DateDeserializer.java

License:Apache License

@Override
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    for (SimpleDateFormat dateFormat : mDateFormatList) {
        try {//from  w  w w.  j  a v  a2  s  .  co m
            return dateFormat.parse(json.getAsString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.karura.javascript.JavaScriptDependencyProcessor.java

License:GNU General Public License

static void parseModuleDependencyGraph(Element eElement, HashMap<String, Set<String>> moduleMap,
        HashMap<String, JsonObject> moduleJsonMap) {
    String dependencyText = eElement.getFirstChild().getNodeValue();
    JsonArray modules = (JsonArray) new JsonParser().parse(dependencyText);

    for (int j = 0; j < modules.size(); j++) {
        if (modules.get(j) instanceof JsonNull)
            continue;
        JsonObject module = (JsonObject) modules.get(j);
        if (module != null) {

            String modName = module.get("name").getAsString();
            moduleJsonMap.put(modName, module);
            if (moduleMap.get(modName) == null) {
                moduleMap.put(modName, new HashSet<String>());
            }/* w  w w  .j  a  va 2s . c  om*/
            if (module.get("depends") != null) {
                JsonArray deps = module.get("depends").getAsJsonArray();
                for (int l = 0; l < deps.size(); l++) {
                    JsonElement dep = deps.get(l);
                    String depName = dep.getAsString();
                    if (moduleMap.get(depName) == null) {
                        moduleMap.put(depName, new HashSet<String>());
                    }

                    moduleMap.get(modName).add(depName);
                }
            }
            //System.out.println(module.toString());
        }
    }
}

From source file:com.keydap.sparrow.DateTimeSerializer.java

License:Apache License

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String d = json.getAsString();
    try {/*from w w  w. j a  v a  2s.c  o m*/
        return df.parse(d);
    } catch (Exception e) {
        throw new JsonParseException(e);
    }
}

From source file:com.keylesspalace.tusky.json.SpannedTypeAdapter.java

License:Open Source License

@Override
public Spanned deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String string = json.getAsString();
    if (string != null) {
        return HtmlUtils.fromHtml(string);
    } else {/*from www .  j av a2s .  c o  m*/
        return new SpannedString("");
    }
}

From source file:com.king.tratt.FunctionFactoryProvider.java

License:Apache License

static FunctionFactory jsonField() {
    return new FunctionFactory() {
        Value pathValue;/*from   www  . ja v a2  s.  c  o m*/
        Value jsonValue;
        JsonParser jsonParser;

        @Override
        public String getName() {
            return "jsonfield";
        }

        @Override
        public int getNumberOfArguments() {
            return 2;
        }

        @Override
        public Value create(List<Value> arguments) {
            pathValue = arguments.get(0);
            jsonValue = arguments.get(1);
            jsonParser = new JsonParser();

            return new Value() {

                @Override
                public String toString() {
                    return String.format("jsonfield('%s', '%s')", pathValue, jsonValue);
                }

                @Override
                public String toDebugString(Event e, Context context) {
                    return util.format(e, context, "[[source:jsonfield('~v', '~v')]]~q", pathValue, jsonValue,
                            this);
                }

                @Override
                protected Object getImp(Event e, Context context) {
                    String path = pathValue.asString(e, context);
                    String json = jsonValue.asString(e, context);

                    String result = null;
                    try {
                        result = getJsonFieldValue(path, json);
                    } catch (Throwable t) {
                        result = "[@ERROR malformed json string]";
                    }
                    return util.parseSupportedType(result);
                }

                private String getJsonFieldValue(String path, String json) {
                    JsonElement jsonElem = jsonParser.parse(json);
                    for (String subPath : path.split("\\.")) {
                        JsonObject jsonObj = jsonElem.getAsJsonObject();
                        jsonElem = jsonObj.get(subPath);
                        if (jsonElem == null) {
                            return String.format("[@ERROR incorrect json path: '%s']", path);
                        }
                    }
                    if (jsonElem.isJsonPrimitive()) {
                        return jsonElem.getAsString();
                    }
                    return jsonElem.toString();

                }
            };
        }
    };
}

From source file:com.kolich.common.entities.gson.KolichDefaultDateTypeAdapter.java

License:Open Source License

@Override
public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    if (!(json instanceof JsonPrimitive)) {
        throw new JsonParseException("The date should be a String type.");
    }/*ww w .  ja  v  a 2s .com*/
    final String jsonDate = json.getAsString();
    try {
        synchronized (format_) {
            return format_.parse(jsonDate);
        }
    } catch (ParseException e) {
        throw new JsonSyntaxException("Failed to de-serialize " + "date. Invalid format? = " + jsonDate, e);
    }
}

From source file:com.koodaripalvelut.common.wicket.component.fullcalendar.AjaxFeedBack.java

License:Open Source License

/**
 * @return String The DISPLAY_MODE entry value.
 *///from  w  w  w  . j  a  v  a 2  s. c o  m
public String getDisplayMode() {
    final JsonElement jsonElement = getFeedback().get(VIEW);
    return jsonElement != null ? jsonElement.getAsString() : null;
}

From source file:com.kotcrab.vis.editor.serializer.json.ClassJsonSerializer.java

License:Apache License

@Override
public Class<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    try {/*from   www  . j a  v a  2 s. co  m*/
        return getFullClassName(json.getAsString());
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(e);
    }
}

From source file:com.kotcrab.vis.editor.serializer.json.RuntimeTypeAdapterFactory.java

License:Apache License

public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) {
    if (baseType.isAssignableFrom(type.getRawType()) == false) {
        return null;
    }/*from w ww .j av a2 s .com*/

    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);
            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);
        }
    };
}