Example usage for com.google.gson JsonPrimitive isString

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

Introduction

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

Prototype

public boolean isString() 

Source Link

Document

Check whether this primitive contains a String value.

Usage

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));
    }//www .j ava 2  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;/*from w w  w  . j a v a2 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 om*/
 * @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 .jav a 2  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 {//from  w  w w. j a v a2s .  c o m
        throw new GIPEventCodeDecodeErrorException("", "Unable to decode JsonValue.");
    }

}

From source file:com.ibm.common.activitystreams.internal.EnumAdapter.java

License:Apache License

/**
 * Method deserialize./*from   w  w  w  .  ja v a  2 s  .c om*/
 * @param json JsonElement
 * @param typeOfT Type
 * @param context JsonDeserializationContext
 * @return E
 * @throws JsonParseException
 * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext)
 */
public E deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    checkArgument(json.isJsonPrimitive());
    JsonPrimitive jp = json.getAsJsonPrimitive();
    checkArgument(jp.isString());
    return des.convert(jp.getAsString());
}

From source file:com.ibm.common.activitystreams.internal.LinkValueAdapter.java

License:Apache License

/**
 * Method deserialize.//  w  ww.ja  v  a 2  s.  c o  m
 * @param el JsonElement
 * @param type Type
 * @param context JsonDeserializationContext
        
        
        
 * @return LinkValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
public LinkValue deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    checkArgument(el.isJsonArray() || el.isJsonObject() || el.isJsonPrimitive());
    if (el.isJsonArray()) {
        LinkValue.ArrayLinkValue.Builder builder = linkValues();
        for (JsonElement aryel : el.getAsJsonArray())
            builder.add(context.<LinkValue>deserialize(aryel, LinkValue.class));
        return builder.get();
    } else if (el.isJsonObject()) {
        JsonObject obj = el.getAsJsonObject();
        if (obj.has("objectType")) {
            TypeValue tv = context.deserialize(obj.get("objectType"), TypeValue.class);
            Model pMap = schema.forObjectType(tv.id());
            return context.deserialize(el, pMap != null && pMap.type() != null ? pMap.type() : ASObject.class);
        } else {
            return context.deserialize(el, ASObject.class);
        }
    } else {
        JsonPrimitive prim = el.getAsJsonPrimitive();
        checkArgument(prim.isString());
        return linkValue(prim.getAsString());
    }
}

From source file:com.ibm.common.activitystreams.internal.NaturalLanguageValueAdapter.java

License:Apache License

/**
 * Method deserialize.//from w w w.ja va  2 s.  c  o m
 * @param element JsonElement
 * @param type1 Type
 * @param context JsonDeserializationContext
        
        
        
 * @return NLV * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
public NLV deserialize(JsonElement element, Type type1, JsonDeserializationContext context)
        throws JsonParseException {
    checkArgument(element.isJsonPrimitive() || element.isJsonObject());
    if (element.isJsonPrimitive()) {
        JsonPrimitive prim = element.getAsJsonPrimitive();
        checkArgument(prim.isString());
        return NLV.SimpleNLV.make(prim.getAsString());
    } else {
        try {
            JsonObject obj = element.getAsJsonObject();
            NLV.MapNLV.Builder builder = NLV.MapNLV.make();
            for (Entry<String, JsonElement> entry : obj.entrySet())
                builder.set(entry.getKey(), entry.getValue().getAsString());
            return builder.get();
        } catch (Throwable t) {
            throw new IllegalArgumentException();
        }
    }
}

From source file:com.ibm.common.activitystreams.internal.TypeValueAdapter.java

License:Apache License

/**
 * Method deserialize.//  w  ww . j  av  a2s .co m
 * @param el JsonElement
 * @param type Type
 * @param context JsonDeserializationContext
        
        
        
 * @return TypeValue * @throws JsonParseException * @see com.google.gson.JsonDeserializer#deserialize(JsonElement, Type, JsonDeserializationContext) */
public TypeValue deserialize(JsonElement el, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    checkArgument(el.isJsonPrimitive() || el.isJsonObject());
    if (el.isJsonPrimitive()) {
        JsonPrimitive prim = el.getAsJsonPrimitive();
        checkArgument(prim.isString());
        return type(prim.getAsString());
    } else {
        JsonObject obj = el.getAsJsonObject();
        if (obj.has("objectType")) {
            TypeValue tv = context.deserialize(obj.get("objectType"), TypeValue.class);
            Model pMap = schema.forObjectType(tv.id());
            return context.<ASObject>deserialize(el, pMap.type() != null ? pMap.type() : ASObject.class);
        } else {
            return context.<ASObject>deserialize(el, ASObject.class);
        }
    }
}

From source file:com.ibm.streamsx.topology.generator.spl.SPLGenerator.java

License:Open Source License

/**
 * Add an arbitrary SPL value.// w w w  .  j a v  a  2s. c om
 * JsonObject has a type and a value. 
 */
static void value(StringBuilder sb, JsonObject tv) {

    JsonElement value = tv.get("value");

    String type = JParamTypes.TYPE_SPL_EXPRESSION;
    if (tv.has("type")) {
        type = tv.get("type").getAsString();
    } else {
        if (value.isJsonPrimitive()) {
            JsonPrimitive pv = value.getAsJsonPrimitive();
            if (pv.isString())
                type = "RSTRING";
        } else if (value.isJsonArray()) {
            type = "RSTRING";
        }
    }

    if (value.isJsonArray()) {
        JsonArray array = value.getAsJsonArray();

        for (int i = 0; i < array.size(); i++) {
            if (i != 0)
                sb.append(", ");
            value(sb, type, array.get(i));
        }
    } else {
        value(sb, type, value);
    }
}