Example usage for com.google.gson JsonNull JsonNull

List of usage examples for com.google.gson JsonNull JsonNull

Introduction

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

Prototype

@Deprecated
public JsonNull() 

Source Link

Document

Creates a new JsonNull object.

Usage

From source file:org.greenrobot.eventbus.EventBus.java

License:Apache License

private Object getValue(Method.Data data, JsonObject json) {
    if (data == null)
        return null;

    String id = data.getId();//  w  w w.ja  v  a2  s.c  om
    Class<?> clazz = data.getType();
    boolean isNull = data.getNull();

    JsonElement value = json == null ? new JsonNull() : json.get(id);
    if (!isNull && value.isJsonNull())
        throw new EventBusException(
                "Method.Data[" + data + "], the id[" + id + "] has null value to give " + data.getType());

    if (!value.isJsonNull()) {
        if (clazz == Boolean.class) {
            return value.getAsBoolean();
        } else if (clazz == Integer.class) {
            return value.getAsInt();
        } else if (clazz == Long.class) {
            return value.getAsLong();
        } else if (clazz == Float.class) {
            return value.getAsFloat();
        } else if (clazz == Double.class) {
            return value.getAsDouble();
        } else if (clazz == String.class) {
            return value.getAsString();
        } else if (clazz == Byte.class) {
            return value.getAsByte();
        } else if (clazz == char.class) {
            return value.getAsCharacter();
        } else {
            return new Gson().fromJson(value, clazz);
        }
    }
    return null;
}

From source file:org.jbpm.formbuilder.server.render.gwt.Renderer.java

License:Apache License

private JsonElement asJsonValue(Object value) {
    if (value == null) {
        return new JsonNull();
    } else if (value.getClass().isArray()) {
        Object[] arr = (Object[]) value;
        JsonArray retval = new JsonArray();
        for (Object sub : arr) {
            retval.add(asJsonValue(sub));
        }/*from   ww  w  .  j  a va2  s.c om*/
        return retval;
    } else if (value instanceof Boolean) {
        return new JsonPrimitive((Boolean) value);
    } else if (value instanceof Number) {
        return new JsonPrimitive((Number) value);
    } else if (value instanceof String) {
        return new JsonPrimitive((String) value);
    } else {
        Map<String, Object> retval = new HashMap<String, Object>();
        Field[] fields = value.getClass().getFields();
        for (Field field : fields) {
            try {
                Object subVal = PropertyUtils.getProperty(value, field.getName());
                retval.put(field.getName(), subVal);
            } catch (Exception e) {
                retval.put(field.getName(), null);
            }
        }
        return toJsonObject(retval);
    }
}

From source file:org.rapla.server.jsonrpc.JsonServlet.java

License:Apache License

private String formatResult(final ActiveCall call) throws UnsupportedEncodingException, IOException {
    final GsonBuilder gb = createGsonBuilder();
    gb.registerTypeAdapter(call.getClass(), new JsonSerializer<ActiveCall>() {
        @Override/*from  w ww  . j a va 2  s.  c o  m*/
        public JsonElement serialize(final ActiveCall src, final Type typeOfSrc,
                final JsonSerializationContext context) {
            if (call.callback != null) {
                if (src.externalFailure != null) {
                    return new JsonNull();
                }
                return context.serialize(src.result);
            }

            final JsonObject r = new JsonObject();
            r.add(src.versionName, src.versionValue);
            if (src.id != null) {
                r.add("id", src.id);
            }
            if (src.xsrfKeyOut != null) {
                r.addProperty("xsrfKey", src.xsrfKeyOut);
            }
            if (src.externalFailure != null) {
                final JsonObject error = new JsonObject();
                if ("jsonrpc".equals(src.versionName)) {
                    final int code = to2_0ErrorCode(src);

                    error.addProperty("code", code);
                    error.addProperty("message", src.externalFailure.getMessage());
                } else {
                    error.addProperty("name", "JSONRPCError");
                    error.addProperty("code", 999);
                    error.addProperty("message", src.externalFailure.getMessage());
                }
                r.add("error", error);
            } else {
                r.add("result", context.serialize(src.result));
            }
            return r;
        }
    });

    final StringWriter o = new StringWriter();
    if (call.callback != null) {
        o.write(call.callback);
        o.write("(");
    }
    gb.create().toJson(call, o);
    if (call.callback != null) {
        o.write(");");
    }
    o.close();
    return o.toString();
}

From source file:org.stjs.server.json.gson.JSArrayAdapter.java

License:Apache License

@Override
public JsonElement serialize(Array<?> array, Type type, JsonSerializationContext ctx) {
    if (array == null) {
        return new JsonNull();
    }//ww w.  j  av a 2s . com
    JsonArray js = new JsonArray();
    for (String i : array) {
        js.add(ctx.serialize(array.$get(i)));
    }
    return js;
}

From source file:org.stjs.server.json.gson.JSDateAdapter.java

License:Apache License

@Override
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
    if (src == null) {
        return new JsonNull();
    }/*from w w w  . j  a v a2 s .co m*/
    return new JsonPrimitive(JSDateUtils.toNormalizedString(src));
}

From source file:org.stjs.server.json.gson.JSMapAdapter.java

License:Apache License

@SuppressWarnings("unchecked")
@Override// ww w.  ja v a2s .co  m
public JsonElement serialize(Map<?, ?> map, Type typeOfSrc, JsonSerializationContext ctx) {
    if (map == null) {
        return new JsonNull();
    }
    JsonObject js = new JsonObject();
    for (Object k : map) {
        js.add(k.toString(), ctx.serialize(((Map) map).$get(k.toString())));
    }
    return js;
}

From source file:org.structr.common.error.PropertiesNotFoundToken.java

License:Open Source License

@Override
public JsonElement getContent() {

    JsonObject obj = new JsonObject();
    JsonObject vals = new JsonObject();

    for (Entry<String, Object> entry : attributes.entrySet()) {

        String key = entry.getKey();
        Object value = entry.getValue();

        if (value == null) {
            vals.add(key, new JsonNull());
        } else {//from  ww w. ja  v a2  s.  c o  m
            vals.add(key, new JsonPrimitive(value.toString()));
        }
    }

    obj.add(getErrorToken(), vals);

    return obj;
}

From source file:org.structr.core.GraphObjectGSONAdapter.java

License:Open Source License

private JsonElement serializeFlatNameValue(GraphObject src, Type typeOfSrc, JsonSerializationContext context,
        String localPropertyView, int depth) {

    // prevent endless recursion by pruning at depth 2
    if (depth > outputNestingDepth) {

        return null;

    }//  w  w  w . j av  a  2s.  c o m

    JsonObject jsonObject = new JsonObject();

    // id (only if idProperty is not set)
    if (idProperty == null) {

        jsonObject.add("id", new JsonPrimitive(src.getId()));

    } else {

        Object idPropertyValue = src.getProperty(idProperty);

        if (idPropertyValue != null) {

            String idString = idPropertyValue.toString();

            jsonObject.add("id", new JsonPrimitive(idString));

        }

    }

    // property keys
    Iterable<String> keys = src.getPropertyKeys(localPropertyView);
    if (keys != null) {
        for (String key : keys) {

            Object value = src.getProperty(key);

            if (value != null) {

                // id property mapping
                if (key.equals(idProperty)) {

                    key = "id";

                }

                if (value instanceof Iterable) {

                    jsonObject.add(key,
                            serializeIterable((Iterable) value, typeOfSrc, context, localPropertyView, depth));

                } else if (value instanceof GraphObject) {

                    GraphObject graphObject = (GraphObject) value;

                    jsonObject.add(key, this.serializeFlatNameValue(graphObject, typeOfSrc, context,
                            localPropertyView, depth + 1));

                } else if (value instanceof Map) {

                    jsonObject.add(key, serializeMap((Map) value, typeOfSrc, context, localPropertyView, false,
                            false, depth));

                } else {

                    //                                      jsonObject.add(key, new JsonPrimitive(value.toString()));
                    jsonObject.add(key, primitive(value));
                }
            } else {

                jsonObject.add(key, new JsonNull());

            }

        }
    }

    return jsonObject;
}

From source file:org.structr.core.GraphObjectGSONAdapter.java

License:Open Source License

private JsonObject serializePrimitive(String key, Object value, boolean includeTypeInOutput) {

    JsonObject property = new JsonObject();

    // id property mapping
    if (key.equals(idProperty)) {

        key = "id";

    }//from   w  w w  .  j  a  v a2s  . co  m

    property.add("key", new JsonPrimitive(key));

    if (value != null) {

        property.add("value", primitive(value));

        // include type?
        if (includeTypeInOutput) {

            String valueType = value.getClass().getSimpleName();

            property.add("type", new JsonPrimitive(valueType));

        }

    } else {

        property.add("value", new JsonNull());

        // include type?
        if (includeTypeInOutput) {

            property.add("type", new JsonNull());

        }

    }

    return property;
}

From source file:org.structr.core.GraphObjectGSONAdapter.java

License:Open Source License

private JsonObject serializeMap(Map<String, Object> map, Type typeOfT, JsonSerializationContext context,
        String localPropertyView, boolean includeType, boolean nested, int depth) {

    JsonObject object = new JsonObject();

    for (Entry<String, Object> entry : map.entrySet()) {

        String key = entry.getKey();
        Object value = entry.getValue();

        if (key != null) {

            // id property mapping
            if (key.equals(idProperty)) {

                key = "id";

            }//from  w w w  .  j  ava 2 s  .  c o m

            if (value != null) {

                // serialize graph objects that are nested in the map..
                if (value instanceof GraphObject) {

                    if (nested) {

                        object.add(key, serializeNestedKeyValueType((GraphObject) value, typeOfT, context,
                                includeType, localPropertyView, depth + 1));

                    } else {

                        object.add(key, serializeFlatNameValue((GraphObject) value, typeOfT, context,
                                localPropertyView, depth + 1));

                    }

                } else if (value instanceof Iterable) {

                    object.add(key,
                            serializeIterable((Iterable) value, typeOfT, context, localPropertyView, depth));

                } else {

                    object.add(key, primitive(value));

                }
            } else {

                object.add(key, new JsonNull());

            }
        }

    }

    return object;
}