Example usage for com.google.gson JsonPrimitive getAsString

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

Introduction

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

Prototype

@Override
public String getAsString() 

Source Link

Document

convenience method to get this element as a String.

Usage

From source file:net.kyori.text.serializer.gson.StyleSerializer.java

License:MIT License

private Style deserialize(final JsonObject json, final Type typeOfT, final JsonDeserializationContext context)
        throws JsonParseException {
    final Style.Builder style = Style.builder();

    if (json.has(COLOR)) {
        final TextColorWrapper color = context.deserialize(json.get(COLOR), TextColorWrapper.class);
        if (color.color != null) {
            style.color(color.color);/*w  w  w.ja  v  a2s. c  o  m*/
        } else if (color.decoration != null) {
            style.decoration(color.decoration, true);
        }
    }

    for (final TextDecoration decoration : DECORATIONS) {
        final String name = TextDecoration.NAMES.name(decoration);
        if (json.has(name)) {
            style.decoration(decoration, json.get(name).getAsBoolean());
        }
    }

    if (json.has(INSERTION)) {
        style.insertion(json.get(INSERTION).getAsString());
    }

    if (json.has(CLICK_EVENT)) {
        final JsonObject clickEvent = json.getAsJsonObject(CLICK_EVENT);
        if (clickEvent != null) {
            final /* @Nullable */ JsonPrimitive rawAction = clickEvent.getAsJsonPrimitive(CLICK_EVENT_ACTION);
            final ClickEvent./*@Nullable*/ Action action = rawAction == null ? null
                    : context.deserialize(rawAction, ClickEvent.Action.class);
            if (action != null && action.readable()) {
                final /* @Nullable */ JsonPrimitive rawValue = clickEvent.getAsJsonPrimitive(CLICK_EVENT_VALUE);
                final /* @Nullable */ String value = rawValue == null ? null : rawValue.getAsString();
                if (value != null) {
                    style.clickEvent(ClickEvent.of(action, value));
                }
            }
        }
    }

    if (json.has(HOVER_EVENT)) {
        final JsonObject hoverEvent = json.getAsJsonObject(HOVER_EVENT);
        if (hoverEvent != null) {
            final /* @Nullable */ JsonPrimitive rawAction = hoverEvent.getAsJsonPrimitive(HOVER_EVENT_ACTION);
            final HoverEvent./*@Nullable*/ Action action = rawAction == null ? null
                    : context.deserialize(rawAction, HoverEvent.Action.class);
            if (action != null && action.readable()) {
                final /* @Nullable */ JsonElement rawValue = hoverEvent.get(HOVER_EVENT_VALUE);
                final /* @Nullable */ Component value = rawValue == null ? null
                        : context.deserialize(rawValue, Component.class);
                if (value != null) {
                    style.hoverEvent(HoverEvent.of(action, value));
                }
            }
        }
    }

    return style.build();
}

From source file:net.nexustools.njs.JSON.java

License:Open Source License

public JSON(final Global global) {
    super(global);
    setHidden("stringify", new AbstractFunction(global) {

        public java.lang.String stringify(BaseObject object) {
            StringBuilder builder = new StringBuilder();
            stringify(object, builder);/*from ww  w  . j av a2  s  .  c o  m*/
            return builder.toString();
        }

        public void stringify(BaseObject object, StringBuilder builder) {
            if (Utilities.isUndefined(object)) {
                builder.append("null");
                return;
            }

            BaseObject toJSON = object.get("toJSON", OR_NULL);
            if (toJSON != null)
                stringify0(((BaseFunction) toJSON).call(object), builder);
            else
                stringify0(object, builder);
        }

        public void stringify0(BaseObject object, StringBuilder builder) {
            if (object instanceof GenericArray) {
                builder.append('[');
                if (((GenericArray) object).length() > 0) {
                    stringify(object.get(0), builder);
                    for (int i = 1; i < ((GenericArray) object).length(); i++) {
                        builder.append(',');
                        stringify(object.get(i), builder);
                    }
                }
                builder.append(']');
            } else if (object instanceof String.Instance) {
                builder.append('"');
                builder.append(object.toString());
                builder.append('"');
            } else if (object instanceof Number.Instance) {
                double number = ((Number.Instance) object).value;
                if (Double.isNaN(number) || Double.isInfinite(number))
                    builder.append("null");

                builder.append(object.toString());
            } else if (object instanceof Boolean.Instance)
                builder.append(object.toString());
            else {
                builder.append('{');
                Iterator<java.lang.String> it = object.keys().iterator();
                if (it.hasNext()) {

                    java.lang.String key = it.next();
                    builder.append('"');
                    builder.append(key);
                    builder.append("\":");
                    stringify(object.get(key), builder);

                    if (it.hasNext()) {
                        do {
                            builder.append(',');
                            key = it.next();
                            builder.append('"');
                            builder.append(key);
                            builder.append("\":");
                            stringify(object.get(key), builder);
                        } while (it.hasNext());
                    }
                }
                builder.append('}');
            }
        }

        @Override
        public BaseObject call(BaseObject _this, BaseObject... params) {
            switch (params.length) {
            case 0:
                return Undefined.INSTANCE;

            case 1:
                if (params[0] == Undefined.INSTANCE)
                    return Undefined.INSTANCE;
                return global.wrap(stringify(params[0]));

            default:
                return global.wrap("undefined");
            }
        }

        @Override
        public java.lang.String name() {
            return "JSON_stringify";
        }
    });
    setHidden("parse", new AbstractFunction(global) {
        final Gson GSON;
        {
            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.registerTypeAdapter(BaseObject.class, new JsonDeserializer<BaseObject>() {
                @Override
                public BaseObject deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
                        throws JsonParseException {
                    if (je.isJsonNull())
                        return Null.INSTANCE;
                    if (je.isJsonPrimitive()) {
                        JsonPrimitive primitive = je.getAsJsonPrimitive();
                        if (primitive.isBoolean())
                            return primitive.getAsBoolean() ? global.Boolean.TRUE : global.Boolean.FALSE;
                        if (primitive.isNumber())
                            return global.wrap(primitive.getAsDouble());
                        if (primitive.isString())
                            return global.wrap(primitive.getAsString());
                        throw new UnsupportedOperationException(primitive.toString());
                    }
                    if (je.isJsonObject()) {
                        GenericObject go = new GenericObject(global);
                        JsonObject jo = je.getAsJsonObject();
                        for (Map.Entry<java.lang.String, JsonElement> entry : jo.entrySet()) {
                            go.set(entry.getKey(), deserialize(entry.getValue(), type, jdc));
                        }
                        return go;
                    }
                    if (je.isJsonArray()) {
                        JsonArray ja = je.getAsJsonArray();
                        BaseObject[] array = new BaseObject[ja.size()];
                        for (int i = 0; i < array.length; i++) {
                            array[i] = deserialize(ja.get(i), type, jdc);
                        }
                        return new GenericArray(global, array);
                    }
                    throw new UnsupportedOperationException(je.toString());
                }
            });
            GSON = gsonBuilder.create();
        }

        @Override
        public BaseObject call(BaseObject _this, BaseObject... params) {
            try {
                return GSON.fromJson(params[0].toString(), BaseObject.class);
            } catch (com.google.gson.JsonSyntaxException ex) {
                throw new Error.JavaException("SyntaxError", "Unexpected token", ex);
            }
        }

        @Override
        public java.lang.String name() {
            return "JSON_parse";
        }
    });
}

From source file:net.oauth.jsontoken.JsonToken.java

License:Apache License

private String getParamAsString(String param) {
    JsonPrimitive primitive = getParamAsPrimitive(param);
    return primitive == null ? null : primitive.getAsString();
}

From source file:net.oauth.signatures.SignedJsonAssertionToken.java

License:Apache License

public String getSubject() {
    JsonPrimitive subjectJson = getParamAsPrimitive(SUBJECT);
    return subjectJson == null ? null : subjectJson.getAsString();
}

From source file:net.oauth.signatures.SignedJsonAssertionToken.java

License:Apache License

public String getScope() {
    JsonPrimitive scopeJson = getParamAsPrimitive(SCOPE);
    return scopeJson == null ? null : scopeJson.getAsString();
}

From source file:net.oauth.signatures.SignedJsonAssertionToken.java

License:Apache License

public String getNonce() {
    JsonPrimitive nonceJson = getParamAsPrimitive(NONCE);
    return nonceJson == null ? null : nonceJson.getAsString();
}

From source file:net.praqma.tracey.tracey_rabbitmq_neo4j_bridge.Tracey2Neo.java

private Object getPrimitiveType(JsonElement jo) {
    if (!jo.isJsonPrimitive()) {
        return null;
    }/*from   www . ja  v  a 2  s  . co  m*/
    JsonPrimitive jp = jo.getAsJsonPrimitive();
    if (jp.isBoolean()) {
        return jp.getAsBoolean();
    }
    if (jp.isNumber()) {
        return jp.getAsNumber();
    }
    if (jp.isString()) {
        return jp.getAsString();
    }
    return null;
}

From source file:org.apache.abdera2.activities.io.gson.BaseAdapter.java

License:Apache License

public ASBase deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject obj = (JsonObject) el;// ww  w  .  j  a  v a  2s.c  o m
    ASBase.Builder<?, ?> builder;
    if (type == Collection.class)
        builder = Collection.makeCollection();
    else if (type == Activity.class)
        builder = Activity.makeActivity();
    else if (type == MediaLink.class)
        builder = MediaLink.makeMediaLink();
    else if (type == PlaceObject.class)
        builder = PlaceObject.makePlace();
    else if (type == Mood.class)
        builder = Mood.makeMood();
    else if (type == Address.class)
        builder = Address.makeAddress();
    else {
        JsonPrimitive ot = obj.getAsJsonPrimitive("objectType");
        if (ot != null) {
            String ots = ot.getAsString();
            Class<? extends ASObject.Builder> _class = objsmap.get(ots);
            if (_class != null) {
                builder = Discover.locate(_class, _class.getName());
                try {
                    builder = _class.getConstructor(String.class).newInstance(ots);
                } catch (Throwable t) {
                }

            } else
                builder = ASObject.makeObject(ots);
        } else {
            if (obj.has("verb") && (obj.has("actor") || obj.has("object") || obj.has("target"))) {
                builder = Activity.makeActivity();
            } else if (obj.has("items")) {
                builder = Collection.makeCollection();
            } else {
                builder = ASObject.makeObject(); // anonymous
            }
        }
    }
    for (Entry<String, JsonElement> entry : obj.entrySet()) {
        String name = entry.getKey();
        if (name.equalsIgnoreCase("objectType"))
            continue;
        Class<?> _class = map.get(name);
        JsonElement val = entry.getValue();
        if (val.isJsonPrimitive()) {
            if (_class != null) {
                builder.set(name, context.deserialize(val, _class));
            } else {
                JsonPrimitive prim = val.getAsJsonPrimitive();
                if (prim.isBoolean())
                    builder.set(name, prim.getAsBoolean());
                else if (prim.isNumber())
                    builder.set(name, prim.getAsNumber());
                else {
                    builder.set(name, prim.getAsString());
                }
            }
        } else if (val.isJsonArray()) {
            ImmutableList.Builder<Object> list = ImmutableList.builder();
            JsonArray arr = val.getAsJsonArray();
            processArray(arr, _class, context, list);
            builder.set(name, list.build());
        } else if (val.isJsonObject()) {
            if (map.containsKey(name)) {
                builder.set(name, context.deserialize(val, map.get(name)));
            } else
                builder.set(name, context.deserialize(val, ASObject.class));
        }
    }
    return builder.get();
}

From source file:org.apache.abdera2.activities.io.gson.BaseAdapter.java

License:Apache License

private void processArray(JsonArray arr, Class<?> _class, JsonDeserializationContext context,
        ImmutableList.Builder<Object> list) {
    for (JsonElement mem : arr) {
        if (mem.isJsonPrimitive()) {
            if (_class != null) {
                list.add(context.deserialize(mem, _class));
            } else {
                JsonPrimitive prim2 = (JsonPrimitive) mem;
                if (prim2.isBoolean())
                    list.add(prim2.getAsBoolean());
                else if (prim2.isNumber())
                    list.add(prim2.getAsNumber());
                else
                    list.add(prim2.getAsString());
            }//  w  ww  . java  2  s .co m
        } else if (mem.isJsonObject()) {
            list.add(context.deserialize(mem, _class != null ? _class : ASObject.class));
        } else if (mem.isJsonArray()) {
            JsonArray array = mem.getAsJsonArray();
            ImmutableList.Builder<Object> objs = ImmutableList.builder();
            processArray(array, _class, context, objs);
            list.add(objs.build());
        }
    }
}

From source file:org.apache.abdera2.activities.io.gson.MultimapAdapter.java

License:Apache License

protected static Object primdes(JsonPrimitive prim) {
    if (prim.isBoolean())
        return prim.getAsBoolean();
    else if (prim.isNumber())
        return prim.getAsNumber();
    else/* w  w  w .ja  v  a2s.c o  m*/
        return prim.getAsString();
}