Example usage for com.google.gson JsonPrimitive getAsNumber

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

Introduction

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

Prototype

@Override
public Number getAsNumber() 

Source Link

Document

convenience method to get this element as a Number.

Usage

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

License:Open Source License

private static Object fromPrimitive(final JsonPrimitive p) {

    if (p.isNumber()) {

        Number number = p.getAsNumber();
        if (number instanceof Integer) {
            return number.intValue();
        } else {//w  w  w  .  j  a  v  a2 s .c  o  m
            return number.doubleValue();
        }

    }

    return p.getAsString();
}

From source file:org.structr.core.rest.JsonInputGSONAdapter.java

License:Open Source License

public static Object fromPrimitive(final JsonPrimitive p) {

    if (p.isNumber()) {

        Number number = p.getAsNumber();

        // Detect if value is floating point
        if (p.getAsString().contains(".")) {

            return number.doubleValue();

        } else {//ww  w .  j a v a 2  s  . c o m

            return number.longValue();

        }

    } else if (p.isBoolean()) {

        return p.getAsBoolean();

    }

    return p.getAsString();
}

From source file:org.trimou.gson.resolver.JsonElementResolver.java

License:Apache License

private Object unwrapJsonPrimitive(JsonPrimitive jsonPrimitive) {
    if (jsonPrimitive.isBoolean()) {
        return jsonPrimitive.getAsBoolean();
    } else if (jsonPrimitive.isString()) {
        return jsonPrimitive.getAsString();
    } else if (jsonPrimitive.isNumber()) {
        return jsonPrimitive.getAsNumber();
    }/* w w w .j  a  v  a2  s.  c  o  m*/
    return jsonPrimitive;
}

From source file:org.tsukuba_bunko.lilac.web.controller.JsonRequestParser.java

License:Open Source License

/**
 * JavaScript?(?,,)Java?????/*from  w w  w. j a v a 2 s  .c  om*/
 * @param jsonPrimitiveJavaScript?
 * @returnJava
 */
public Object getJavaObject(JsonPrimitive jsonPrimitive) {
    if (jsonPrimitive.isBoolean()) {
        return jsonPrimitive.getAsBoolean();
    } else if (jsonPrimitive.isNumber()) {
        return jsonPrimitive.getAsNumber();
    } else {
        return jsonPrimitive.getAsString();
    }
}

From source file:org.tuleap.mylyn.task.core.internal.parser.AbstractTuleapDeserializer.java

License:Open Source License

/**
 * {@inheritDoc}/*from w  w  w.java  2 s  .c o  m*/
 *
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type,
 *      com.google.gson.JsonDeserializationContext)
 */
@Override
public T deserialize(JsonElement rootJsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = rootJsonElement.getAsJsonObject();
    T pojo = super.deserialize(rootJsonElement, type, context);
    JsonElement jsonTrackerRef = jsonObject.get(ITuleapConstants.JSON_TRACKER);
    TuleapReference trackerRef = context.deserialize(jsonTrackerRef, TuleapReference.class);
    pojo.setTracker(trackerRef);

    JsonArray fields = jsonObject.get(ITuleapConstants.VALUES).getAsJsonArray();
    for (JsonElement field : fields) {
        JsonObject jsonField = field.getAsJsonObject();
        int fieldId = jsonField.get(ITuleapConstants.FIELD_ID).getAsInt();
        if (jsonField.has(ITuleapConstants.FIELD_VALUE)) {
            JsonElement jsonValue = jsonField.get(ITuleapConstants.FIELD_VALUE);
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isString()) {
                    pojo.addFieldValue(new LiteralFieldValue(fieldId, primitive.getAsString()));
                } else if (primitive.isNumber()) {
                    pojo.addFieldValue(new LiteralFieldValue(fieldId, String.valueOf(primitive.getAsNumber())));
                } else if (primitive.isBoolean()) {
                    pojo.addFieldValue(
                            new LiteralFieldValue(fieldId, String.valueOf(primitive.getAsBoolean())));
                }
            }
        } else if (jsonField.has(ITuleapConstants.FIELD_BIND_VALUE_ID)
                && !jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_ID).isJsonNull()) {
            // sb?
            JsonElement jsonBindValueId = jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_ID);
            int bindValueId = jsonBindValueId.getAsInt();
            pojo.addFieldValue(new BoundFieldValue(fieldId, Lists.newArrayList(Integer.valueOf(bindValueId))));
        } else if (jsonField.has(ITuleapConstants.FIELD_BIND_VALUE_IDS)
                && !jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_IDS).isJsonNull()) {
            // sb?, msb, cb, or tbl (open list)
            JsonElement jsonBindValueIds = jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_IDS);
            JsonArray jsonIds = jsonBindValueIds.getAsJsonArray();
            if (jsonIds.size() > 0) {
                JsonPrimitive firstElement = jsonIds.get(0).getAsJsonPrimitive();
                if (firstElement.isString()) {
                    // Open list (tbl)
                    List<String> bindValueIds = new ArrayList<String>();
                    for (JsonElement idElement : jsonIds) {
                        bindValueIds.add(idElement.getAsString());
                    }
                    pojo.addFieldValue(new OpenListFieldValue(fieldId, bindValueIds));
                } else {
                    List<Integer> bindValueIds = new ArrayList<Integer>();
                    for (JsonElement idElement : jsonIds) {
                        bindValueIds.add(Integer.valueOf(idElement.getAsInt()));
                    }
                    pojo.addFieldValue(new BoundFieldValue(fieldId, bindValueIds));
                }
            } else {
                pojo.addFieldValue(new BoundFieldValue(fieldId, Collections.<Integer>emptyList()));
            }
        } else if (jsonField.has(ITuleapConstants.FIELD_LINKS)
                && !jsonField.get(ITuleapConstants.FIELD_LINKS).isJsonNull()) {
            // Artifact links
            pojo.addFieldValue(
                    context.<ArtifactLinkFieldValue>deserialize(jsonField, ArtifactLinkFieldValue.class));
        } else if (jsonField.has(ITuleapConstants.FILE_DESCRIPTIONS)
                && jsonField.get(ITuleapConstants.FILE_DESCRIPTIONS).isJsonArray()) {
            AttachmentFieldValue value = context.deserialize(jsonField, AttachmentFieldValue.class);
            pojo.addFieldValue(value);
        }

    }

    return pojo;
}

From source file:org.wso2.ballerina.nativeimpl.lang.json.GetDouble.java

License:Open Source License

@Override
public BValue[] execute(Context ctx) {
    String jsonPath = null;//ww w .  j  a v  a 2  s. c  o  m
    BValue result = null;
    try {
        // Accessing Parameters.
        BJSON json = (BJSON) getArgument(ctx, 0);
        jsonPath = getArgument(ctx, 1).stringValue();

        // Getting the value from JSON
        ReadContext jsonCtx = JsonPath.parse(json.value());
        Object elementObj = jsonCtx.read(jsonPath);
        if (elementObj == null) {
            throw new BallerinaException("No matching element found for jsonpath: " + jsonPath);
        } else if (elementObj instanceof JsonElement) {
            JsonElement element = (JsonElement) elementObj;
            if (element.isJsonPrimitive()) {
                // if the resulting value is a primitive, return the respective primitive value object
                JsonPrimitive value = element.getAsJsonPrimitive();
                if (value.isNumber()) {
                    Number number = value.getAsNumber();
                    if (number instanceof Float || number instanceof Double) {
                        result = new BDouble(number.doubleValue());
                    } else {
                        throw new BallerinaException(
                                "The element matching path: " + jsonPath + " is not a Double.");
                    }
                } else {
                    throw new BallerinaException(
                            "The element matching path: " + jsonPath + " is not a Double.");
                }
            } else {
                throw new BallerinaException(
                        "The element matching path: " + jsonPath + " is a JSON, not a Double.");
            }
        } else if (elementObj instanceof Double) {
            // this handles the JsonPath's min(), max(), avg(), stddev() function
            result = new BDouble((Double) elementObj);
        }
    } catch (PathNotFoundException e) {
        ErrorHandler.handleNonExistingJsonpPath(OPERATION, jsonPath, e);
    } catch (InvalidPathException e) {
        ErrorHandler.handleInvalidJsonPath(OPERATION, e);
    } catch (JsonPathException e) {
        ErrorHandler.handleJsonPathException(OPERATION, e);
    } catch (Throwable e) {
        ErrorHandler.handleJsonPathException(OPERATION, e);
    }

    // Setting output value.
    return getBValues(result);
}

From source file:org.wso2.ballerina.nativeimpl.lang.json.GetFloat.java

License:Open Source License

@Override
public BValue[] execute(Context ctx) {
    String jsonPath = null;/* ww  w. j a v a  2s . co m*/
    BValue result = null;

    try {
        // Accessing Parameters.
        BJSON json = (BJSON) getArgument(ctx, 0);
        jsonPath = getArgument(ctx, 1).stringValue();

        // Getting the value from JSON
        ReadContext jsonCtx = JsonPath.parse(json.value());
        JsonElement element = jsonCtx.read(jsonPath);
        if (element == null) {
            throw new BallerinaException("No matching element found for jsonpath: " + jsonPath);
        } else if (element.isJsonPrimitive()) {
            // if the resulting value is a primitive, return the respective primitive value object
            JsonPrimitive value = element.getAsJsonPrimitive();
            if (value.isNumber()) {
                Number number = value.getAsNumber();
                if (number instanceof Float || number instanceof Double) {
                    result = new BFloat(number.floatValue());
                } else {
                    throw new BallerinaException("The element matching path: " + jsonPath + " is not a Float.");
                }
            } else {
                throw new BallerinaException("The element matching path: " + jsonPath + " is not a Float.");
            }
        } else {
            throw new BallerinaException("The element matching path: " + jsonPath + " is a JSON, not a Float.");
        }
    } catch (PathNotFoundException e) {
        ErrorHandler.handleNonExistingJsonpPath(OPERATION, jsonPath, e);
    } catch (InvalidPathException e) {
        ErrorHandler.handleInvalidJsonPath(OPERATION, e);
    } catch (JsonPathException e) {
        ErrorHandler.handleJsonPathException(OPERATION, e);
    } catch (Throwable e) {
        ErrorHandler.handleJsonPathException(OPERATION, e);
    }

    // Setting output value.
    return getBValues(result);
}

From source file:org.wso2.ballerina.nativeimpl.lang.json.GetInt.java

License:Open Source License

@Override
public BValue[] execute(Context ctx) {
    String jsonPath = null;/*ww  w .j a  va2 s  . co m*/
    BValue result = null;
    try {
        // Accessing Parameters.
        BJSON json = (BJSON) getArgument(ctx, 0);
        jsonPath = getArgument(ctx, 1).stringValue();

        // Getting the value from JSON
        ReadContext jsonCtx = JsonPath.parse(json.value());
        Object elementObj = jsonCtx.read(jsonPath);
        if (elementObj == null) {
            throw new BallerinaException("No matching element found for jsonpath: " + jsonPath);
        } else if (elementObj instanceof JsonElement) {

            JsonElement element = (JsonElement) elementObj;

            if (element.isJsonPrimitive()) {
                // if the resulting value is a primitive, return the respective primitive value object
                JsonPrimitive value = element.getAsJsonPrimitive();
                if (value.isNumber()) {
                    Number number = value.getAsNumber();
                    if (number instanceof Integer | number instanceof Long | number instanceof Short) {
                        result = new BInteger(number.intValue());
                    } else {
                        throw new BallerinaException(
                                "The element matching path: " + jsonPath + " is not an Integer.");
                    }
                } else {
                    throw new BallerinaException(
                            "The element matching path: " + jsonPath + " is not an Integer.");
                }
            } else {
                throw new BallerinaException(
                        "The element matching path: " + jsonPath + " is a JSON, not an Integer.");
            }
        } else if (elementObj instanceof Integer) {
            // this handles the JsonPath's length() function
            result = new BInteger((Integer) elementObj);
        }
    } catch (PathNotFoundException e) {
        ErrorHandler.handleNonExistingJsonpPath(OPERATION, jsonPath, e);
    } catch (InvalidPathException e) {
        ErrorHandler.handleInvalidJsonPath(OPERATION, e);
    } catch (JsonPathException e) {
        ErrorHandler.handleJsonPathException(OPERATION, e);
    } catch (Throwable e) {
        ErrorHandler.handleJsonPathException(OPERATION, e);
    }

    // Setting output value.
    return getBValues(result);
}

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 {//from   w  w w . j  ava  2  s. c  o m
        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;
    }/*from  ww w  .  j  a v a  2  s.co m*/

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