Example usage for com.google.gson JsonPrimitive getAsBoolean

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

Introduction

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

Prototype

@Override
public boolean getAsBoolean() 

Source Link

Document

convenience method to get this element as a boolean value.

Usage

From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java

License:Apache License

private static List<KeyValueProto> parseProtoValues(JsonObject valuesObject) {
    List<KeyValueProto> result = new ArrayList<>();
    for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) {
        JsonElement element = valueEntry.getValue();
        if (element.isJsonPrimitive()) {
            JsonPrimitive value = element.getAsJsonPrimitive();
            if (value.isString()) {
                if (maxStringValueLength > 0 && value.getAsString().length() > maxStringValueLength) {
                    String message = String.format(
                            "String value length [%d] for key [%s] is greater than maximum allowed [%d]",
                            value.getAsString().length(), valueEntry.getKey(), maxStringValueLength);
                    throw new JsonSyntaxException(message);
                }/*w  ww  .ja  v  a  2 s . c  om*/
                if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
                    try {
                        result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
                    } catch (RuntimeException th) {
                        result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey())
                                .setType(KeyValueType.STRING_V).setStringV(value.getAsString()).build());
                    }
                } else {
                    result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey())
                            .setType(KeyValueType.STRING_V).setStringV(value.getAsString()).build());
                }
            } else if (value.isBoolean()) {
                result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey())
                        .setType(KeyValueType.BOOLEAN_V).setBoolV(value.getAsBoolean()).build());
            } else if (value.isNumber()) {
                result.add(buildNumericKeyValueProto(value, valueEntry.getKey()));
            } else {
                throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value);
            }
        } else {
            throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element);
        }
    }
    return result;
}

From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java

License:Apache License

private static List<KvEntry> parseValues(JsonObject valuesObject) {
    List<KvEntry> result = new ArrayList<>();
    for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) {
        JsonElement element = valueEntry.getValue();
        if (element.isJsonPrimitive()) {
            JsonPrimitive value = element.getAsJsonPrimitive();
            if (value.isString()) {
                if (maxStringValueLength > 0 && value.getAsString().length() > maxStringValueLength) {
                    String message = String.format(
                            "String value length [%d] for key [%s] is greater than maximum allowed [%d]",
                            value.getAsString().length(), valueEntry.getKey(), maxStringValueLength);
                    throw new JsonSyntaxException(message);
                }//from   ww w .  j  a va 2  s  .c om
                if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) {
                    try {
                        parseNumericValue(result, valueEntry, value);
                    } catch (RuntimeException th) {
                        result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
                    }
                } else {
                    result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString()));
                }
            } else if (value.isBoolean()) {
                result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean()));
            } else if (value.isNumber()) {
                parseNumericValue(result, valueEntry, value);
            } else {
                throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value);
            }
        } else {
            throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element);
        }
    }
    return result;
}

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();
    }//from www. j  a  va  2s .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 . ja  v  a  2 s .  c o m
 * @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  .j a  v  a2 s  .co  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.GetBoolean.java

License:Open Source License

@Override
public BValue[] execute(Context ctx) {
    String jsonPath = null;/*w w  w  .j  ava 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());
        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.isBoolean()) {
                result = new BBoolean(value.getAsBoolean());
            } else {
                throw new BallerinaException("The element matching path: " + jsonPath + " is not a Boolean.");
            }
        } else {
            throw new BallerinaException(
                    "The element matching path: " + jsonPath + " is a JSON, not a Boolean.");
        }
    } 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.xacml4j.v30.marshal.json.GsonUtil.java

License:Open Source License

public static boolean getAsBoolean(JsonObject o, String memberName, boolean defaultValue) {
    JsonPrimitive v = o.getAsJsonPrimitive(memberName);
    return (v != null) ? v.getAsBoolean() : defaultValue;
}

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

License:Apache License

public static NVBase<?> guessPrimitive(String name, NVConfig nvc, JsonPrimitive jp) {
    GNVType gnvType = nvc != null ? GNVType.toGNVType(nvc) : null;

    if (gnvType == null) {
        GNVTypeName tn = GNVType.toGNVTypeName(name, ':');
        if (tn != null) {
            gnvType = tn.getType();//w  w  w  .j  a va2 s . co m
            name = tn.getName();
        }
    }

    if (gnvType != null) {
        switch (gnvType) {
        case NVBLOB:
            try {
                byte value[] = SharedBase64.decode(Base64Type.URL, jp.getAsString());
                return new NVBlob(name, value);
            } catch (Exception e) {

            }
            break;
        case NVBOOLEAN:
            return new NVBoolean(name, jp.getAsBoolean());
        case NVDOUBLE:
            return new NVDouble(name, jp.getAsDouble());
        case NVFLOAT:
            return new NVFloat(name, jp.getAsFloat());
        case NVINT:
            return new NVInt(name, jp.getAsInt());
        case NVLONG:
            return new NVLong(name, jp.getAsLong());

        }
    }

    if (jp.isBoolean()) {
        return new NVBoolean(name, jp.getAsBoolean());
    } else if (jp.isNumber()) {
        // if there is no dots it should be a 
        //if (jp.getAsString().indexOf(".") == -1)
        {
            try {
                Number number = SharedUtil.parseNumber(jp.getAsString());
                return SharedUtil.numberToNVBase(name, number);

            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        //else
        {
            try {
                return new NVDouble(name, jp.getAsDouble());
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    } else if (jp.isString()) {
        try {
            byte value[] = SharedBase64.decodeWrappedAsString(jp.getAsString());
            return new NVBlob(name, value);
        } catch (Exception e) {

        }
        try {
            Long.parseLong(jp.getAsString());
        } catch (Exception e) {
            if (TimestampFilter.SINGLETON.isValid(jp.getAsString())) {
                return new NVLong(name, TimestampFilter.SINGLETON.validate(jp.getAsString()));
            }
        }

        return new NVPair(name, jp.getAsString());
    }

    return null;

}

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());
                            }/*w  w  w .  ja v  a2s  .c om*/
                        }
                    }

                    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 {/*from w  w w .  j a va 2s  .c o  m*/
        throw new IllegalStateException();
    }
}