Example usage for com.google.gson JsonPrimitive isBoolean

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

Introduction

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

Prototype

public boolean isBoolean() 

Source Link

Document

Check whether this primitive contains a boolean value.

Usage

From source file:org.zoxweb.server.util.GSONUtil.java

License:Apache License

public static Map<String, ?> fromJSONMap(String json, Base64Type b64Type) throws APIException {
    Map<String, Object> ret = new LinkedHashMap<String, Object>();

    JsonElement je = new JsonParser().parse(json);

    log.log(Level.FINE, "JSONElement created from json (String): " + je);

    if (je instanceof JsonObject) {
        JsonObject jo = (JsonObject) je;

        for (Entry<String, JsonElement> element : jo.entrySet()) {
            if (!element.getValue().isJsonNull()) {
                if (element.getValue().isJsonArray()) {
                    List<Object> list = new ArrayList<Object>();

                    JsonArray jsonArray = element.getValue().getAsJsonArray();

                    for (int i = 0; i < jsonArray.size(); i++) {
                        if (jsonArray.get(i).isJsonObject()) {
                            NVEntity nve = fromJSON(jsonArray.get(i).getAsJsonObject(), null, b64Type);
                            list.add(nve);
                        } else if (jsonArray.get(i).isJsonPrimitive()) {
                            JsonPrimitive jsonPrimitive = jsonArray.get(i).getAsJsonPrimitive();

                            if (jsonPrimitive.isString()) {
                                list.add(jsonArray.get(i).getAsString());
                            } else if (jsonPrimitive.isBoolean()) {
                                list.add(jsonArray.get(i).getAsBoolean());
                            }//from   ww w .ja  va  2s  . com
                        }
                    }

                    ret.put(element.getKey(), list);
                } else if (element.getValue().isJsonObject()) {
                    NVEntity nve = fromJSON(element.getValue().getAsJsonObject(), null, b64Type);
                    ret.put(element.getKey(), nve);
                } else if (element.getValue().isJsonPrimitive()) {
                    JsonPrimitive jsonPrimitive = element.getValue().getAsJsonPrimitive();

                    if (jsonPrimitive.isString()) {
                        ret.put(element.getKey(), jsonPrimitive.getAsString());
                    } else if (jsonPrimitive.isBoolean()) {
                        ret.put(element.getKey(), jsonPrimitive.getAsBoolean());
                    }
                }
            } else {
                ret.put(element.getKey(), null);
            }
        }
    }

    return ret;
}

From source file:pI.generator.JsonJavaMapper.java

License:Open Source License

public Object json2JavaPrimitive(JsonPrimitive prim) {
    if (prim.isBoolean()) {
        return prim.getAsBoolean();
    } else if (prim.isString()) {
        return prim.getAsString();
    } else if (prim.isNumber()) {
        return prim.getAsNumber();
    } else {//  w ww . j  a  va 2 s. c om
        throw new IllegalStateException();
    }
}

From source file:rpc.server.data.JSONSerializer.java

License:Open Source License

private Object fromJsonElement(JsonElement jsonElement, Type expected) throws NoSuitableSerializableFactory {

    if (jsonElement == null) {
        return null;
    }/* w ww.  j  ava 2s  .  c  om*/

    // Null
    if (jsonElement.isJsonNull()) {
        return null;
    }

    // Boolean
    // Integer
    // Long
    // Float
    // Double
    // String
    // Enum
    if (jsonElement.isJsonPrimitive()) {
        JsonPrimitive asJsonPrimitive = jsonElement.getAsJsonPrimitive();

        if (asJsonPrimitive.isBoolean()) {
            return asJsonPrimitive.getAsBoolean();
        }

        if (asJsonPrimitive.isNumber()) {
            if (expected.isInteger()) {
                return asJsonPrimitive.getAsInt();
            }

            if (expected.isLong()) {
                return asJsonPrimitive.getAsLong();
            }

            if (expected.isFloat()) {
                return asJsonPrimitive.getAsFloat();
            }

            if (expected.isDouble()) {
                return asJsonPrimitive.getAsDouble();
            }

            return asJsonPrimitive.getAsNumber();
        }

        if (asJsonPrimitive.isString()) {
            if (expected.isEnum()) {
                String value = asJsonPrimitive.getAsString();
                return Enum.valueOf((Class) expected.getTypeClass(), value);
            } else {
                return asJsonPrimitive.getAsString();
            }
        }
    }

    // Map
    // Serializable
    if (jsonElement.isJsonObject()) {
        JsonObject asJsonObject = jsonElement.getAsJsonObject();

        if (expected.isMap()) {
            Map<Object, Object> map = new HashMap<Object, Object>();

            Type keyType = expected.getParameterized(0);
            Type valueType = expected.getParameterized(1);

            if (!(keyType.isString() || keyType.isEnum())) {
                return null;
            }

            for (Map.Entry<String, JsonElement> entry : asJsonObject.entrySet()) {

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

                if (keyType.isString()) {
                    map.put(entry.getKey(), fromJsonElement(value, valueType));
                }

                if (keyType.isEnum()) {
                    map.put(Enum.valueOf((Class) keyType.getTypeClass(), key),
                            fromJsonElement(value, valueType));
                }
            }

            return map;
        } else {
            if (provider == null) {
                throw new NoSuitableSerializableFactory();
            }

            Serializable object = provider.make(expected);

            for (Map.Entry<String, Type> entry : object.fields().entrySet()) {

                String field = entry.getKey();
                Type fieldType = entry.getValue();

                JsonElement value = asJsonObject.get(field);
                object.set(field, fromJsonElement(value, fieldType));
            }

            return object;
        }
    }

    // List
    if (jsonElement.isJsonArray()) {
        JsonArray asJsonArray = jsonElement.getAsJsonArray();

        int size = asJsonArray.size();

        List<Object> list = new ArrayList<Object>();
        Type itemType = expected.getParameterized(0);

        for (int i = 0; i < size; i++) {
            JsonElement value = asJsonArray.get(i);
            list.add(fromJsonElement(value, itemType));
        }

        return list;
    }

    return null;
}

From source file:tk.breezy64.pantex.core.ConfigManager.java

private static Object jsonElementToObject(JsonElement e) {
    if (e.isJsonObject()) {
        return jsonObjectToMap(e.getAsJsonObject());
    } else if (e.isJsonPrimitive()) {
        JsonPrimitive p = e.getAsJsonPrimitive();
        if (p.isNumber()) {
            return p.getAsInt();
        } else if (p.isString()) {
            return p.getAsString();
        } else if (p.isBoolean()) {
            return p.getAsBoolean();
        }// w w w.  j a  v  a 2  s. co m
    } else if (e.isJsonArray()) {
        List<Object> list = new ArrayList<>();
        e.getAsJsonArray().forEach((x) -> list.add(jsonElementToObject(x)));
        return list;
    }
    return null;
}