List of usage examples for com.google.gson JsonPrimitive getAsBoolean
@Override public boolean getAsBoolean()
From source file:com.gst.infrastructure.core.serialization.JsonParserHelper.java
License:Apache License
public Boolean extractBooleanNamed(final String parameterName, final JsonElement element, final Set<String> requestParamatersDetected) { Boolean value = null;/* ww w. j a v a2s . co m*/ if (element.isJsonObject()) { final JsonObject object = element.getAsJsonObject(); if (object.has(parameterName) && object.get(parameterName).isJsonPrimitive()) { requestParamatersDetected.add(parameterName); final JsonPrimitive primitive = object.get(parameterName).getAsJsonPrimitive(); value = primitive.getAsBoolean(); } } return value; }
From source file:com.haulmont.restapi.controllers.EntitiesController.java
License:Apache License
@PostMapping("/{entityName}/search") public ResponseEntity<String> searchEntitiesListPost(@PathVariable String entityName, @RequestBody String requestBodyJson) { EntitiesSearchResult entitiesSearchResult = entitiesControllerManager.searchEntities(entityName, requestBodyJson);//from ww w . j a v a 2 s. c o m ResponseEntity.BodyBuilder responseBuilder = ResponseEntity.status(HttpStatus.OK); JsonObject requestJsonObject = new JsonParser().parse(requestBodyJson).getAsJsonObject(); JsonPrimitive returnCount = requestJsonObject.getAsJsonPrimitive("returnCount"); if (returnCount != null && returnCount.getAsBoolean()) { responseBuilder.header("X-Total-Count", entitiesSearchResult.getCount().toString()); } return responseBuilder.body(entitiesSearchResult.getJson()); }
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;/* ww w .j av a2s . c om*/ 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// w w w .j a v a 2s.c o m * @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./*from w w w .j a v a2s . com*/ * @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 {//from www . java 2 s . c om throw new GIPEventCodeDecodeErrorException("", "Unable to decode JsonValue."); } }
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/* w w w .j a v a 2s . 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.JSONHelpers.java
License:Open Source License
/** * Gets the value of the JSON element as a boolean if possible. * @param element The JSON element./*from w ww . j a v a 2s . co m*/ * @return A string if the JSON element was a boolean, or * false otherwise. */ public static boolean getBooleanIfPossible(JsonElement element) { if (element == null) return false; if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } } return false; }
From source file:com.jayway.jsonpath.internal.spi.json.GsonJsonProvider.java
License:Apache License
public static Object unwrap(Object o) { if (o == null) { return null; }//from w ww. j av a 2s .c o m 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; }
From source file:com.jayway.jsonpath.internal.spi.mapper.GsonMapper.java
License:Apache License
@Override public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) { assertValidConversion(src, srcType, targetType); if (src == null || src.getClass().equals(JsonNull.class)) { return null; }/*from ww w . j a va 2 s.c o m*/ if (JsonPrimitive.class.isAssignableFrom(srcType)) { JsonPrimitive primitive = (JsonPrimitive) src; if (targetType.equals(Long.class)) { return primitive.getAsLong(); } else if (targetType.equals(Integer.class)) { return primitive.getAsInt(); } else if (targetType.equals(BigInteger.class)) { return primitive.getAsBigInteger(); } else if (targetType.equals(Byte.class)) { return primitive.getAsByte(); } else if (targetType.equals(BigDecimal.class)) { return primitive.getAsBigDecimal(); } else if (targetType.equals(Double.class)) { return primitive.getAsDouble(); } else if (targetType.equals(Float.class)) { return primitive.getAsFloat(); } else if (targetType.equals(String.class)) { return primitive.getAsString(); } else if (targetType.equals(Boolean.class)) { return primitive.getAsBoolean(); } else if (targetType.equals(Date.class)) { if (primitive.isNumber()) { return new Date(primitive.getAsLong()); } else if (primitive.isString()) { try { return DateFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { throw new MappingException(e); } } } } else if (JsonObject.class.isAssignableFrom(srcType)) { JsonObject srcObject = (JsonObject) src; if (targetType.equals(Map.class)) { Map<String, Object> targetMap = new LinkedHashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) { Object val = null; JsonElement element = entry.getValue(); if (element.isJsonPrimitive()) { val = GsonJsonProvider.unwrap(element); } else if (element.isJsonArray()) { val = convert(element, element.getClass(), List.class, conf); } else if (element.isJsonObject()) { val = convert(element, element.getClass(), Map.class, conf); } else if (element.isJsonNull()) { val = null; } targetMap.put(entry.getKey(), val); } return targetMap; } } else if (JsonArray.class.isAssignableFrom(srcType)) { JsonArray srcArray = (JsonArray) src; if (targetType.equals(List.class)) { List<Object> targetList = new ArrayList<Object>(); for (JsonElement element : srcArray) { if (element.isJsonPrimitive()) { targetList.add(GsonJsonProvider.unwrap(element)); } else if (element.isJsonArray()) { targetList.add(convert(element, element.getClass(), List.class, conf)); } else if (element.isJsonObject()) { targetList.add(convert(element, element.getClass(), Map.class, conf)); } else if (element.isJsonNull()) { targetList.add(null); } } return targetList; } } throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName()); }