Example usage for com.google.gson JsonPrimitive getAsByte

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

Introduction

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

Prototype

@Override
    public byte getAsByte() 

Source Link

Usage

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonByteTypeAdapter.java

License:Apache License

@Override
public Byte deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    return jsonPrimitive.getAsByte();
}

From source file:com.jayway.jsonpath.internal.spi.mapper.GsonMapper.java

License:Apache License

@Override
public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) {

    assertValidConversion(src, srcType, targetType);

    if (src == null || src.getClass().equals(JsonNull.class)) {
        return null;
    }/*from w w w  .  j a  v  a2  s . c  o m*/

    if (JsonPrimitive.class.isAssignableFrom(srcType)) {

        JsonPrimitive primitive = (JsonPrimitive) src;
        if (targetType.equals(Long.class)) {
            return primitive.getAsLong();
        } else if (targetType.equals(Integer.class)) {
            return primitive.getAsInt();
        } else if (targetType.equals(BigInteger.class)) {
            return primitive.getAsBigInteger();
        } else if (targetType.equals(Byte.class)) {
            return primitive.getAsByte();
        } else if (targetType.equals(BigDecimal.class)) {
            return primitive.getAsBigDecimal();
        } else if (targetType.equals(Double.class)) {
            return primitive.getAsDouble();
        } else if (targetType.equals(Float.class)) {
            return primitive.getAsFloat();
        } else if (targetType.equals(String.class)) {
            return primitive.getAsString();
        } else if (targetType.equals(Boolean.class)) {
            return primitive.getAsBoolean();
        } else if (targetType.equals(Date.class)) {

            if (primitive.isNumber()) {
                return new Date(primitive.getAsLong());
            } else if (primitive.isString()) {
                try {
                    return DateFormat.getInstance().parse(primitive.getAsString());
                } catch (ParseException e) {
                    throw new MappingException(e);
                }
            }
        }

    } else if (JsonObject.class.isAssignableFrom(srcType)) {
        JsonObject srcObject = (JsonObject) src;
        if (targetType.equals(Map.class)) {
            Map<String, Object> targetMap = new LinkedHashMap<String, Object>();
            for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) {
                Object val = null;
                JsonElement element = entry.getValue();
                if (element.isJsonPrimitive()) {
                    val = GsonJsonProvider.unwrap(element);
                } else if (element.isJsonArray()) {
                    val = convert(element, element.getClass(), List.class, conf);
                } else if (element.isJsonObject()) {
                    val = convert(element, element.getClass(), Map.class, conf);
                } else if (element.isJsonNull()) {
                    val = null;
                }
                targetMap.put(entry.getKey(), val);
            }
            return targetMap;
        }

    } else if (JsonArray.class.isAssignableFrom(srcType)) {
        JsonArray srcArray = (JsonArray) src;
        if (targetType.equals(List.class)) {
            List<Object> targetList = new ArrayList<Object>();
            for (JsonElement element : srcArray) {
                if (element.isJsonPrimitive()) {
                    targetList.add(GsonJsonProvider.unwrap(element));
                } else if (element.isJsonArray()) {
                    targetList.add(convert(element, element.getClass(), List.class, conf));
                } else if (element.isJsonObject()) {
                    targetList.add(convert(element, element.getClass(), Map.class, conf));
                } else if (element.isJsonNull()) {
                    targetList.add(null);
                }
            }
            return targetList;
        }
    }

    throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName());
}

From source file:org.eclipse.milo.opcua.binaryschema.gson.JsonStructureCodec.java

License:Open Source License

@Override
protected Object memberTypeToOpcUaScalar(JsonElement member, String typeName) {
    if (member == null || member.isJsonNull()) {
        return null;
    } else if (member.isJsonArray()) {
        JsonArray array = member.getAsJsonArray();

        switch (typeName) {
        case "ByteString": {
            byte[] bs = new byte[array.size()];

            for (int i = 0; i < array.size(); i++) {
                bs[i] = array.get(i).getAsByte();
            }//from   ww w  .  jav  a  2s .co m

            return ByteString.of(bs);
        }

        default:
            return array;
        }
    } else if (member.isJsonObject()) {
        JsonObject jsonObject = member.getAsJsonObject();

        switch (typeName) {
        case "QualifiedName": {
            return new QualifiedName(jsonObject.get("namespaceIndex").getAsInt(),
                    jsonObject.get("name").getAsString());
        }
        case "LocalizedText": {
            return new LocalizedText(jsonObject.get("locale").getAsString(),
                    jsonObject.get("text").getAsString());
        }

        default:
            return jsonObject;
        }
    } else if (member.isJsonPrimitive()) {
        JsonPrimitive primitive = member.getAsJsonPrimitive();

        if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        } else if (primitive.isString()) {
            switch (typeName) {
            case "Guid":
                return UUID.fromString(primitive.getAsString());

            case "NodeId":
                return NodeId.parseSafe(primitive.getAsString()).orElse(NodeId.NULL_VALUE);

            case "ExpandedNodeId":
                return ExpandedNodeId.parse(primitive.getAsString());

            case "XmlElement":
                return new XmlElement(primitive.getAsString());

            default:
                return primitive.getAsString();
            }
        } else if (primitive.isNumber()) {
            switch (typeName) {
            case "SByte":
                return primitive.getAsByte();
            case "Int16":
                return primitive.getAsShort();
            case "Int32":
                return primitive.getAsInt();
            case "Int64":
                return primitive.getAsLong();

            case "Byte":
                return ubyte(primitive.getAsShort());
            case "UInt16":
                return ushort(primitive.getAsInt());
            case "UInt32":
                return uint(primitive.getAsLong());
            case "UInt64":
                return ulong(primitive.getAsBigInteger());

            case "Float":
                return primitive.getAsFloat();
            case "Double":
                return primitive.getAsDouble();

            case "DateTime":
                return new DateTime(primitive.getAsLong());

            case "StatusCode":
                return new StatusCode(primitive.getAsLong());

            default:
                return primitive.getAsNumber();
            }
        }
    }

    return null;
}