Example usage for com.google.gson JsonElement getAsJsonPrimitive

List of usage examples for com.google.gson JsonElement getAsJsonPrimitive

Introduction

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

Prototype

public JsonPrimitive getAsJsonPrimitive() 

Source Link

Document

convenience method to get this element as a JsonPrimitive .

Usage

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

License:Open Source License

@Override
public BValue[] execute(Context ctx) {
    String jsonPath = null;//from   w w  w.  java 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;/*from   www .j  a  va  2  s.  com*/
    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;/*from  w w w .  j  a  va 2  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:org.wso2.ballerina.nativeimpl.lang.json.GetString.java

License:Open Source License

@Override
public BValue[] execute(Context ctx) {
    String jsonPath = null;//from  ww w .  ja va2  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());
        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.isString()) {
                result = new BString(value.getAsString());
            } else {
                throw new BallerinaException("The element matching path: " + jsonPath + " is not a String.");
            }
        } else {
            throw new BallerinaException("The element matching path: " + jsonPath + " is not a String.");
        }
    } 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.carbon.identity.entitlement.endpoint.util.JSONRequestParser.java

License:Open Source License

/**
 * Private methods used by the parser to convert a given <code>{@link JsonObject}</code>
 * to a Balana <code>{@link Attribute}</code>
 *
 * @param jsonObject <code>{@link JsonObject}</code> representing the Attributes
 * @return <code>{@link Attribute}</code>
 * @throws RequestParseException/*from  ww w .  j a  v  a  2s .c  o m*/
 * @throws UnknownIdentifierException
 */
private static Attribute jsonObjectToAttribute(JsonObject jsonObject)
        throws RequestParseException, UnknownIdentifierException {
    URI id = null;
    URI type = stringAttributeToURI(EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_STRING);
    boolean includeInResult = false;
    String issuer = null;
    List<AttributeValue> attributeValues = new ArrayList<>();

    Set<Map.Entry<String, JsonElement>> properties = jsonObject.entrySet();
    for (Map.Entry<String, JsonElement> property : properties) {
        if (property.getValue().isJsonPrimitive()) {
            switch (property.getKey()) {
            case EntitlementEndpointConstants.ATTRIBUTE_ID:
                id = stringAttributeToURI(property.getValue().getAsString());
                break;

            case EntitlementEndpointConstants.ATTRIBUTE_ISSUER:
                issuer = property.getValue().getAsString();
                break;

            case EntitlementEndpointConstants.ATTRIBUTE_INCLUDE_IN_RESULT:
                includeInResult = property.getValue().getAsBoolean();
                break;

            case EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE:
                type = stringAttributeToURI(property.getValue().getAsString());
                break;

            case EntitlementEndpointConstants.ATTRIBUTE_VALUE:
                URI dataType = stringAttributeToURI(
                        jsonElementToDataType(property.getValue().getAsJsonPrimitive()));

                //If a recognizable data type is given, it should replace the above
                if (type.equals(stringAttributeToURI(EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_STRING))
                        && dataType != null) {
                    type = dataType;
                }

                attributeValues.add(getAttributeValue(property.getValue().getAsString(), dataType, type));
            }
        } else if (property.getValue().isJsonArray()) {
            if (property.getKey().equals(EntitlementEndpointConstants.ATTRIBUTE_VALUE)) {
                JsonArray valueArray = property.getValue().getAsJsonArray();
                for (JsonElement value : valueArray) {
                    if (value.isJsonPrimitive()) {
                        //check if each value's data type can be determined
                        URI dataType = stringAttributeToURI(jsonElementToDataType(value.getAsJsonPrimitive()));
                        attributeValues.add(getAttributeValue(value.getAsString(), dataType, type));
                    }
                }
            }

            /*
            Todo: Spec mentions resolve the type by checking all elements at the end
             */
        }
    }

    if (id == null) {
        throw new RequestParseException("Attribute Id should be set");
    }

    if (attributeValues.isEmpty()) {
        throw new RequestParseException("Attribute should have at least one value");
    }

    return new Attribute(id, type, issuer, null, attributeValues, includeInResult,
            XACMLConstants.XACML_VERSION_3_0);
}

From source file:org.wso2.developerstudio.datamapper.diagram.schemagen.util.SchemaBuilder.java

License:Open Source License

private static TypeEnum RealTypeOf(JsonElement element) {
    if (element == null || element.isJsonNull()) {
        return TypeEnum.NULL;
    } else if (element.isJsonArray()) {
        return TypeEnum.ARRAY;
    } else if (element.isJsonObject()) {
        return TypeEnum.OBJECT;
    } else if (element.isJsonPrimitive()) {
        JsonPrimitive p = element.getAsJsonPrimitive();
        if (p.isNumber()) {
            return TypeEnum.NUMBER;
        } else if (p.isBoolean()) {
            return TypeEnum.BOOLEAN;
        } else if (p.isString()) {
            String value = p.getAsString();
            if (StringUtils.isNotEmpty(value)) {
                return TypeEnum.STRING;
            } else {
                return TypeEnum.NULL;
            }/* www .ja v  a2  s  . com*/
        }
    }
    return TypeEnum.UNDEFINED;
}

From source file:org.xacml4j.v30.marshal.json.AttributesReferenceAdapter.java

License:Open Source License

@Override
public CategoryReference deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    String referenceId = json.getAsJsonPrimitive().getAsString();
    return CategoryReference.builder().id(referenceId).build();
}

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

License:Apache License

private static NVBase<?> guessNVBaseArray(JsonArray ja) {
    NVBase<?> ret = null;//w  w  w.j a  v a 2 s.c  o  m

    GNVType guess = null;
    for (int i = 0; i < ja.size(); i++) {
        JsonElement je = ja.get(i);

        if (je.isJsonObject()) {
            // could an NVEntity or NVPairList or NVGenericMap
            // nvpair
            JsonObject jo = je.getAsJsonObject();
            if (jo.size() == 1) {

                if (ret == null) {
                    return new NVPairList(null, new ArrayList<NVPair>());
                }
            }

            if (jo.size() > 1) {
                return new NVGenericMapList();

            }
        } else if (je.isJsonPrimitive()) {
            if (je.getAsJsonPrimitive().isString()) {
                // must be fixed
                //break;
                return new NVStringList();
            }

            GNVType gnv = GNVType.toGNVType(je.getAsNumber());
            if (gnv != null) {
                if (guess == null) {
                    guess = gnv;
                } else {
                    switch (gnv) {

                    case NVDOUBLE:
                        if (guess == GNVType.NVINT || guess == GNVType.NVLONG || guess == GNVType.NVFLOAT) {
                            guess = gnv;
                        }
                        break;
                    case NVFLOAT:
                        if (guess == GNVType.NVINT || guess == GNVType.NVLONG) {
                            guess = gnv;
                        }
                        break;
                    case NVINT:
                        break;
                    case NVLONG:
                        if (guess == GNVType.NVINT) {
                            guess = gnv;
                        }
                        break;
                    default:
                        break;

                    }
                }
            }
        }
    }

    if (ret == null && guess != null) {
        switch (guess) {

        case NVDOUBLE:
            ret = new NVDoubleList(null, new ArrayList<Double>());
            break;
        case NVFLOAT:
            ret = new NVFloatList(null, new ArrayList<Float>());
            break;
        case NVINT:
            ret = new NVIntList(null, new ArrayList<Integer>());
            break;
        case NVLONG:
            ret = new NVLongList(null, new ArrayList<Long>());
            break;
        default:
            break;

        }
    }

    return ret;
}

From source file:pI.generator.JsonJavaMapper.java

License:Open Source License

public Object json2Java(JsonElement elem) {
    if (elem.isJsonArray()) {
        return jsonArrayAsList(elem.getAsJsonArray());
    } else if (elem.isJsonPrimitive()) {
        return json2JavaPrimitive(elem.getAsJsonPrimitive());
    } else {//w  ww. j  av a2  s  . c  om
        return jsonObject2Map(elem.getAsJsonObject());
    }
}

From source file:ro.agitman.customserializer.utils.DateDeserializer.java

@Override
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    SimpleDateFormat sdf = new SimpleDateFormat(JsonName.DATE_FORMAT);
    Date date = null;/*  w  w  w .  jav  a  2 s  .c o m*/
    try {
        date = sdf.parse(json.getAsJsonPrimitive().getAsString());
        return date;
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }
}