Example usage for com.google.gson JsonPrimitive isBoolean

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

Introduction

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

Prototype

public boolean isBoolean() 

Source Link

Document

Check whether this primitive contains a boolean value.

Usage

From source file:org.apache.hadoop.hive.json.JsonSchemaFinder.java

License:Apache License

private static HiveType pickType(JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive prim = (JsonPrimitive) json;
        if (prim.isBoolean()) {
            return new BooleanType();
        } else if (prim.isNumber()) {
            BigDecimal dec = prim.getAsBigDecimal();
            if (dec.scale() > 0) {
                return new FloatingPointType(dec.doubleValue());
            } else {
                return new IntegerType(dec.longValue());
            }//from  w ww.  ja v  a 2  s  .com
        } else {
            String str = prim.getAsString();
            if (DATE_PATTERN.matcher(str).matches()) {
                return new TimestampType();
            } else if (HEX_PATTERN.matcher(str).matches()) {
                return new BinaryType();
            } else {
                return new StringType();
            }
        }
    } else if (json.isJsonNull()) {
        return new NullType();
    } else if (json.isJsonArray()) {
        ListType result = new ListType();
        for (JsonElement child : ((JsonArray) json)) {
            HiveType sub = pickType(child);
            if (result.elementType == null) {
                result.elementType = sub;
            } else {
                result.elementType = mergeType(result.elementType, sub);
            }
        }
        return result;
    } else {
        JsonObject obj = (JsonObject) json;
        StructType result = new StructType();
        for (Map.Entry<String, JsonElement> field : obj.entrySet()) {
            String fieldName = field.getKey();
            HiveType type = pickType(field.getValue());
            result.fields.put(fieldName, type);
        }
        StringBuilder builder = new StringBuilder();
        boolean first = true;
        for (String key : result.fields.keySet()) {
            if (first) {
                first = false;
            } else {
                builder.append(",");
            }
            builder.append(key);
        }
        result.values.add(builder.toString());
        return result;
    }
}

From source file:org.apache.hadoop.hive.json.JsonShredder.java

License:Apache License

private void shredObject(String name, JsonElement json) throws IOException {
    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = (JsonPrimitive) json;
        if (primitive.isBoolean()) {
            getFile(name).append(primitive.getAsBoolean() + "\n");
        } else if (primitive.isString()) {
            getFile(name).append(primitive.getAsString().replace("\\", "\\\\").replace("\n", "\\n") + "\n");
        } else if (primitive.isNumber()) {
            getFile(name).append(primitive.getAsNumber() + "\n");
        }//from w ww .  j  ava2s .c om
    } else if (json.isJsonNull()) {
        // just skip it
    } else if (json.isJsonArray()) {
        for (JsonElement child : ((JsonArray) json)) {
            shredObject(name, child);
        }
    } else {
        JsonObject obj = (JsonObject) json;
        for (Map.Entry<String, JsonElement> field : obj.entrySet()) {
            String fieldName = field.getKey();
            shredObject(name + "." + fieldName, field.getValue());
        }
    }
}

From source file:org.apache.lens.driver.es.client.jest.JestResultSetTransformer.java

License:Apache License

private Type getDataType(int colPosition, JsonElement jsonObjectValue) {
    if (columnDataTypes.get(colPosition) != Type.NULL_TYPE) {
        return columnDataTypes.get(colPosition);
    }// ww  w.j  a  v a  2s.co  m

    final JsonPrimitive jsonPrimitive = jsonObjectValue.getAsJsonPrimitive();
    if (jsonPrimitive.isJsonNull()) {
        return Type.NULL_TYPE;
    }

    final Type type;
    if (jsonPrimitive.isBoolean()) {
        type = Type.BOOLEAN_TYPE;
    } else if (jsonPrimitive.isNumber()) {
        type = Type.DOUBLE_TYPE;
    } else {
        type = Type.STRING_TYPE;
    }
    columnDataTypes.set(colPosition, type);
    return type;
}

From source file:org.apache.orc.tools.json.JsonSchemaFinder.java

License:Apache License

static HiveType pickType(JsonElement json) {
    if (json.isJsonPrimitive()) {
        JsonPrimitive prim = (JsonPrimitive) json;
        if (prim.isBoolean()) {
            return new BooleanType();
        } else if (prim.isNumber()) {
            Matcher matcher = DECIMAL_PATTERN.matcher(prim.getAsString());
            if (matcher.matches()) {
                int intDigits = matcher.group("int").length();
                String fraction = matcher.group("fraction");
                int scale = fraction == null ? 0 : fraction.length();
                if (scale == 0) {
                    if (intDigits < 19) {
                        long value = prim.getAsLong();
                        if (value >= -128 && value < 128) {
                            return new NumericType(HiveType.Kind.BYTE, intDigits, scale);
                        } else if (value >= -32768 && value < 32768) {
                            return new NumericType(HiveType.Kind.SHORT, intDigits, scale);
                        } else if (value >= -2147483648 && value < 2147483648L) {
                            return new NumericType(HiveType.Kind.INT, intDigits, scale);
                        } else {
                            return new NumericType(HiveType.Kind.LONG, intDigits, scale);
                        }//from  ww  w. ja  v  a 2 s .  co  m
                    } else if (intDigits == 19) {
                        // at 19 digits, it may fit inside a long, but we need to check
                        BigInteger val = prim.getAsBigInteger();
                        if (val.compareTo(MIN_LONG) >= 0 && val.compareTo(MAX_LONG) <= 0) {
                            return new NumericType(HiveType.Kind.LONG, intDigits, scale);
                        }
                    }
                }
                if (intDigits + scale <= MAX_DECIMAL_DIGITS) {
                    return new NumericType(HiveType.Kind.DECIMAL, intDigits, scale);
                }
            }
            double value = prim.getAsDouble();
            if (value >= Float.MIN_VALUE && value <= Float.MAX_VALUE) {
                return new NumericType(HiveType.Kind.FLOAT, 0, 0);
            } else {
                return new NumericType(HiveType.Kind.DOUBLE, 0, 0);
            }
        } else {
            String str = prim.getAsString();
            if (TIMESTAMP_PATTERN.matcher(str).matches()) {
                return new StringType(HiveType.Kind.TIMESTAMP);
            } else if (HEX_PATTERN.matcher(str).matches()) {
                return new StringType(HiveType.Kind.BINARY);
            } else {
                return new StringType(HiveType.Kind.STRING);
            }
        }
    } else if (json.isJsonNull()) {
        return new NullType();
    } else if (json.isJsonArray()) {
        ListType result = new ListType();
        result.elementType = new NullType();
        for (JsonElement child : ((JsonArray) json)) {
            HiveType sub = pickType(child);
            if (result.elementType.subsumes(sub)) {
                result.elementType.merge(sub);
            } else if (sub.subsumes(result.elementType)) {
                sub.merge(result.elementType);
                result.elementType = sub;
            } else {
                result.elementType = new UnionType(result.elementType, sub);
            }
        }
        return result;
    } else {
        JsonObject obj = (JsonObject) json;
        StructType result = new StructType();
        for (Map.Entry<String, JsonElement> field : obj.entrySet()) {
            String fieldName = field.getKey();
            HiveType type = pickType(field.getValue());
            result.fields.put(fieldName, type);
        }
        return result;
    }
}

From source file:org.apache.qpid.disttest.json.PropertyValueAdapter.java

License:Apache License

@Override
public PropertyValue deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    } else if (json.isJsonPrimitive()) {
        Object result = null;//from   www  .ja v  a 2  s. c o m
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            result = primitive.getAsString();
        } else if (primitive.isNumber()) {
            String asString = primitive.getAsString();
            if (asString.indexOf('.') != -1 || asString.indexOf('e') != -1) {
                result = primitive.getAsDouble();
            } else {
                result = primitive.getAsLong();
            }
        } else if (primitive.isBoolean()) {
            result = primitive.getAsBoolean();
        } else {
            throw new JsonParseException("Unsupported primitive value " + primitive);
        }
        return new SimplePropertyValue(result);
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        List<Object> result = new ArrayList<Object>(array.size());
        for (JsonElement element : array) {
            result.add(context.deserialize(element, Object.class));
        }
        return new SimplePropertyValue(result);
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        JsonElement defElement = object.getAsJsonPrimitive(DEF_FIELD);
        Class<?> classInstance = null;
        if (defElement != null) {
            try {
                classInstance = _factory.getPropertyValueClass(defElement.getAsString());
            } catch (ClassNotFoundException e) {
                // ignore
            }
        }
        if (classInstance == null) {
            Map<String, Object> result = new HashMap<String, Object>();
            for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
                Object value = context.deserialize(entry.getValue(), Object.class);
                result.put(entry.getKey(), value);
            }
            return new SimplePropertyValue(result);
        } else {
            return context.deserialize(json, classInstance);
        }
    } else {
        throw new JsonParseException("Unsupported JSON type " + json);
    }
}

From source file:org.apache.sling.jms.Json.java

License:Apache License

private static <T> T toMapValue(JsonPrimitive p) {
    if (p.isString()) {
        return (T) p.getAsString();
    } else if (p.isBoolean()) {
        return (T) ((Boolean) p.getAsBoolean());
    } else if (p.isNumber()) {
        double d = p.getAsDouble();
        if (Math.floor(d) == d) {
            return (T) ((Long) p.getAsLong());
        }//from   ww w.jav  a 2 s.  com
        return (T) ((Double) d);
    } else {
        return null;
    }
}

From source file:org.cvasilak.jboss.mobile.app.model.ManagementModelBase.java

License:Apache License

public void setValue(JsonElement value) {
    if (value instanceof JsonPrimitive) {
        JsonPrimitive primitive = (JsonPrimitive) value;

        if (primitive.isNumber()) {
            try {
                this.value = NumberFormat.getInstance().parse(primitive.getAsString());
            } catch (ParseException e) {
            }/*w w  w  .j a  v  a 2 s.  c  o  m*/
        } else if (primitive.isBoolean()) {
            this.value = primitive.getAsBoolean();
        } else if (primitive.isString()) {
            this.value = primitive.getAsString();
        }

    } else if (value instanceof JsonNull) {
        this.value = "undefined";

    } else if (value instanceof JsonArray) {
        List<String> list = new ArrayList<String>();

        Iterator<JsonElement> iterator = value.getAsJsonArray().iterator();

        while (iterator.hasNext()) {
            JsonElement elem = iterator.next();

            if (elem instanceof JsonObject)
                list.add(elem.toString());
            else
                list.add(elem.getAsString());
        }

        this.value = list;

    } else if (value instanceof JsonObject) {
        this.value = value.toString();
    }
}

From source file:org.cvasilak.jboss.mobile.app.model.OperationParameter.java

License:Apache License

public void setDefaultValue(JsonElement value) {
    if (value instanceof JsonPrimitive) {
        JsonPrimitive primitive = (JsonPrimitive) value;

        if (primitive.isNumber()) {
            try {
                this.defaultValue = NumberFormat.getInstance().parse(primitive.getAsString());
            } catch (ParseException e) {
            }//  w ww .  java  2 s  .  c  o m
        } else if (primitive.isBoolean()) {
            this.defaultValue = primitive.getAsBoolean();
        } else if (primitive.isString()) {
            this.defaultValue = primitive.getAsString();
        }
    }
}

From source file:org.debux.webmotion.server.call.ClientSession.java

License:Open Source License

/**
 * Return only JsonElement use getAttribute and getAttributes with class as 
 * parameter to get a value.//from   ww  w  .  ja v a  2 s. c o  m
 */
@Override
public Object getAttribute(String name) {
    JsonElement value = attributes.get(name);
    if (value == null) {
        return value;

    } else if (value.isJsonPrimitive()) {
        JsonPrimitive primitive = value.getAsJsonPrimitive();
        if (primitive.isString()) {
            return primitive.getAsString();

        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();

        } else if (primitive.isNumber()) {
            return primitive.getAsDouble();
        }

    } else if (value.isJsonArray()) {
        return value.getAsJsonArray();

    } else if (value.isJsonObject()) {
        return value.getAsJsonObject();

    } else if (value.isJsonNull()) {
        return value.getAsJsonNull();
    }
    return value;
}

From source file:org.devnexus.aerogear.ShadowStore.java

License:Apache License

private void saveElement(JsonObject serialized, String path, Serializable id) {
    String sql = String.format(
            "insert into shadow_%s_property (PROPERTY_NAME, PROPERTY_VALUE, PARENT_ID) values (?,?,?)",
            className);//from w  w w.j av a2s  .co  m
    Set<Map.Entry<String, JsonElement>> members = serialized.entrySet();
    String pathVar = path.isEmpty() ? "" : ".";
    for (Map.Entry<String, JsonElement> member : members) {
        JsonElement jsonValue = member.getValue();
        String propertyName = member.getKey();
        if (jsonValue.isJsonObject()) {
            saveElement((JsonObject) jsonValue, path + pathVar + propertyName, id);
        } else if (jsonValue.isJsonArray()) {
            JsonArray jsonArray = jsonValue.getAsJsonArray();
            for (int index = 0; index < jsonArray.size(); index++) {
                JsonElement arrayElement = jsonArray.get(index);
                if (arrayElement.isJsonPrimitive()) {
                    JsonPrimitive primitive = arrayElement.getAsJsonPrimitive();
                    if (primitive.isBoolean()) {
                        String value = primitive.getAsBoolean() ? "true" : "false";
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else if (primitive.isNumber()) {
                        Number value = primitive.getAsNumber();
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else if (primitive.isString()) {
                        String value = primitive.getAsString();
                        getDatabase().execSQL(sql, new Object[] {
                                path + pathVar + propertyName + String.format("[%d]", index), value, id });
                    } else {
                        throw new IllegalArgumentException(
                                arrayElement + " isn't a number, boolean, or string");
                    }

                } else {
                    saveElement(arrayElement.getAsJsonObject(),
                            path + pathVar + propertyName + String.format("[%d]", index), id);
                }

            }
        } else {
            if (jsonValue.isJsonPrimitive()) {
                JsonPrimitive primitive = jsonValue.getAsJsonPrimitive();
                if (primitive.isBoolean()) {
                    String value = primitive.getAsBoolean() ? "true" : "false";
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else if (primitive.isNumber()) {
                    Number value = primitive.getAsNumber();
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else if (primitive.isString()) {
                    String value = primitive.getAsString();
                    getDatabase().execSQL(sql, new Object[] { path + pathVar + propertyName, value, id });
                } else {
                    throw new IllegalArgumentException(jsonValue + " isn't a number, boolean, or string");
                }

            } else {
                throw new IllegalArgumentException(jsonValue + " isn't a JsonPrimitive");
            }

        }
    }
}