Example usage for com.google.gson JsonPrimitive isString

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

Introduction

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

Prototype

public boolean isString() 

Source Link

Document

Check whether this primitive contains a String value.

Usage

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

License:Apache License

@Override
public Character deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isString()) {
        throw new DatastoreException("Invalid value for character type.");
    }//from  w  w  w  .jav  a 2 s.c  o m
    return jsonPrimitive.getAsCharacter();
}

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

License:Apache License

@Override
public Enum deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isString()) {
        throw new DatastoreException("Invalid value for enum type.");
    }/*from w  w  w .j a v  a  2 s  . c  o m*/
    Class<? extends Enum> enumClass = (Class<? extends Enum>) typeMetadata.getFieldType();
    return Enum.valueOf(enumClass, jsonPrimitive.getAsString());
}

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

License:Apache License

@Override
public String deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isString()) {
        throw new DatastoreException("Invalid value for string type.");
    }/*from w w  w. ja v a2 s . c o  m*/
    return jsonPrimitive.getAsString();
}

From source file:at.orz.arangodb.util.JsonUtils.java

License:Apache License

public static double toDouble(JsonElement elem) {
    if (elem != null && !elem.isJsonNull()) {
        JsonPrimitive primitive = elem.getAsJsonPrimitive();
        if (primitive.isNumber()) {
            return primitive.getAsDouble();
        } else if (primitive.isString()) {
            if ("INF".equals(primitive.getAsString())) {
                return Double.POSITIVE_INFINITY;
            } else if ("NaN".equals(primitive.getAsString())) {
                return Double.NaN;
            }/*from   w  w w  .j a v a 2s .c  om*/
        }
    }
    return Double.NaN;
}

From source file:bind.JsonTreeReader.java

License:Apache License

@Override
public JsonToken peek() throws IOException {
    if (stack.isEmpty()) {
        return JsonToken.END_DOCUMENT;
    }/*from  w  ww .  j ava2  s . c  o m*/

    Object o = peekStack();
    if (o instanceof Iterator) {
        Object secondToTop = stack.get(stack.size() - 2);
        boolean isObject = secondToTop instanceof JsonElement && ((JsonElement) secondToTop).isJsonObject();
        Iterator<?> iterator = (Iterator<?>) o;
        if (iterator.hasNext()) {
            if (isObject) {
                return JsonToken.NAME;
            } else {
                stack.add(iterator.next());
                return peek();
            }
        } else {
            return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
        }
    } else if (o instanceof JsonElement) {
        JsonElement el = (JsonElement) o;
        if (el.isJsonObject()) {
            return JsonToken.BEGIN_OBJECT;
        } else if (el.isJsonArray()) {
            return JsonToken.BEGIN_ARRAY;
        } else if (el.isJsonPrimitive()) {
            JsonPrimitive primitive = (JsonPrimitive) o;
            if (primitive.isString()) {
                return JsonToken.STRING;
            } else if (primitive.isBoolean()) {
                return JsonToken.BOOLEAN;
            } else if (primitive.isNumber()) {
                return JsonToken.NUMBER;
            } else {
                throw new AssertionError();
            }
        } else if (el.isJsonNull()) {
            return JsonToken.NULL;
        }
        throw new AssertionError();
    } else if (o == SENTINEL_CLOSED) {
        throw new IllegalStateException("JsonReader is closed");
    } else {
        throw new AssertionError();
    }
}

From source file:ccm.pay2spawn.util.JsonNBTHelper.java

License:Open Source License

/**
 * There is no way to detect number types and NBT is picky about this. Lets hope the original type id is there, otherwise we are royally screwed.
 *///from ww  w. j  a v a  2 s  .  c  om
public static NBTBase parseJSON(JsonPrimitive element) {
    String string = element.getAsString();
    if (string.contains(":")) {
        for (int id = 0; id < NBTTypes.length; id++) {
            if (string.startsWith(NBTTypes[id] + ":")) {
                String value = string.replace(NBTTypes[id] + ":", "");
                value = RandomRegistry.solveRandom(id, value);
                switch (id) {
                // 0 = END
                case BYTE:
                    return new NBTTagByte(Byte.parseByte(value));
                case SHORT:
                    return new NBTTagShort(Short.parseShort(value));
                case INT:
                    return new NBTTagInt(Integer.parseInt(value));
                case LONG:
                    return new NBTTagLong(Long.parseLong(value));
                case FLOAT:
                    return new NBTTagFloat(Float.parseFloat(value));
                case DOUBLE:
                    return new NBTTagDouble(Double.parseDouble(value));
                case BYTE_ARRAY:
                    return parseJSONByteArray(value);
                case STRING:
                    return new NBTTagString(value);
                // 9 = LIST != JsonPrimitive
                // 10 = COMPOUND != JsonPrimitive
                case INT_ARRAY:
                    return parseJSONIntArray(value);
                }
            }
        }
    }

    // Now it becomes guesswork.
    if (element.isString())
        return new NBTTagString(string);
    if (element.isBoolean())
        return new NBTTagByte((byte) (element.getAsBoolean() ? 1 : 0));

    Number n = element.getAsNumber();
    if (n instanceof Byte)
        return new NBTTagByte(n.byteValue());
    if (n instanceof Short)
        return new NBTTagShort(n.shortValue());
    if (n instanceof Integer)
        return new NBTTagInt(n.intValue());
    if (n instanceof Long)
        return new NBTTagLong(n.longValue());
    if (n instanceof Float)
        return new NBTTagFloat(n.floatValue());
    if (n instanceof Double)
        return new NBTTagDouble(n.doubleValue());

    throw new NumberFormatException(element.toString() + " is was not able to be parsed.");
}

From source file:ccm.pay2spawn.util.JsonNBTHelper.java

License:Open Source License

public static JsonPrimitive fixNulls(JsonPrimitive primitive) {
    if (primitive.isBoolean())
        return new JsonPrimitive(primitive.getAsBoolean());
    if (primitive.isNumber())
        return new JsonPrimitive(primitive.getAsNumber());
    if (primitive.isString())
        return new JsonPrimitive(primitive.getAsString());
    return JSON_PARSER.parse(primitive.toString()).getAsJsonPrimitive();
}

From source file:ch.cern.db.flume.sink.elasticsearch.serializer.JSONtoElasticSearchEventSerializer.java

License:GNU General Public License

private void appendBody(XContentBuilder builder, Event event) throws IOException {
    JsonParser parser = new JsonParser();

    JsonObject json = parser.parse(new String(event.getBody())).getAsJsonObject();

    for (Entry<String, JsonElement> property : json.entrySet()) {

        if (property.getValue().isJsonNull()) {
            builder.nullField(property.getKey());

            continue;
        }//  w ww  .  j  ava2s. co m

        if (!property.getValue().isJsonPrimitive()) {
            builder.field(property.getKey(), property.getValue());

            continue;
        }

        JsonPrimitive primitiveValue = (JsonPrimitive) property.getValue();

        if (primitiveValue.isBoolean())
            builder.field(property.getKey(), primitiveValue.getAsBoolean());
        else if (primitiveValue.isNumber())
            if (primitiveValue.getAsString().indexOf('.') != -1)
                builder.field(property.getKey(), primitiveValue.getAsNumber().doubleValue());
            else
                builder.field(property.getKey(), primitiveValue.getAsNumber().longValue());
        else if (primitiveValue.isString())
            builder.field(property.getKey(), primitiveValue.getAsString());
    }
}

From source file:com.adobe.acs.commons.remoteassets.impl.RemoteAssetsNodeSyncImpl.java

License:Apache License

/**
 * Set a simple resource property from the fetched JSON.
 *
 * @param value Object//www  .j  a v  a2  s.c o  m
 * @param key String
 * @param resource Resource
 * @throws RepositoryException exception
 */
private void setNodeSimpleProperty(final JsonPrimitive value, final String key, final Resource resource)
        throws RepositoryException {
    ValueMap resourceProperties = resource.adaptTo(ModifiableValueMap.class);
    if (value.isString() && DATE_REGEX.matcher(value.getAsString()).matches()) {
        try {
            resourceProperties.put(key,
                    GregorianCalendar.from(ZonedDateTime.parse(value.getAsString(), DATE_TIME_FORMATTER)));
        } catch (DateTimeParseException e) {
            LOG.warn("Unable to parse date '{}' for property:resource '{}'.", value,
                    key + ":" + resource.getPath());
        }
    } else if (value.isString() && DECIMAL_REGEX.matcher(value.getAsString()).matches()) {
        resourceProperties.put(key, value.getAsBigDecimal());
    } else if (value.isBoolean()) {
        resourceProperties.put(key, value.getAsBoolean());
    } else if (value.isNumber()) {
        if (DECIMAL_REGEX.matcher(value.getAsString()).matches()) {
            resourceProperties.put(key, value.getAsBigDecimal());
        } else {
            resourceProperties.put(key, value.getAsLong());
        }
    } else if (value.isJsonNull()) {
        resourceProperties.remove(key);
    } else {
        resourceProperties.put(key, value.getAsString());
    }

    LOG.trace("Property '{}' added for resource '{}'.", key, resource.getPath());
}

From source file:com.balajeetm.mystique.core.module.GsonSerialiser.java

License:Open Source License

@Override
public void serialize(JsonElement value, JsonGenerator gen, SerializerProvider provider) throws IOException {
    if (jsonLever.isNull(value)) {
        gen.writeNull();/*from  w  w  w .  j av  a  2 s.  c  om*/
    } else if (jsonLever.isObject(value)) {
        gen.writeStartObject();
        JsonObject jsonObject = value.getAsJsonObject();
        for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {
            gen.writeFieldName(entry.getKey());
            serialize(entry.getValue(), gen, provider);
        }
        gen.writeEndObject();
    } else if (jsonLever.isArray(value)) {
        gen.writeStartArray();
        JsonArray jsonArray = value.getAsJsonArray();
        for (JsonElement jsonElement : jsonArray) {
            serialize(jsonElement, gen, provider);
        }
        gen.writeEndArray();
    } else if (jsonLever.isPrimitive(value)) {
        JsonPrimitive jsonPrimitive = value.getAsJsonPrimitive();
        if (jsonPrimitive.isBoolean()) {
            gen.writeBoolean(jsonPrimitive.getAsBoolean());
        }
        if (jsonPrimitive.isNumber()) {
            Number nnode = jsonPrimitive.getAsNumber();
            if (nnode instanceof LazilyParsedNumber) {
                gen.writeNumber(nnode.toString());
            } else if (nnode instanceof Integer) {
                gen.writeNumber(nnode.intValue());
            } else if (nnode instanceof Short) {
                gen.writeNumber(nnode.shortValue());
            } else if (nnode instanceof BigInteger || nnode instanceof Long) {
                gen.writeNumber(nnode.longValue());
            } else if (nnode instanceof Float) {
                gen.writeNumber(nnode.floatValue());
            } else if (nnode instanceof Double || nnode instanceof BigDecimal) {
                gen.writeNumber(nnode.doubleValue());
            }
        }
        if (jsonPrimitive.isString()) {
            gen.writeString(jsonPrimitive.getAsString());
        }
    }
}