Example usage for com.google.gson JsonPrimitive isBoolean

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

Introduction

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

Prototype

public boolean isBoolean() 

Source Link

Document

Check whether this primitive contains a boolean value.

Usage

From source file:com.google.iosched.model.validator.BooleanConverter.java

License:Open Source License

@Override
public JsonPrimitive convert(JsonPrimitive value) {
    if (value == null) {
        return new JsonPrimitive(false);
    }/*  w  w w .j  a  va2s  .  co  m*/
    if (value.isBoolean()) {
        return value;
    }
    String str = value.getAsString();
    if (str.equalsIgnoreCase("false")) {
        return new JsonPrimitive(false);
    }
    if (str.equalsIgnoreCase("true")) {
        return new JsonPrimitive(true);
    }
    throw new ConverterException(value, this);
}

From source file:com.google.samples.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

private boolean isHiddenSession(JsonObject sessionObj) {
    JsonPrimitive hide = getMapValue(get(sessionObj, InputJsonKeys.VendorAPISource.Topics.info),
            InputJsonKeys.VendorAPISource.Topics.INFO_HIDDEN_SESSION, Converters.BOOLEAN, null);
    if (hide != null && hide.isBoolean() && hide.getAsBoolean()) {
        return true;
    }//w ww  .j  a v  a 2  s  . c o  m
    return false;
}

From source file:com.google.samples.apps.iosched.server.schedule.model.DataExtractor.java

License:Open Source License

/** Check whether a session is featured or not.
 *
 * @param sessionObj Session to check//from  www.  j a  v  a2 s  .c  o  m
 * @return True if featured, false otherwise.
 */
private boolean isFeatured(JsonObject sessionObj) {
    // Extract "Featured Session" flag from EventPoint "info" block.
    JsonPrimitive featured = getMapValue(get(sessionObj, InputJsonKeys.VendorAPISource.Topics.info),
            InputJsonKeys.VendorAPISource.Topics.INFO_FEATURED_SESSION, Converters.BOOLEAN, null);
    if (featured != null && featured.isBoolean() && featured.getAsBoolean()) {
        return true;
    }
    return false;
}

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;//from  www .  j  a v a  2  s. 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   w  w w  .j  a  va 2  s .  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.//  w  w  w.j  a va  2 s  . c  o 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 {//from  ww  w  . j a v a2s . c o  m
        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/*from w ww. j  ava  2s . c  om*/
    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.//ww w . j a va2s . c om
 * @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  w  w. ja va2  s.  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;
}