Example usage for com.google.gson JsonPrimitive getAsCharacter

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

Introduction

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

Prototype

@Override
    public char getAsCharacter() 

Source Link

Usage

From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonCharacterTypeAdapter.java

License:Apache License

@Override
public Character deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) {
    JsonPrimitive jsonPrimitive = (JsonPrimitive) element;
    if (!jsonPrimitive.isString()) {
        throw new DatastoreException("Invalid value for character type.");
    }/*from   w  w w .  ja  v  a 2  s.  co  m*/
    return jsonPrimitive.getAsCharacter();
}

From source file:com.getperka.flatpack.codexes.CharacterCodex.java

License:Apache License

@Override
public Character readNotNull(JsonElement element, DeserializationContext context) throws Exception {
    if (element.isJsonPrimitive()) {
        JsonPrimitive primitive = element.getAsJsonPrimitive();
        // Treat a number as a BMP code point
        if (primitive.isNumber()) {
            return (char) primitive.getAsInt();
        } else {/*from w w w.  j ava2  s . c  o m*/
            return primitive.getAsCharacter();
        }
    }
    throw new IllegalArgumentException("Cannot convert " + element.toString() + " to a char");
}

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 ava2s . co 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:qa.qcri.qnoise.util.NoiseJsonAdapterDeserializer.java

License:MIT License

@Override
public NoiseJsonAdapter deserialize(JsonElement rootElement, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    NoiseJsonAdapter adapter = new NoiseJsonAdapter();

    JsonObject rootObj = (JsonObject) rootElement;

    // ---------------- parsing source --------------------- //
    JsonObject source = (JsonObject) (rootObj.get("source"));
    Preconditions.checkNotNull(source != null, "Source is not found.");

    JsonPrimitive pathPrimitive = source.getAsJsonPrimitive("path");
    Preconditions.checkNotNull(pathPrimitive);
    adapter.inputFile = pathPrimitive.getAsString();

    JsonArray schemaArray = source.getAsJsonArray("type");
    if (schemaArray != null) {
        adapter.schema = Lists.newArrayList();
        for (JsonElement primitive : schemaArray) {
            adapter.schema.add(primitive.getAsString());
        }/*  www.j  a v a2 s  .c  o m*/
    }

    JsonPrimitive csvSeparator = source.getAsJsonPrimitive("csvSeparator");
    if (csvSeparator != null) {
        adapter.csvSeparator = csvSeparator.getAsCharacter();
    } else {
        adapter.csvSeparator = NoiseJsonAdapter.DEFAULT_CSV_SEPARATOR;
    }

    // ---------------- parsing noises --------------------- //
    JsonElement noiseElement = rootObj.get("noise");
    JsonArray noiseArray;
    if (noiseElement instanceof JsonArray) {
        noiseArray = (JsonArray) (rootObj.get("noise"));
    } else {
        noiseArray = new JsonArray();
        noiseArray.add(noiseElement);
    }

    Preconditions.checkArgument(noiseArray != null && noiseArray.size() > 0,
            "Noise specification is null or empty.");

    adapter.specs = Lists.newArrayList();
    for (JsonElement specJson : noiseArray) {
        JsonObject noiseObj = (JsonObject) specJson;
        NoiseSpec spec = new NoiseSpec();

        JsonPrimitive noiseType = noiseObj.getAsJsonPrimitive("noiseType");
        Preconditions.checkNotNull(noiseType);
        spec.noiseType = NoiseType.fromString(noiseType.getAsString());

        JsonPrimitive granularity = noiseObj.getAsJsonPrimitive("granularity");
        if (granularity != null) {
            spec.granularity = GranularityType.fromString(granularity.getAsString());
        } else {
            // assign default granularity
            if (spec.noiseType == NoiseType.Duplicate)
                spec.granularity = GranularityType.Row;
            else
                spec.granularity = GranularityType.Cell;
        }

        JsonPrimitive percentage = noiseObj.getAsJsonPrimitive("percentage");
        if (percentage != null)
            spec.percentage = percentage.getAsDouble();
        else
            throw new IllegalArgumentException("Percentage cannot be null.");

        JsonPrimitive model = noiseObj.getAsJsonPrimitive("model");
        if (model != null) {
            spec.model = NoiseModel.fromString(model.getAsString());
        } else {
            spec.model = NoiseModel.Random;
        }

        JsonArray column = noiseObj.getAsJsonArray("column");
        if (column != null) {
            spec.filteredColumns = new String[column.size()];
            for (int i = 0; i < column.size(); i++)
                spec.filteredColumns[i] = column.get(i).getAsString();
        }

        JsonPrimitive numberOfSeed = noiseObj.getAsJsonPrimitive("numberOfSeed");
        if (numberOfSeed != null) {
            spec.numberOfSeed = numberOfSeed.getAsDouble();
        }

        JsonArray distance = noiseObj.getAsJsonArray("distance");
        if (distance != null) {
            spec.distance = new double[distance.size()];
            for (int i = 0; i < distance.size(); i++)
                spec.distance[i] = distance.get(i).getAsDouble();
        } else if (spec.filteredColumns != null)
            spec.distance = new double[spec.filteredColumns.length];

        // domain or distance
        if (spec.distance == null) {
            JsonArray domain = noiseObj.getAsJsonArray("domain");
            if (domain != null) {
                spec.distance = new double[domain.size()];
                for (int i = 0; i < domain.size(); i++)
                    spec.distance[i] = domain.get(i).getAsDouble();
            } else if (spec.filteredColumns != null)
                spec.distance = new double[spec.filteredColumns.length];
        }

        JsonArray constraints = noiseObj.getAsJsonArray("constraint");
        if (constraints != null) {
            spec.constraint = new Constraint[constraints.size()];
            for (int i = 0; i < constraints.size(); i++)
                spec.constraint[i] = ConstraintFactory
                        .createConstraintFromString(constraints.get(i).getAsString());
        }

        JsonPrimitive logFile = noiseObj.getAsJsonPrimitive("logFile");
        if (logFile != null) {
            spec.logFile = logFile.getAsString();
        } else {
            Calendar calendar = Calendar.getInstance();
            DateFormat dateFormat = new SimpleDateFormat("MMddHHmmss");
            spec.logFile = "log" + dateFormat.format(calendar.getTime()) + ".csv";
        }
        adapter.specs.add(spec);
    }

    // -------------- verify specifications ------------------- //
    for (NoiseSpec spec : adapter.specs) {
        String errorMessage = NoiseHelper.verify(spec);
        if (errorMessage != null)
            throw new IllegalArgumentException(errorMessage);
    }

    return adapter;
}