Example usage for com.google.gson JsonElement isJsonPrimitive

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

Introduction

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

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java

License:Apache License

public static Object unwrap(Object o) {

    if (o == null) {
        return null;
    }//from   w ww .j a  va  2 s  .co m
    if (!(o instanceof JsonElement)) {
        return o;
    }

    JsonElement e = (JsonElement) o;

    if (e.isJsonNull()) {
        return null;
    } else if (e.isJsonPrimitive()) {

        JsonPrimitive p = e.getAsJsonPrimitive();
        if (p.isString()) {
            return p.getAsString();
        } else if (p.isBoolean()) {
            return p.getAsBoolean();
        } else if (p.isNumber()) {
            return unwrapNumber(p.getAsNumber());
        }
    }
    return o;
}

From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java

License:Apache License

@Override
public int length(Object obj) {
    if (isArray(obj)) {
        return toJsonArray(obj).size();
    } else if (isMap(obj)) {
        return toJsonObject(obj).entrySet().size();
    } else {/*from w  w w.j  a v a 2 s. c om*/
        if (obj instanceof JsonElement) {
            JsonElement element = toJsonElement(obj);
            if (element.isJsonPrimitive()) {
                return element.toString().length();
            }
        }
    }
    throw new JsonPathException(
            "length operation can not applied to " + obj != null ? obj.getClass().getName() : "null");
}

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;
    }//  w ww . ja va  2  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:com.jayway.jsonpath.spi.json.GsonJsonProvider.java

License:Apache License

public Object unwrap(final Object o) {

    if (o == null) {
        return null;
    }/*from   w ww . j  av  a2s  . c  o  m*/

    if (!(o instanceof JsonElement)) {
        return o;
    }

    JsonElement e = (JsonElement) o;

    if (e.isJsonNull()) {
        return null;
    } else if (e.isJsonPrimitive()) {

        JsonPrimitive p = e.getAsJsonPrimitive();
        if (p.isString()) {
            return p.getAsString();
        } else if (p.isBoolean()) {
            return p.getAsBoolean();
        } else if (p.isNumber()) {
            return unwrapNumber(p.getAsNumber());
        }
    }

    return o;
}

From source file:com.jayway.jsonpath.spi.json.GsonJsonProvider.java

License:Apache License

@Override
public int length(final Object obj) {
    if (isArray(obj)) {
        return toJsonArray(obj).size();
    } else if (isMap(obj)) {
        return toJsonObject(obj).entrySet().size();
    } else {/*from  ww  w.j  a va2  s  . co m*/
        if (obj instanceof JsonElement) {
            JsonElement element = toJsonElement(obj);
            if (element.isJsonPrimitive()) {
                return element.toString().length();
            }
        }
    }

    throw new JsonPathException(
            "length operation can not applied to " + obj != null ? obj.getClass().getName() : "null");
}

From source file:com.king.tratt.FunctionFactoryProvider.java

License:Apache License

static FunctionFactory jsonField() {
    return new FunctionFactory() {
        Value pathValue;/*from w  w  w .j  a  v a  2  s .c  o  m*/
        Value jsonValue;
        JsonParser jsonParser;

        @Override
        public String getName() {
            return "jsonfield";
        }

        @Override
        public int getNumberOfArguments() {
            return 2;
        }

        @Override
        public Value create(List<Value> arguments) {
            pathValue = arguments.get(0);
            jsonValue = arguments.get(1);
            jsonParser = new JsonParser();

            return new Value() {

                @Override
                public String toString() {
                    return String.format("jsonfield('%s', '%s')", pathValue, jsonValue);
                }

                @Override
                public String toDebugString(Event e, Context context) {
                    return util.format(e, context, "[[source:jsonfield('~v', '~v')]]~q", pathValue, jsonValue,
                            this);
                }

                @Override
                protected Object getImp(Event e, Context context) {
                    String path = pathValue.asString(e, context);
                    String json = jsonValue.asString(e, context);

                    String result = null;
                    try {
                        result = getJsonFieldValue(path, json);
                    } catch (Throwable t) {
                        result = "[@ERROR malformed json string]";
                    }
                    return util.parseSupportedType(result);
                }

                private String getJsonFieldValue(String path, String json) {
                    JsonElement jsonElem = jsonParser.parse(json);
                    for (String subPath : path.split("\\.")) {
                        JsonObject jsonObj = jsonElem.getAsJsonObject();
                        jsonElem = jsonObj.get(subPath);
                        if (jsonElem == null) {
                            return String.format("[@ERROR incorrect json path: '%s']", path);
                        }
                    }
                    if (jsonElem.isJsonPrimitive()) {
                        return jsonElem.getAsString();
                    }
                    return jsonElem.toString();

                }
            };
        }
    };
}

From source file:com.kotcrab.vis.editor.serializer.json.ArrayJsonSerializer.java

License:Apache License

@Override
public JsonElement serialize(Array<T> array, Type typeOfSrc, JsonSerializationContext context) {
    JsonArray jsonArray = new JsonArray();
    for (T element : array) {
        JsonElement jsonElement = context.serialize(element);

        if (jsonElement.isJsonObject()) {
            GsonUtils.appendClassProperty(jsonElement.getAsJsonObject(), element, context);
        } else if (jsonElement.isJsonPrimitive()) {
            JsonObject jsonNestedPrimitive = new JsonObject();
            jsonNestedPrimitive.add(PRIMITIVE_CONTENT, jsonElement);
            GsonUtils.appendClassProperty(jsonNestedPrimitive, element, context);
            jsonElement = jsonNestedPrimitive;
        } else if (jsonElement instanceof JsonArray) {
            throw new UnsupportedOperationException("Nested Arrays are not supported by ArrayJsonSerializer");
        }/* w  w w  .j  a  v a2 s.c  o  m*/

        jsonArray.add(jsonElement);
    }

    return jsonArray;
}

From source file:com.liferay.hackaday.lunchhangout.model.Poll.java

License:Open Source License

public String getVotesCount() {
    int votesCount = 0;

    JsonElement votes = getVotes();

    if (votes != null) {
        if (votes.isJsonPrimitive()) {
            votesCount = 1;/*w  ww . j  ava  2  s  .c  o  m*/
        } else {
            votesCount = votes.getAsJsonArray().size();
        }
    }

    return String.valueOf(votesCount);
}

From source file:com.ls.util.internal.ObjectComparator.java

License:Open Source License

private static @Nullable Object getDifferencesObject(JsonElement origin, JsonElement patched) {
    if (origin != null && origin.equals(patched)) {
        return UNCHANGED;
    }//from  w  w  w .  ja va2s.c om

    if (patched == null || patched.isJsonNull()) {
        return null;
    }

    if (origin == null || origin.isJsonNull()) {
        return convertElementToStringRepresentation(patched);
    }

    if (origin.isJsonArray()) {
        if (patched.isJsonArray()) {
            return getDifferencesForArrays((JsonArray) origin, (JsonArray) patched);
        } else {
            return convertElementToStringRepresentation(patched);
        }
    }

    if (origin.isJsonObject()) {
        if (patched.isJsonObject()) {
            return getDifferencesMapForObjects((JsonObject) origin, (JsonObject) patched);
        } else {
            return convertElementToStringRepresentation(patched);
        }
    }

    if (origin.isJsonPrimitive()) {
        if (patched.isJsonPrimitive()) {
            return getDifferencesForPrimitives((JsonPrimitive) origin, (JsonPrimitive) patched);
        } else {
            return convertElementToStringRepresentation(patched);
        }
    }

    return convertElementToStringRepresentation(patched);
}

From source file:com.ls.util.internal.ObjectComparator.java

License:Open Source License

private static Object convertElementToStringRepresentation(JsonElement source) {
    if (source.isJsonNull()) {
        return null;
    }// w w  w  . java  2  s.c om

    if (source.isJsonPrimitive()) {
        return source.toString();
    }

    if (source.isJsonObject()) {
        return getMapFromJsonElement((JsonObject) source);
    }

    if (source.isJsonArray()) {
        return getListFromJsonElement((JsonArray) source);
    }

    return null;
}