List of usage examples for com.google.gson JsonPrimitive isNumber
public boolean isNumber()
From source file:com.hbm.devices.jet.JetPeer.java
License:Open Source License
private boolean isResponse(JsonObject object) { JsonPrimitive id = object.getAsJsonPrimitive("id"); return (id != null) && (id.isNumber()); }
From source file:com.hbm.devices.jet.JetPeer.java
License:Open Source License
private void sendResponse(JsonObject request, JsonObject responseObject) { JsonPrimitive id = request.getAsJsonPrimitive("id"); if ((id != null) && ((id.isString()) || (id.isNumber()))) { responseObject.add("id", id); this.connection.sendMessage(gson.toJson(responseObject)); }/* w w w . j av a2 s .c o m*/ }
From source file:com.headswilllol.mineflat.world.SaveManager.java
License:Open Source License
public static Chunk loadChunk(Level level, int chunk) { JsonObject jChunk = level.getWorld().getJson().getAsJsonObject("levels") .getAsJsonObject(Integer.toString(level.getIndex())).getAsJsonObject("chunks") .getAsJsonObject(Integer.toString(chunk)); if (jChunk != null) { System.out.println("Loading chunk " + chunk); Biome biome = Biome.getById(jChunk.get("biome").toString()); Chunk c = new Chunk(level, chunk, biome); for (JsonElement blockObj : jChunk.getAsJsonArray("blocks")) { JsonObject block = blockObj.getAsJsonObject(); Material type = Material.valueOf(block.get("type").getAsString()); if (type == null) type = Material.AIR;/* w w w .java 2s .c o m*/ int data = block.get("data") != null ? block.get("data").getAsInt() : 0; Block b = new Block(type, data, new Location(level, Chunk.getWorldXFromChunkIndex(chunk, block.get("x").getAsLong()), block.get("y").getAsLong())); JsonObject meta = (JsonObject) block.get("metadata"); for (Map.Entry<String, JsonElement> e : meta.entrySet()) { JsonPrimitive prim = meta.get(e.getKey()).getAsJsonPrimitive(); Object value; if (prim.isBoolean()) value = prim.getAsBoolean(); else if (prim.isNumber()) value = prim.getAsNumber(); else if (prim.isString()) value = prim.getAsString(); else value = prim.getAsCharacter(); b.setMetadata(e.getKey(), value); } b.addToWorld(); } for (Object entityObj : jChunk.get("entities").getAsJsonArray()) { JsonObject entity = (JsonObject) entityObj; EntityType type = EntityType.valueOf(entity.get("type").getAsString()); float x = Chunk.getWorldXFromChunkIndex(c.getIndex(), Float.valueOf(Double.toString(entity.get("x").getAsDouble()))); float y = Float.valueOf(Double.toString(entity.get("y").getAsDouble())); float w = Float.valueOf(Double.toString(entity.get("w").getAsDouble())); float h = Float.valueOf(Double.toString(entity.get("h").getAsDouble())); Entity e; if (entity.has("living")) { if (entity.has("mob")) { switch (type) { case GHOST: e = new Ghost(new Location(level, x, y)); break; case SNAIL: e = new Snail(new Location(level, x, y)); break; default: continue; // ignore it } ((Mob) e).setPlannedWalkDistance(entity.get("pwd").getAsFloat()); ((Mob) e).setActualWalkDistance(entity.get("awd").getAsFloat()); ((Mob) e).setLastX(entity.get("lx").getAsFloat()); } else { switch (type) { case PLAYER: e = new Player(new Location(level, x, y)); break; case HUMAN: e = new Human(new Location(level, x, y)); break; default: continue; // ignore it } } ((Living) e).setFacingDirection(Direction.valueOf(entity.get("fd").getAsString())); ((Living) e).setJumping(entity.get("j").getAsBoolean()); } else e = new Entity(type, new Location(level, x, y), w, h); e.getVelocity().setX(Float.valueOf(Double.toString(entity.get("xv").getAsDouble()))); e.getVelocity().setY(Float.valueOf(Double.toString(entity.get("yv").getAsDouble()))); level.addEntity(e); if (type == EntityType.PLAYER) Main.player = (Player) e; } c.updateLight(); Chunk left = c.getLevel().getChunk(c.getIndex() == 1 ? -1 : c.getIndex() - 1); Chunk right = c.getLevel().getChunk(c.getIndex() == -1 ? 1 : c.getIndex() + 1); if (left != null) { for (int y = 0; y < c.getLevel().getWorld().getChunkHeight(); y++) { left.getBlock(c.getLevel().getWorld().getChunkLength() - 1, y).updateLight(); VboUtil.updateChunkArray(c.getLevel(), left.getIndex()); } } if (right != null) { for (int y = 0; y < c.getLevel().getWorld().getChunkHeight(); y++) { right.getBlock(0, y).updateLight(); VboUtil.updateChunkArray(c.getLevel(), right.getIndex()); } } System.gc(); //TODO: temporary fix until I have the motivation to find the memory leak return c; } return null; }
From source file:com.helion3.prism.util.DataUtil.java
License:MIT License
/** * Attempts to convert a JsonElement to an a known type. * * @param element JsonElement/*from ww w . ja v a2 s .com*/ * @return Optional<Object> */ private static Optional<Object> jsonElementToObject(JsonElement element) { Object result = null; if (element.isJsonObject()) { result = dataViewFromJson(element.getAsJsonObject()); } else if (element.isJsonPrimitive()) { JsonPrimitive prim = element.getAsJsonPrimitive(); if (prim.isBoolean()) { result = prim.getAsBoolean(); } else if (prim.isNumber()) { result = prim.getAsNumber().intValue(); } else if (prim.isString()) { result = prim.getAsString(); } } else if (element.isJsonArray()) { List<Object> list = new ArrayList<Object>(); JsonArray arr = element.getAsJsonArray(); arr.forEach(new Consumer<JsonElement>() { @Override public void accept(JsonElement t) { Optional<Object> translated = jsonElementToObject(t); if (translated.isPresent()) { list.add(translated.get()); } } }); result = list; } return Optional.ofNullable(result); }
From source file:com.helion3.safeguard.util.DataUtil.java
License:MIT License
/** * Convert JSON to a DataContainer.// w w w. j a v a2 s .co m * @param json JsonObject * @return DataContainer */ public static DataContainer jsonToDataContainer(JsonObject json) { DataContainer result = new MemoryDataContainer(); for (Entry<String, JsonElement> entry : json.entrySet()) { DataQuery keyQuery = DataQuery.of(entry.getKey()); Object object = entry.getValue(); if (object instanceof JsonObject) { result.set(keyQuery, jsonToDataContainer((JsonObject) object)); } else if (object instanceof JsonArray) { JsonArray array = (JsonArray) object; Iterator<JsonElement> iterator = array.iterator(); List<Object> list = new ArrayList<Object>(); while (iterator.hasNext()) { Object child = iterator.next(); if (child instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) child; if (primitive.isString()) { list.add(primitive.getAsString()); } else if (primitive.isNumber()) { // @todo may be wrong format. how do we handle? list.add(primitive.getAsInt()); } else if (primitive.isBoolean()) { list.add(primitive.getAsBoolean()); } else { SafeGuard.getLogger() .error("Unhandled list JsonPrimitive data type: " + primitive.toString()); } } } result.set(keyQuery, list); } else { if (object instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) object; if (primitive.isString()) { result.set(keyQuery, primitive.getAsString()); } else if (primitive.isNumber()) { // @todo may be wrong format. how do we handle? result.set(keyQuery, primitive.getAsInt()); } else if (primitive.isBoolean()) { result.set(keyQuery, primitive.getAsBoolean()); } else { SafeGuard.getLogger().error("Unhandled JsonPrimitive data type: " + primitive.toString()); } } } } return result; }
From source file:com.hi3project.dandelion.gip.codec.json.JSONGIPCodec.java
License:Open Source License
private String jsonValueAsString(JsonElement value) throws GIPEventCodeDecodeErrorException { JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isString()) { return primitive.getAsString(); } else if (primitive.isBoolean()) { return (Boolean.toString(primitive.getAsBoolean())); } else if (primitive.isNumber()) { return (Double.toString(primitive.getAsDouble())); } else {// w ww . j ava2s .c om throw new GIPEventCodeDecodeErrorException("", "Unable to decode JsonValue."); } }
From source file:com.ibm.streamsx.topology.generator.spl.SPLGenerator.java
License:Open Source License
/** * Append the value with the correct SPL suffix. Integer & Double do not * require a suffix/*w w w . j a v a 2 s.co m*/ */ static void numberLiteral(StringBuilder sb, JsonPrimitive value, String type) { String suffix = ""; switch (type) { case "INT8": suffix = "b"; break; case "INT16": suffix = "h"; break; case "INT32": break; case "INT64": suffix = "l"; break; case "UINT8": suffix = "ub"; break; case "UINT16": suffix = "uh"; break; case "UINT32": suffix = "uw"; break; case "UINT64": suffix = "ul"; break; case "FLOAT32": suffix = "w"; break; // word, meaning 32 bits case "FLOAT64": break; } String literal; if (value.isNumber() && isUnsignedInt(type)) { Number nv = value.getAsNumber(); if ("UINT64".equals(type)) literal = Long.toUnsignedString(nv.longValue()); else if ("UINT32".equals(type)) literal = Integer.toUnsignedString(nv.intValue()); else if ("UINT16".equals(type)) literal = Integer.toUnsignedString(Short.toUnsignedInt(nv.shortValue())); else literal = Integer.toUnsignedString(Byte.toUnsignedInt(nv.byteValue())); } else { literal = value.getAsNumber().toString(); } sb.append(literal); sb.append(suffix); }
From source file:com.ikanow.infinit.e.data_model.store.MongoDbUtil.java
License:Apache License
public static Object encodeUnknown(JsonElement from) { if (from.isJsonArray()) { // Array return encodeArray(from.getAsJsonArray()); } //TESTED/*from w ww . jav a 2 s.co m*/ else if (from.isJsonObject()) { // Object JsonObject obj = from.getAsJsonObject(); // Check for OID/Date: if (1 == obj.entrySet().size()) { if (obj.has("$date")) { try { return _format.parse(obj.get("$date").getAsString()); } catch (ParseException e) { try { return _format2.parse(obj.get("$date").getAsString()); } catch (ParseException e2) { return null; } } } //TESTED else if (obj.has("$oid")) { return new ObjectId(obj.get("$oid").getAsString()); } //TESTED } return encode(obj); } //TESTED else if (from.isJsonPrimitive()) { // Primitive JsonPrimitive val = from.getAsJsonPrimitive(); if (val.isNumber()) { return val.getAsNumber(); } //TESTED else if (val.isBoolean()) { return val.getAsBoolean(); } //TESTED else if (val.isString()) { return val.getAsString(); } //TESTED } //TESTED return null; }
From source file:com.indragie.cmput301as1.DateJSONDeserializer.java
License:Open Source License
@Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isNumber()) { return new Date(primitive.getAsLong()); }/*from w ww .j a v a2s. c o m*/ } return null; }
From source file:com.indragie.cmput301as1.JSONHelpers.java
License:Open Source License
/** * Gets the value of the JSON element as a long if possible. * @param element The JSON element.//from ww w. ja v a2 s. c o m * @return A string if the JSON element was a long, or * 0 otherwise. */ public static long getLongIfPossible(JsonElement element) { if (element == null) return 0; if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isNumber()) { return primitive.getAsLong(); } } return 0; }