public <T> T determineJsonElementForDistinguishingImplementers(JsonElement json, JsonDeserializationContext context, String field, String origin) { JsonObject jsonObject = json.getAsJsonObject(); JsonPrimitive prim = (JsonPrimitive) jsonObject.get(field); JsonPrimitive originField = (JsonPrimitive) jsonObject.get(origin); String typeName = prim.getAsString(); Class<?> klass = classForName(typeName, originField == null ? "gocd" : originField.getAsString()); return context.deserialize(jsonObject, klass); }
public String setterParameterType(String parameter, JsonPrimitive parameterDescription) { if (parameter.equals("values") || parameter.equals("fieldValues") || parameter.equals("keys")) { return "List<String>"; } if (parameterDescription != null && parameterDescription.getAsString().matches(".*[Cc]omma.?separated.*|.*[Ll]ist of.*")) { return "List<String>"; } return "String"; }
private static String defensivelyGetString(JsonObject obj, String key) { return obj.has(key) ? obj.getAsJsonPrimitive(key).getAsString() : ""; } }
private void setString(Consumer<String> setter, JsonObject jsonObject, String name) { JsonPrimitive primitive = jsonObject.getAsJsonPrimitive(name); if (primitive != null) { setter.accept(primitive.getAsString()); } }
@Override public String getAsString(String name) { return map.getAsJsonPrimitive(name).getAsString(); }
JsonObject jsonObject = json.getAsJsonObject(); JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME); String className = prim.getAsString();
final String sha1 = checksumList.getAsJsonPrimitive("sha1").getAsString(); final String sha256 = sha256Primitive == null ? null : sha256Primitive.getAsString(); final String md5 = checksumList.getAsJsonPrimitive("md5").getAsString(); final String downloadUri = jsonElement.getAsJsonObject().getAsJsonPrimitive("downloadUri").getAsString(); final String path = jsonElement.getAsJsonObject().getAsJsonPrimitive("path").getAsString();
metadata.shader = obj.getAsJsonPrimitive("shader").getAsString();
private BlockFamilyDefinitionData createBaseData(JsonObject jsonObject) { JsonPrimitive basedOn = jsonObject.getAsJsonPrimitive("basedOn"); if (basedOn != null && !basedOn.getAsString().isEmpty()) { Optional<BlockFamilyDefinition> baseDef = assetManager.getAsset(basedOn.getAsString(), BlockFamilyDefinition.class); if (baseDef.isPresent()) { BlockFamilyDefinitionData data = baseDef.get().getData(); if (data.getBlockFamily() == FreeformFamily.class) { data.setBlockFamily(null); } return data; } else { throw new JsonParseException("Unable to resolve based block definition '" + basedOn.getAsString() + "'"); } } BlockFamilyDefinitionData data = new BlockFamilyDefinitionData(); data.getBaseSection().setSounds(assetManager.getAsset("engine:default", BlockSounds.class).get()); return data; } }
@Override public BlockShapeData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { BlockShapeData shape = new BlockShapeData(); JsonObject shapeObj = json.getAsJsonObject(); if (shapeObj.has(DISPLAY_NAME)) { shape.setDisplayName(shapeObj.getAsJsonPrimitive(DISPLAY_NAME).getAsString()); } for (BlockPart part : BlockPart.values()) { if (shapeObj.has(part.toString().toLowerCase(Locale.ENGLISH))) { JsonObject meshObj = shapeObj.getAsJsonObject(part.toString().toLowerCase(Locale.ENGLISH)); shape.setMeshPart(part, (BlockMeshPart) context.deserialize(meshObj, BlockMeshPart.class)); if (part.isSide() && meshObj.has(FULL_SIDE)) { shape.setBlockingSide(part.getSide(), meshObj.get(FULL_SIDE).getAsBoolean()); } } } if (shapeObj.has(COLLISION) && shapeObj.get(COLLISION).isJsonObject()) { JsonObject collisionInfo = shapeObj.get(COLLISION).getAsJsonObject(); processCollision(context, shape, collisionInfo); } else { shape.setCollisionShape(COLLISION_SHAPE_FACTORY.getNewUnitCube()); shape.setCollisionSymmetric(true); } return shape; }
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; }
parsedNode = new JsonTreeValue(jsonKey, jsonValue.getAsNumber(), JsonTreeValue.Type.KEY_VALUE_PAIR); } else { parsedNode = new JsonTreeValue(jsonKey, jsonValue.getAsString(), JsonTreeValue.Type.KEY_VALUE_PAIR);
JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive(); if (jsonPrimitive.isString()) return StringMemory.valueOf(jsonPrimitive.getAsString()); else if (jsonPrimitive.isBoolean()) return jsonPrimitive.getAsBoolean() ? Memory.TRUE : Memory.FALSE; else if (jsonPrimitive.isNumber()) { Memory l = StringMemory.toLong(jsonPrimitive.getAsString()); if (l != null) return l;
final JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isString()) { builder.value(primitive.getAsString()); } else if (primitive.isNumber()) { builder.value(primitive.getAsInt());
/** * 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()); } } }
/** * convenience method to get this element as a primitive integer. * * @return get this element as a primitive integer. * @throws NumberFormatException if the value contained is not a valid integer. */ @Override public int getAsInt() { return isNumber() ? getAsNumber().intValue() : Integer.parseInt(getAsString()); }
/** * convenience method to get this element as a float. * * @return get this element as a float. * @throws NumberFormatException if the value contained is not a valid float. */ @Override public float getAsFloat() { return isNumber() ? getAsNumber().floatValue() : Float.parseFloat(getAsString()); }
/** * convenience method to get this element as a primitive short. * * @return get this element as a primitive short. * @throws NumberFormatException if the value contained is not a valid short value. */ @Override public short getAsShort() { return isNumber() ? getAsNumber().shortValue() : Short.parseShort(getAsString()); }
@Override public String nextString() throws IOException { JsonToken token = peek(); if (token != JsonToken.STRING && token != JsonToken.NUMBER) { throw new IllegalStateException( "Expected " + JsonToken.STRING + " but was " + token + locationString()); } String result = ((JsonPrimitive) popStack()).getAsString(); if (stackSize > 0) { pathIndices[stackSize - 1]++; } return result; }