Example usage for com.google.gson JsonPrimitive isNumber

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

Introduction

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

Prototype

public boolean isNumber() 

Source Link

Document

Check whether this primitive contains a Number.

Usage

From source file:com.vsthost.rnd.jpsolver.data.ValueBuilder.java

License:Apache License

public static Value fromJsonElement(JsonElement element) {
    // Check primitive types:
    if (element instanceof JsonNull) {
        // Return the NONE value:
        return Value.NONE;
    } else if (element instanceof JsonPrimitive) {
        // Cast and get the primitive:
        JsonPrimitive primitive = (JsonPrimitive) element;

        // Check primitive types:
        if (primitive.isBoolean()) {
            return primitive.getAsBoolean() ? BooleanValue.TRUE : BooleanValue.FALSE;
        } else if (primitive.isNumber()) {
            return new NumericValue(primitive.getAsBigDecimal());
        } else {//  w ww .  j  a va  2 s.  co m
            return new TextValue(primitive.getAsString());
        }
    } else if (element instanceof JsonArray) {
        // Cast and get the array:
        JsonArray array = (JsonArray) element;

        // Iterate over the elements and populate the return value:
        ValueList<Value> retval = new ValueList<Value>();
        for (JsonElement e : array) {
            retval.add(ValueBuilder.fromJsonElement(e));
        }

        // Done, return:
        return retval;
    } else {
        // Cast and get the object:
        JsonObject object = (JsonObject) element;

        // Iterate over the elements and populate the return value:
        ValueMap retval = new ValueMap();
        for (Map.Entry<String, JsonElement> e : object.entrySet()) {
            retval.put(e.getKey(), ValueBuilder.fromJsonElement(e.getValue()));
        }

        // Done, return:
        return retval;
    }
}

From source file:cosmos.records.JsonRecords.java

License:Apache License

/**
 * Parses a {@link JsonObject{ into a MapRecord. A duplicate key in the JsonObject will
 * overwrite the previous key./*from   www  .ja v  a2  s.c  o m*/
 * @param map The JsonObject being parsed
 * @param generator Construct to generate a docId for this {@link MapRecord}
 * @return A MapRecord built from the {@link JsonObject}
 */
protected static MapRecord asMapRecord(JsonObject map, DocIdGenerator generator) {
    Map<Column, RecordValue<?>> data = Maps.newHashMap();
    for (Entry<String, JsonElement> entry : map.entrySet()) {
        final Column key = Column.create(entry.getKey());
        final JsonElement value = entry.getValue();

        if (value.isJsonNull()) {
            data.put(key, null);
        } else if (value.isJsonPrimitive()) {
            JsonPrimitive primitive = (JsonPrimitive) value;

            // Numbers
            if (primitive.isNumber()) {
                NumberRecordValue<?> v;

                double d = primitive.getAsDouble();
                if ((int) d == d) {
                    v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                } else if ((long) d == d) {
                    v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                } else {
                    v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                }

                data.put(key, v);

            } else if (primitive.isString()) {
                // String
                data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

            } else if (primitive.isBoolean()) {
                // Boolean
                data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

            } else if (primitive.isJsonNull()) {
                // Is this redundant?
                data.put(key, null);
            } else {
                throw new RuntimeException("Unhandled primitive: " + primitive);
            }
        } else {
            throw new RuntimeException("Expected a String, Number or Boolean");
        }
    }

    return new MapRecord(data, generator.getDocId(data), Defaults.EMPTY_VIS);
}

From source file:cosmos.records.JsonRecords.java

License:Apache License

/**
 * Builds a {@link MultimapRecord} from the provided {@link JsonObject} using a docid
 * from the {@link DocIdGenerator}. This method will support lists of values for a key
 * in the {@link JsonObject} but will fail on values which are {@link JsonObject}s and 
 * values which have nested {@link JsonArray}s. 
 * @param map The {@link JsonObject} to build this {@link MultimapRecord} from
 * @param generator {@link DocIdGenerator} to construct a docid for this {@link MultimapRecord}
 * @return A {@link MultimapRecord} built from the provided arguments.
 *///from  w  w  w  .ja v  a2s  .  c  o m
protected static MultimapRecord asMultimapRecord(JsonObject map, DocIdGenerator generator) {
    Multimap<Column, RecordValue<?>> data = HashMultimap.create();
    for (Entry<String, JsonElement> entry : map.entrySet()) {
        final Column key = Column.create(entry.getKey());
        final JsonElement value = entry.getValue();

        if (value.isJsonNull()) {
            data.put(key, null);
        } else if (value.isJsonPrimitive()) {
            JsonPrimitive primitive = (JsonPrimitive) value;

            // Numbers
            if (primitive.isNumber()) {
                NumberRecordValue<?> v;

                double d = primitive.getAsDouble();
                if ((int) d == d) {
                    v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                } else if ((long) d == d) {
                    v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                } else {
                    v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                }

                data.put(key, v);

            } else if (primitive.isString()) {
                // String
                data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

            } else if (primitive.isBoolean()) {
                // Boolean
                data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

            } else if (primitive.isJsonNull()) {
                // Is this redundant?
                data.put(key, null);
            } else {
                throw new RuntimeException("Unhandled primitive: " + primitive);
            }
        } else if (value.isJsonArray()) {

            // Multimaps should handle the multiple values, not fail
            JsonArray values = value.getAsJsonArray();
            for (JsonElement element : values) {
                if (element.isJsonNull()) {
                    data.put(key, null);
                } else if (element.isJsonPrimitive()) {

                    JsonPrimitive primitive = (JsonPrimitive) element;

                    // Numbers
                    if (primitive.isNumber()) {
                        NumberRecordValue<?> v;

                        double d = primitive.getAsDouble();
                        if ((int) d == d) {
                            v = new IntegerRecordValue((int) d, Defaults.EMPTY_VIS);
                        } else if ((long) d == d) {
                            v = new LongRecordValue((long) d, Defaults.EMPTY_VIS);
                        } else {
                            v = new DoubleRecordValue(d, Defaults.EMPTY_VIS);
                        }

                        data.put(key, v);

                    } else if (primitive.isString()) {
                        // String
                        data.put(key, new StringRecordValue(primitive.getAsString(), Defaults.EMPTY_VIS));

                    } else if (primitive.isBoolean()) {
                        // Boolean
                        data.put(key, new BooleanRecordValue(primitive.getAsBoolean(), Defaults.EMPTY_VIS));

                    } else if (primitive.isJsonNull()) {
                        // Is this redundant?
                        data.put(key, null);
                    } else {
                        throw new RuntimeException("Unhandled Json primitive: " + primitive);
                    }
                } else {
                    throw new RuntimeException("Expected a Json primitive");
                }
            }

        } else {
            throw new RuntimeException("Expected a String, Number or Boolean");
        }
    }

    return new MultimapRecord(data, generator.getDocId(data), Defaults.EMPTY_VIS);

}

From source file:Days.Day12.java

public int parsePrimitive(JsonPrimitive primitive) {
    if (primitive.isNumber()) {
        return primitive.getAsInt();
    }//w w  w . jav  a2  s  . com
    return 0;
}

From source file:de.azapps.mirakel.model.task.TaskDeserializer.java

License:Open Source License

private static void handleAdditionalEntries(final Task t, final String key, final JsonElement val) {
    if (val.isJsonPrimitive()) {
        final JsonPrimitive p = (JsonPrimitive) val;
        if (p.isBoolean()) {
            t.addAdditionalEntry(key, String.valueOf(val.getAsBoolean()));
        } else if (p.isNumber()) {
            t.addAdditionalEntry(key, String.valueOf(val.getAsInt()));
        } else if (p.isJsonNull()) {
            t.addAdditionalEntry(key, "null");
        } else if (p.isString()) {
            t.addAdditionalEntry(key, '"' + val.getAsString() + '"');
        } else {//from w  w  w .j  a  v  a 2  s . c  o  m
            Log.w(TAG, "unknown json-type");
        }
    } else if (val.isJsonArray()) {
        final JsonArray a = (JsonArray) val;
        StringBuilder s = new StringBuilder("[");
        boolean first = true;
        for (final JsonElement e : a) {
            if (e.isJsonPrimitive()) {
                final JsonPrimitive p = (JsonPrimitive) e;
                final String add;
                if (p.isBoolean()) {
                    add = String.valueOf(p.getAsBoolean());
                } else if (p.isNumber()) {
                    add = String.valueOf(p.getAsInt());
                } else if (p.isString()) {
                    add = '"' + p.getAsString() + '"';
                } else if (p.isJsonNull()) {
                    add = "null";
                } else {
                    Log.w(TAG, "unknown json-type");
                    break;
                }
                s.append(first ? "" : ",").append(add);
                first = false;
            } else {
                Log.w(TAG, "unknown json-type");
            }
        }
        t.addAdditionalEntry(key, s + "]");
    } else {
        Log.w(TAG, "unknown json-type");
    }
}

From source file:de.csdev.ebus.cfg.std.EBusValueJsonDeserializer.java

License:Open Source License

@Override
public List<EBusValueDTO> deserialize(JsonElement jElement, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonArray asJsonArray = jElement.getAsJsonArray();
    ArrayList<EBusValueDTO> result = new ArrayList<EBusValueDTO>();

    ArrayList<String> fields = new ArrayList<String>();
    for (Field field : EBusValueDTO.class.getDeclaredFields()) {
        SerializedName annotation = field.getAnnotation(SerializedName.class);

        if (annotation != null) {
            fields.add(annotation.value());

        } else {//from w  w  w .  j a  v a2 s. com
            fields.add(field.getName());
        }
    }

    for (JsonElement jsonElement : asJsonArray) {
        JsonObject jObject = jsonElement.getAsJsonObject();
        EBusValueDTO valueDTO = context.deserialize(jObject, EBusValueDTO.class);

        for (Entry<String, JsonElement> entry : jObject.entrySet()) {
            if (!fields.contains(entry.getKey())) {

                if (entry.getValue().isJsonPrimitive()) {
                    JsonPrimitive primitive = (JsonPrimitive) entry.getValue();

                    if (primitive.isNumber()) {
                        valueDTO.setProperty(entry.getKey(), primitive.getAsBigDecimal());

                    } else if (primitive.isBoolean()) {
                        valueDTO.setProperty(entry.getKey(), primitive.getAsBoolean());

                    } else if (primitive.isString()) {
                        valueDTO.setProperty(entry.getKey(), primitive.getAsString());
                    }

                } else {
                    valueDTO.setProperty(entry.getKey(), entry.getValue().getAsString());

                }

            }
        }

        result.add(valueDTO);
    }

    return result;
}

From source file:de.innovationgate.wgpublisher.webtml.utils.JsonUtils.java

License:Open Source License

public Object jsonToJava(JsonElement jsonValue) {
    Object value = null;/*from  w  w w.j a v  a2  s .co  m*/
    if (jsonValue.isJsonNull()) {
        value = null;
    } else if (jsonValue.isJsonPrimitive()) {
        JsonPrimitive prim = (JsonPrimitive) jsonValue;
        if (prim.isNumber()) {
            value = prim.getAsDouble();
        } else if (prim.isBoolean()) {
            value = prim.getAsBoolean();
        } else {
            value = prim.getAsString();
        }
        value = jsonToJavaConversions(value);
    } else if (jsonValue.isJsonArray()) {
        JsonArray array = jsonValue.getAsJsonArray();
        List<Object> list = new ArrayList<Object>();
        for (JsonElement element : array) {
            list.add(jsonToJava(element));
        }
    } else if (jsonValue.isJsonObject()) {
        JsonObject obj = jsonValue.getAsJsonObject();
        Map<String, Object> map = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            map.put(String.valueOf(entry.getKey()), jsonToJava(entry.getValue()));
        }
    }

    return value;
}

From source file:hdm.stuttgart.esell.router.GeneralObjectDeserializer.java

License:Apache License

public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonNull()) {
        return null;
    } else if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            return primitive.getAsString();
        } else if (primitive.isNumber()) {
            return primitive.getAsNumber();
        } else if (primitive.isBoolean()) {
            return primitive.getAsBoolean();
        }/*from w w  w  .j  a  v a 2  s .  c  o  m*/
    } else if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        Object[] result = new Object[array.size()];
        int i = 0;
        for (JsonElement element : array) {
            result[i] = deserialize(element, null, context);
            ++i;
        }
        return result;
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        Map<String, Object> result = new HashMap<String, Object>();
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            Object value = deserialize(entry.getValue(), null, context);
            result.put(entry.getKey(), value);
        }
        return result;
    } else {
        throw new JsonParseException("Unknown JSON type for JsonElement " + json.toString());
    }
    return null;
}

From source file:hu.bme.mit.incqueryd.engine.test.util.TupleDeserializer.java

License:Open Source License

@Override
public Tuple deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonObject = json.getAsJsonObject();
    JsonArray array = jsonObject.getAsJsonArray("tuple");

    ArrayList<Object> list = new ArrayList<Object>();

    for (Object object : array) {
        // checking if the object is a primitive
        if (object instanceof JsonPrimitive) {
            JsonPrimitive primitive = (JsonPrimitive) object;

            // if it is a number, get it as a long
            if (primitive.isNumber()) {
                long i = primitive.getAsLong();
                list.add(i);//from   w w w  . j  a va2s  . c o m
            }
        }
    }

    Tuple tuple = new Tuple(list.toArray());
    return tuple;
}

From source file:io.thinger.thinger.DeviceResourceDescription.java

License:Open Source License

public DeviceResourceDescription(String resourceName, JsonElement resourceDescription) {
    this.resourceName = resourceName;
    this.resourceType = ResourceType.NONE;
    if (resourceDescription.isJsonObject()) {
        JsonObject object = resourceDescription.getAsJsonObject();
        if (object.has("fn")) {
            JsonElement function = object.get("fn");
            if (function.isJsonPrimitive()) {
                JsonPrimitive value = function.getAsJsonPrimitive();
                if (value.isNumber()) {
                    resourceType = ResourceType.get(value.getAsInt());
                }//w w w.  j a  v  a 2s  . c  o m
            }
        }
    }
}