// Creates the json object which will manage the information received GsonBuilder builder = new GsonBuilder(); // Register an adapter to manage the date types as long values builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); } }); Gson gson = builder.create();
@Override public boolean isBooleanArray() { for (JsonElement element : array) { if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isBoolean()) { return false; } } return true; }
@Override public boolean isStringArray() { for (JsonElement element : array) { if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isString()) { return false; } } return true; }
@Override public boolean isNumberArray() { for (JsonElement element : array) { if (!element.isJsonPrimitive() || !element.getAsJsonPrimitive().isNumber()) { return false; } } return true; }
private static JsonObject normalizeSourceClause(JsonObject queryObject) { JsonObject sourceObject; if (queryObject.has("_source")) { JsonElement sourceElement = queryObject.get("_source"); if (sourceElement.isJsonObject()) { sourceObject = sourceElement.getAsJsonObject(); } else if (sourceElement.isJsonArray()) { // in this case, the values of the array are includes sourceObject = new JsonObject(); queryObject.add("_source", sourceObject); sourceObject.add("includes", sourceElement.getAsJsonArray()); } else if (sourceElement.isJsonPrimitive() && sourceElement.getAsJsonPrimitive().isBoolean()) { // if _source is a boolean, we override the configuration with include/exclude sourceObject = new JsonObject(); } else { throw new JsonSyntaxException("_source must be an object, an array or a boolean"); } } else { sourceObject = new JsonObject(); } queryObject.add("_source", sourceObject); return sourceObject; }
@Override public final boolean isBytes() { if (getElement().isJsonPrimitive() && getElement().getAsJsonPrimitive().isString()) { if (cachedDecodedBytes != null) { return true; } try { cachedDecodedBytes = BaseEncoding.base64().decode(getElement().getAsString()); return true; } catch (IllegalArgumentException e) { return false; } } return false; }
sortArray = new JsonArray(); sortArray.add(sortElement.getAsJsonObject()); } else if (sortElement.isJsonPrimitive() && sortElement.getAsJsonPrimitive().isString()) { String sortField = sortElement.getAsString(); sortArray = new JsonArray();
@Override public final boolean isString() { return getElement().isJsonPrimitive() && getElement().getAsJsonPrimitive().isString(); }
@Override public final boolean isNumber() { return getElement().isJsonPrimitive() && getElement().getAsJsonPrimitive().isNumber(); }
for (Map.Entry<String, JsonElement> prop : params.entrySet()) { if (prop.getValue().isJsonPrimitive()) { if (prop.getValue().getAsJsonPrimitive().isString()) { Optional<? extends Texture> texture = assetManager.getAsset(prop.getValue().getAsString(), Texture.class); if (texture.isPresent()) { metadata.textures.put(prop.getKey(), texture.get()); } else if (prop.getValue().getAsJsonPrimitive().isNumber()) { metadata.floatParams.put(prop.getKey(), prop.getValue().getAsFloat()); } else if (prop.getValue().getAsJsonPrimitive().isBoolean()) { metadata.intParams.put(prop.getKey(), (prop.getValue().getAsBoolean()) ? 1 : 0); boolean valid = true; for (int i = 0; i < array.size(); ++i) { if (!array.get(i).isJsonPrimitive() || !array.get(i).getAsJsonPrimitive().isNumber()) { valid = false; break;
@Override public final boolean isBoolean() { return getElement().isJsonPrimitive() && getElement().getAsJsonPrimitive().isBoolean(); }
private void processCollision(JsonDeserializationContext context, BlockShapeData shape, JsonObject collisionInfo) { if (collisionInfo.has(PITCH_SYMMETRIC) && collisionInfo.get(PITCH_SYMMETRIC).isJsonPrimitive() && collisionInfo.get(PITCH_SYMMETRIC).getAsJsonPrimitive().isBoolean()) { shape.setPitchSymmetric(collisionInfo.get(PITCH_SYMMETRIC).getAsBoolean()); && collisionInfo.get(YAW_SYMMETRIC).getAsJsonPrimitive().isBoolean()) { shape.setYawSymmetric(collisionInfo.get(YAW_SYMMETRIC).getAsBoolean()); && collisionInfo.get(ROLL_SYMMETRIC).getAsJsonPrimitive().isBoolean()) { shape.setRollSymmetric(collisionInfo.get(ROLL_SYMMETRIC).getAsBoolean()); && collisionInfo.get(SYMMETRIC).getAsJsonPrimitive().isBoolean()) { if (collisionInfo.get(SYMMETRIC).getAsBoolean()) { shape.setCollisionSymmetric(true); && collisionInfo.get(CONVEX_HULL).getAsJsonPrimitive().isBoolean()) { List<Vector3f> verts = buildVertList(shape); ConvexHullShape convexHull = COLLISION_SHAPE_FACTORY.getNewConvexHull(verts);
public void convert(JsonElement value, ColumnVector vect, int row) { if (value == null || value.isJsonNull()) { vect.noNulls = false; vect.isNull[row] = true; } else { if (value.getAsJsonPrimitive().isString()) { TimestampColumnVector vector = (TimestampColumnVector) vect; vector.set( row, Timestamp.valueOf(value.getAsString().replaceAll( "[TZ]", " "))); } else if (value.getAsJsonPrimitive().isNumber()) { TimestampColumnVector vector = (TimestampColumnVector) vect; vector.set( row, new Timestamp(value.getAsLong())); } else { if (!back.isBackOff()) { LOG.warn("Timestamp is neither string nor number: {}", value); } vect.noNulls = false; vect.isNull[row] = true; } } } }
@Override public PixelFormat deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) { return new PixelFormat().withDepthBits(json.getAsInt()); } return new PixelFormat().withDepthBits(24); }
public Object unwrap(final Object o) { if (o == null) { return null; } 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; }
@Override public EntityData.Value deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { EntityData.Value.Builder value = EntityData.Value.newBuilder(); if (json.isJsonPrimitive()) { extractPrimitive(value, json); } else if (json.isJsonObject()) { extractMap(json, context, value); } else if (json.isJsonArray()) { JsonArray jsonArray = json.getAsJsonArray(); TByteList byteList = new TByteArrayList(); for (JsonElement element : jsonArray) { if (element.isJsonArray()) { value.addValue((EntityData.Value) context.deserialize(element, EntityData.Value.class)); } else if (element.isJsonObject()) { value.addValue((EntityData.Value) context.deserialize(element, EntityData.Value.class)); } else if (element.isJsonPrimitive()) { extractPrimitive(value, element); if (element.getAsJsonPrimitive().isNumber()) { try { byteList.add(element.getAsByte()); } catch (NumberFormatException nfe) { byteList.add((byte) 0); } } } } value.setBytes(ByteString.copyFrom(byteList.toArray())); } return value.build(); }
JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isBoolean()) { return new JsonTree(new JsonTreeValue(name, json.getAsBoolean(),
final JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isString()) { builder.value(primitive.getAsString());
/** * Reading from BSON to GSON */ @Test public void bsonToGson() throws Exception { BsonDocument document = new BsonDocument(); document.append("boolean", new BsonBoolean(true)); document.append("int32", new BsonInt32(32)); document.append("int64", new BsonInt64(64)); document.append("double", new BsonDouble(42.42D)); document.append("string", new BsonString("foo")); document.append("null", new BsonNull()); document.append("array", new BsonArray()); document.append("object", new BsonDocument()); JsonElement element = TypeAdapters.JSON_ELEMENT.read(new BsonReader(new BsonDocumentReader(document))); check(element.isJsonObject()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().isBoolean()); check(element.getAsJsonObject().get("boolean").getAsJsonPrimitive().getAsBoolean()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int32").getAsJsonPrimitive().getAsNumber().intValue()).is(32); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("int64").getAsJsonPrimitive().getAsNumber().longValue()).is(64L); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().isNumber()); check(element.getAsJsonObject().get("double").getAsJsonPrimitive().getAsNumber().doubleValue()).is(42.42D); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().isString()); check(element.getAsJsonObject().get("string").getAsJsonPrimitive().getAsString()).is("foo"); check(element.getAsJsonObject().get("null").isJsonNull()); check(element.getAsJsonObject().get("array").isJsonArray()); check(element.getAsJsonObject().get("object").isJsonObject()); }
private void extractPrimitive(EntityData.Value.Builder value, JsonElement element) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isNumber()) { value.addDouble(primitive.getAsDouble()); value.addFloat(primitive.getAsFloat()); try { value.addInteger(primitive.getAsInt()); value.addLong(primitive.getAsLong()); } catch (NumberFormatException e) { value.addInteger(0); value.addLong(0); } } if (primitive.isBoolean()) { value.addBoolean(primitive.getAsBoolean()); } if (primitive.isString()) { value.addString(primitive.getAsString()); } } }