Example usage for com.google.gson JsonNull INSTANCE

List of usage examples for com.google.gson JsonNull INSTANCE

Introduction

In this page you can find the example usage for com.google.gson JsonNull INSTANCE.

Prototype

JsonNull INSTANCE

To view the source code for com.google.gson JsonNull INSTANCE.

Click Source Link

Document

singleton for JsonNull

Usage

From source file:edu.oregonstate.eecs.mcplan.search.fsss.experiments.FsssJairExperiments.java

License:Open Source License

private static <S extends State, A extends VirtualConstructor<A>> void writeEpisodeHistory(
        final PrintWriter out, final int episode, final HistoryRecorder<S, ?, A> hacc) throws IOException {
    if (hacc.states.isEmpty()) {
        System.out.println("! Warning: Empty history (writeEpisodeHistory)");
        return;/* w  w  w . j a va 2s  .c  o m*/
    }

    final JsonObject root = new JsonObject();
    root.add("episode", new JsonPrimitive(episode));

    final JsonArray h = new JsonArray();
    root.add("h", h);
    for (int i = 0; i < hacc.actions.size() - 1; ++i) {
        final JsonObject t = new JsonObject();
        t.add("s", ((JsonRepresentation<S>) hacc.states.get(i)).json);
        t.add("a", gson.toJsonTree(hacc.actions.get(i).get(0)));
        t.add("r", gson.toJsonTree(hacc.rewards.get(i)[0]));
        h.add(t);
    }
    final JsonObject terminal = new JsonObject();
    terminal.add("s", ((JsonRepresentation<S>) hacc.states.get(hacc.states.size() - 1)).json);
    terminal.add("a", JsonNull.INSTANCE);
    terminal.add("r", gson.toJsonTree(hacc.rewards.get(hacc.rewards.size() - 1)[0]));
    h.add(terminal);

    gson.toJson(root, out);
    out.println();
    out.flush();
}

From source file:fr.zcraft.MultipleInventories.snaphots.PlayerSnapshot.java

License:Open Source License

/**
 * @return A JSON export of this snapshot (including inventories and {@link
 * ItemStackSnapshot item snapshots}./* w  w  w.j  av a 2 s.  com*/
 */
public JsonElement toJSON() {
    final JsonObject dump = new JsonObject();
    final JsonArray armorDump = new JsonArray();
    final JsonArray effectsDump = new JsonArray();

    Arrays.stream(armor).map(item -> item != null ? item.toJSON() : JsonNull.INSTANCE)
            .forEachOrdered(armorDump::add);

    effects.stream().map(this::toJSON).forEach(effectsDump::add);

    dump.addProperty("level", level);
    dump.addProperty("exp", exp);
    dump.addProperty("expTotal", expTotal);
    dump.addProperty("foodLevel", foodLevel);
    dump.addProperty("exhaustion", exhaustion);
    dump.addProperty("saturation", saturation);
    dump.addProperty("health", health);
    dump.addProperty("maxHealth", maxHealth);

    dump.add("armor", armorDump);
    dump.add("inventory", toJSON(inventory));
    dump.add("enderChest", toJSON(enderChest));
    dump.add("effects", effectsDump);

    return dump;
}

From source file:fr.zcraft.MultipleInventories.snaphots.PlayerSnapshot.java

License:Open Source License

/**
 * @return A JSON export of an inventory.
 *//*  w  ww  . j a v  a  2 s  . c  o  m*/
private JsonElement toJSON(final Map<Integer, ItemStackSnapshot> inventory) {
    final JsonObject dump = new JsonObject();

    inventory.forEach((index, itemSnapshot) -> dump.add(index.toString(),
            itemSnapshot != null ? itemSnapshot.toJSON() : JsonNull.INSTANCE));

    return dump;
}

From source file:fr.zcraft.zbanque.network.packets.PacketPlayOutSilos.java

License:Open Source License

private JsonElement item2WSItem(BlockType item) {
    if (item == null)
        return JsonNull.INSTANCE;

    JsonObject itemJson = new JsonObject();

    itemJson.addProperty("idItem", item.getType().getId());
    itemJson.addProperty("data", item.getType().getMaxDurability());

    return itemJson;
}

From source file:funf.FunfManager.java

License:Open Source License

/**
 * Get a gson builder with the probe factory built in
 * @return//from  w  ww  . j  av  a 2  s .  co m
 */
public static GsonBuilder getGsonBuilder(Context context) {
    return new GsonBuilder().registerTypeAdapterFactory(getProbeFactory(context))
            .registerTypeAdapterFactory(getPipelineFactory(context))
            .registerTypeAdapterFactory(new ConfigurableRuntimeTypeAdapterFactory<Schedule>(context,
                    Schedule.class, BasicSchedule.class))
            .registerTypeAdapterFactory(new ConfigurableRuntimeTypeAdapterFactory<ConfigUpdater>(context,
                    ConfigUpdater.class, HttpConfigUpdater.class))
            .registerTypeAdapterFactory(new ConfigurableRuntimeTypeAdapterFactory<FileArchive>(context,
                    FileArchive.class, DefaultArchive.class))
            .registerTypeAdapterFactory(new ConfigurableRuntimeTypeAdapterFactory<RemoteFileArchive>(context,
                    RemoteFileArchive.class, HttpArchive.class))
            .registerTypeAdapter(DefaultSchedule.class, new DefaultScheduleSerializer())
            .registerTypeAdapter(Class.class, new JsonSerializer<Class<?>>() {

                @Override
                public JsonElement serialize(Class<?> src, Type typeOfSrc, JsonSerializationContext context) {
                    return src == null ? JsonNull.INSTANCE : new JsonPrimitive(src.getName());
                }
            });
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

public SimpleFeature parse(String input) {
    try {/*  ww w .j  a  v a  2s . com*/
        final JsonElement element = jsonParser.parse(input);
        if (element != null && element != JsonNull.INSTANCE) {
            final JsonObject jsonObject = element.getAsJsonObject();
            return convertToFeature(jsonObject, this.builder, geoFac, df);
        }
    } catch (Exception e) {
        log.error("error parsing input: " + input, e);
    }
    return null;
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

public boolean parse(String input, SimpleFeature toWrite) {
    try {//from  w ww. j  ava 2s.c  om
        final JsonElement element = jsonParser.parse(input);
        if (element != null && element != JsonNull.INSTANCE) {
            final JsonObject jsonObject = element.getAsJsonObject();
            fillFeature(jsonObject, toWrite, geoFac, df);
            return true;
        }
    } catch (Exception e) {
        log.error("error parsing input: " + input, e);
    }
    return false;
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private SimpleFeature convertToFeature(final JsonObject obj, final SimpleFeatureBuilder builder,
        final GeometryFactory factory, final DateTimeFormatter df) {
    builder.reset();//from   w w  w.  j a v a2s  .c  o  m

    // user info
    final JsonObject user = obj.getAsJsonObject(USER);
    final String userName = user.getAsJsonPrimitive(USER_NAME).getAsString();
    final String userId = user.getAsJsonPrimitive(USER_ID).getAsString();
    builder.set(TwitterFeatureIngester.FEAT_USER_NAME, utf8(userName));
    builder.set(TwitterFeatureIngester.FEAT_USER_ID, userId);

    builder.set(TwitterFeatureIngester.FEAT_TEXT, utf8(obj.get(TEXT).getAsString()));

    // geo info
    final boolean hasGeoJson = obj.has(COORDS_GEO_JSON) && obj.get(COORDS_GEO_JSON) != JsonNull.INSTANCE;
    if (hasGeoJson) {
        final JsonObject geo = obj.getAsJsonObject(COORDS_GEO_JSON);
        final JsonArray coords = geo.getAsJsonArray(COORDS);
        double lat = coords.get(0).getAsDouble();
        double lon = coords.get(1).getAsDouble();

        if (lon != 0.0 && lat != 0.0) {
            final Coordinate coordinate = new Coordinate(lat, lon);
            final Geometry g = new Point(new CoordinateArraySequence(new Coordinate[] { coordinate }), factory);
            builder.set(TwitterFeatureIngester.FEAT_GEOM, g);
        }
    }

    // time and id
    final String tweetId = obj.get(TWEET_ID).getAsString();
    final Date date = df.parseDateTime(obj.get(CREATED_AT).getAsString()).toDate();
    builder.set(TwitterFeatureIngester.FEAT_TWEET_ID, tweetId);
    builder.set(TwitterFeatureIngester.FEAT_DTG, date);

    if (useExtendedFeatures) {
        conditionalSetString(builder, obj, FEAT_IS_RETWEET);
        conditionalSetString(builder, obj, FEAT_SOURCE);
        conditionalSetString(builder, obj, FEAT_RETWEETS);
        conditionalSetString(builder, obj, FEAT_IN_REPLY_TO_USER_ID);
        conditionalSetString(builder, obj, FEAT_IN_REPLY_TO_USER_NAME);
        conditionalSetString(builder, obj, FEAT_IN_REPLY_TO_STATUS);
        conditionalSetString(builder, obj, FEAT_FILTER_LEVEL);
        conditionalSetString(builder, obj, FEAT_LANGUAGE);
        conditionalSetString(builder, obj, FEAT_WITHHELD_COPYRIGHT);
        conditionalSetString(builder, obj, FEAT_WITHHELD_SCOPE);
        conditionalSetArray(builder, obj, FEAT_WITHHELD_COUNTRIES);

        JsonElement entities = obj.get("entities");
        if (entities != null && entities != JsonNull.INSTANCE) {
            JsonObject e = (JsonObject) entities;
            conditionalSetObjectArray(builder, e, FEAT_HASHTAGS, "text");
            conditionalSetObjectArray(builder, e, FEAT_URLS, "url");
            conditionalSetObjectArray(builder, e, FEAT_SYMBOLS, "text");
            conditionalSetObjectArray(builder, e, FEAT_USER_MENTIONS, "id");
            conditionalSetObjectArray(builder, e, FEAT_MEDIA, "media_url");
        }
    }

    return builder.buildFeature(tweetId);
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private void fillFeature(final JsonObject obj, final SimpleFeature sf, final GeometryFactory factory,
        final DateTimeFormatter df) {

    // user info/*from w w w. ja  va  2  s.c  o m*/
    final JsonObject user = obj.getAsJsonObject(USER);
    final String userName = user.getAsJsonPrimitive(USER_NAME).getAsString();
    final String userId = user.getAsJsonPrimitive(USER_ID).getAsString();
    sf.setAttribute(TwitterFeatureIngester.FEAT_USER_NAME, utf8(userName));
    sf.setAttribute(TwitterFeatureIngester.FEAT_USER_ID, userId);

    sf.setAttribute(TwitterFeatureIngester.FEAT_TEXT, utf8(obj.get(TEXT).getAsString()));

    // geo info
    final boolean hasGeoJson = obj.has(COORDS_GEO_JSON) && obj.get(COORDS_GEO_JSON) != JsonNull.INSTANCE;
    if (hasGeoJson) {
        final JsonObject geo = obj.getAsJsonObject(COORDS_GEO_JSON);
        final JsonArray coords = geo.getAsJsonArray(COORDS);
        double lat = coords.get(0).getAsDouble();
        double lon = coords.get(1).getAsDouble();

        if (lon != 0.0 && lat != 0.0) {
            final Coordinate coordinate = new Coordinate(lat, lon);
            final Geometry g = new Point(new CoordinateArraySequence(new Coordinate[] { coordinate }), factory);
            sf.setAttribute(TwitterFeatureIngester.FEAT_GEOM, g);
        }
    }

    // time and id
    final String tweetId = obj.get(TWEET_ID).getAsString();
    final Date date = df.parseDateTime(obj.get(CREATED_AT).getAsString()).toDate();
    sf.setAttribute(TwitterFeatureIngester.FEAT_TWEET_ID, tweetId);
    sf.setAttribute(TwitterFeatureIngester.FEAT_DTG, date);

    if (useExtendedFeatures) {
        conditionalSetString(sf, obj, FEAT_IS_RETWEET);
        conditionalSetString(sf, obj, FEAT_SOURCE);
        conditionalSetString(sf, obj, FEAT_RETWEETS);
        conditionalSetString(sf, obj, FEAT_IN_REPLY_TO_USER_ID);
        conditionalSetString(sf, obj, FEAT_IN_REPLY_TO_USER_NAME);
        conditionalSetString(sf, obj, FEAT_IN_REPLY_TO_STATUS);
        conditionalSetString(sf, obj, FEAT_FILTER_LEVEL);
        conditionalSetString(sf, obj, FEAT_LANGUAGE);
        conditionalSetString(sf, obj, FEAT_WITHHELD_COPYRIGHT);
        conditionalSetString(sf, obj, FEAT_WITHHELD_SCOPE);
        conditionalSetArray(sf, obj, FEAT_WITHHELD_COUNTRIES);

        JsonElement entities = obj.get("entities");
        if (entities != null && entities != JsonNull.INSTANCE) {
            JsonObject e = (JsonObject) entities;
            conditionalSetObjectArray(sf, e, FEAT_HASHTAGS, "text");
            conditionalSetObjectArray(sf, e, FEAT_URLS, "url");
            conditionalSetObjectArray(sf, e, FEAT_SYMBOLS, "text");
            conditionalSetObjectArray(sf, e, FEAT_USER_MENTIONS, "id");
            conditionalSetObjectArray(sf, e, FEAT_MEDIA, "media_url");
        }
    }

    //((FeatureIdImpl)sf.getIdentifier()).setID(Long.toString(tweetId));
    //sf.getUserData().put(Hints.USE_PROVIDED_FID, Boolean.TRUE);
}

From source file:geomesa.example.twitter.ingest.TwitterParser.java

License:Apache License

private void conditionalSetObjectArray(SimpleFeatureBuilder builder, JsonObject obj, String feature,
        String nestedAttribute) {
    JsonElement object = obj.get(feature);
    if (object != null && object != JsonNull.INSTANCE) {
        List<String> values = new ArrayList<>();
        for (Iterator<JsonElement> iter = ((JsonArray) object).iterator(); iter.hasNext();) {
            JsonElement next = iter.next();
            if (next != null && next != JsonNull.INSTANCE) {
                JsonElement attribute = ((JsonObject) next).get(nestedAttribute);
                if (attribute != null && attribute != JsonNull.INSTANCE) {
                    values.add(attribute.getAsString());
                }//from  w ww .j  ava  2 s . c om
            }
        }
        if (!values.isEmpty()) {
            builder.set(feature, Joiner.on(",").join(values));
        }
    }
}