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:com.vmware.xenon.common.serialization.ObjectCollectionTypeConverter.java

License:Open Source License

@Override
public JsonElement serialize(Collection<Object> set, Type type, JsonSerializationContext context) {
    JsonArray setObject = new JsonArray();
    for (Object e : set) {
        if (e == null) {
            setObject.add(JsonNull.INSTANCE);
        } else if (e instanceof JsonElement) {
            setObject.add((JsonElement) e);
        } else {//  w w w  .  jav  a 2 s.  c om
            setObject.add(context.serialize(e));
        }
    }
    return setObject;
}

From source file:de.dentrassi.pm.maven.NodeAdapter.java

License:Open Source License

@Override
public JsonElement serialize(final Node node, final Type type, final JsonSerializationContext ctx) {
    if (node == null) {
        return JsonNull.INSTANCE;
    }/*  w w w. j  av a  2  s .  co  m*/

    final JsonObject o = new JsonObject();

    if (node instanceof ContentNode && !(node instanceof DataNode)) {
        final ContentNode cnode = (ContentNode) node;
        o.addProperty("type", DataNode.class.getSimpleName());
        o.add("node", ctx.serialize(new DataNode(cnode.getData(), cnode.getMimeType())));
    } else {
        o.addProperty("type", node.getClass().getSimpleName());
        o.add("node", ctx.serialize(node));
    }

    return o;
}

From source file:de.symeda.sormas.app.rest.RetroProvider.java

License:Open Source License

private RetroProvider(Context context)
        throws ServerConnectionException, ServerCommunicationException, ApiVersionException {

    lastConnectionId = this.hashCode();

    this.context = context;

    RuntimeTypeAdapterFactory<ClassificationCriteriaDto> classificationCriteriaFactory = RuntimeTypeAdapterFactory
            .of(ClassificationCriteriaDto.class, "type")
            .registerSubtype(ClassificationAllOfCriteriaDto.class, "ClassificationAllOfCriteriaDto")
            .registerSubtype(ClassificationCaseCriteriaDto.class, "ClassificationCaseCriteriaDto")
            .registerSubtype(ClassificationNoneOfCriteriaDto.class, "ClassificationNoneOfCriteriaDto")
            .registerSubtype(ClassificationPersonAgeBetweenYearsCriteriaDto.class,
                    "ClassificationPersonAgeBetweenYearsCriteriaDto")
            .registerSubtype(ClassificationPathogenTestPositiveResultCriteriaDto.class,
                    "ClassificationPathogenTestPositiveResultCriteriaDto")
            .registerSubtype(ClassificationXOfCriteriaDto.class, "ClassificationXOfCriteriaDto")
            .registerSubtype(ClassificationEpiDataCriteriaDto.class, "ClassificationEpiDataCriteriaDto")
            .registerSubtype(ClassificationNotInStartDateRangeCriteriaDto.class,
                    "ClassificationNotInStartDateRangeCriteriaDto")
            .registerSubtype(ClassificationSymptomsCriteriaDto.class, "ClassificationSymptomsCriteriaDto")
            .registerSubtype(ClassificationPathogenTestCriteriaDto.class,
                    "ClassificationPathogenTestCriteriaDto")
            .registerSubtype(ClassificationXOfCriteriaDto.ClassificationXOfSubCriteriaDto.class,
                    "ClassificationXOfSubCriteriaDto")
            .registerSubtype(ClassificationXOfCriteriaDto.ClassificationOneOfCompactCriteriaDto.class,
                    "ClassificationOneOfCompactCriteriaDto")
            .registerSubtype(ClassificationAllOfCriteriaDto.ClassificationAllOfCompactCriteriaDto.class,
                    "ClassificationAllOfCompactCriteriaDto");

    Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            if (json.isJsonNull()) {
                return null;
            }//w ww . j av  a  2 s . c  o m
            long milliseconds = json.getAsLong();
            return new Date(milliseconds);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            if (src == null) {
                return JsonNull.INSTANCE;
            }
            return new JsonPrimitive(src.getTime());
        }
    }).registerTypeAdapterFactory(classificationCriteriaFactory).create();

    // Basic auth as explained in https://futurestud.io/tutorials/android-basic-authentication-with-retrofit

    String authToken = Credentials.basic(ConfigProvider.getUsername(), ConfigProvider.getPassword());
    AuthenticationInterceptor interceptor = new AuthenticationInterceptor(authToken);

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.connectTimeout(20, TimeUnit.SECONDS);
    httpClient.readTimeout(60, TimeUnit.SECONDS); // for infrastructure data
    httpClient.writeTimeout(30, TimeUnit.SECONDS);

    // adds "Accept-Encoding: gzip" by default
    httpClient.addInterceptor(interceptor);

    // header for logging purposes
    httpClient.addInterceptor(chain -> {

        Request original = chain.request();
        Request.Builder builder = original.newBuilder();

        User user = ConfigProvider.getUser();
        if (user != null) {
            builder.header("User", DataHelper.getShortUuid(user.getUuid()));
            builder.header("Connection", String.valueOf(lastConnectionId)); // not sure if this is a good solution
        }

        builder.method(original.method(), original.body());
        return chain.proceed(builder.build());
    });

    retrofit = new Retrofit.Builder().baseUrl(ConfigProvider.getServerRestUrl())
            .addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient.build()).build();

    checkCompatibility();

    updateLocale();
}

From source file:de.winniehell.battlebeavers.storage.SoldierSerializer.java

License:Open Source License

@Override
public JsonElement serialize(final Soldier pSrc, final Type pType, final JsonSerializationContext pContext) {
    if (pSrc == null) {
        return JsonNull.INSTANCE;
    }//  w  ww .ja va  2  s  .c o  m

    final JsonObject object = new JsonObject();

    object.add("id", pContext.serialize(pSrc.getId()));
    object.add("team", pContext.serialize(pSrc.getTeam()));
    object.add("tile", pContext.serialize(pSrc.getTile()));
    object.add("view_angle", pContext.serialize(pSrc.getRotation()));
    object.add("waypoints", pContext.serialize(pSrc.getWaypoints()));
    object.add("hp", pContext.serialize(pSrc.getHP()));

    return object;
}

From source file:de.winniehell.battlebeavers.storage.StepSerializer.java

License:Open Source License

@Override
public JsonElement serialize(final Step pSrc, final Type pType, final JsonSerializationContext pContext) {
    if (pSrc == null) {
        return JsonNull.INSTANCE;
    }// ww w . jav a 2s . co  m

    final JsonArray array = new JsonArray();

    array.add(pContext.serialize(pSrc.getTileColumn()));
    array.add(pContext.serialize(pSrc.getTileRow()));

    return array;
}

From source file:de.winniehell.battlebeavers.storage.TileSerializer.java

License:Open Source License

@Override
public JsonElement serialize(final Tile pSrc, final Type pType, final JsonSerializationContext pContext) {
    if (pSrc == null) {
        return JsonNull.INSTANCE;
    }//w  ww  .j a v  a 2s .c o  m

    final JsonArray array = new JsonArray();

    array.add(pContext.serialize(pSrc.getColumn()));
    array.add(pContext.serialize(pSrc.getRow()));

    return array;
}

From source file:de.winniehell.battlebeavers.storage.WayPointSerializer.java

License:Open Source License

@Override
public JsonElement serialize(final WayPoint pSrc, final Type pType, final JsonSerializationContext pContext) {
    if (pSrc == null) {
        return JsonNull.INSTANCE;
    }/*  w ww.j  av  a2  s.c o m*/

    final JsonObject object = new JsonObject();

    object.add("path", pContext.serialize(pSrc.getPath()));
    object.add("tile", pContext.serialize(pSrc.getTile()));

    if (pSrc.getAim() == null) {
        object.add("aim", JsonNull.INSTANCE);
    } else {
        object.add("aim", pContext.serialize(pSrc.getAim().getTile()));
    }

    if (pSrc.getWait() > 0) {
        object.addProperty("wait", pSrc.getWait());
    }

    return object;
}

From source file:de.winniehell.battlebeavers.storage.WeightedPathSerializer.java

License:Open Source License

@Override
public JsonElement serialize(final WeightedPath pSrc, final Type pType,
        final JsonSerializationContext pContext) {
    if (pSrc == null) {
        return JsonNull.INSTANCE;
    }//from   w  w w . java  2  s  .co m

    final JsonArray array = new JsonArray();

    for (int i = 0; i < pSrc.getLength(); ++i) {
        array.add(pContext.serialize(pSrc.getStep(i)));
    }

    return array;
}

From source file:edu.mit.media.funf.FunfManager.java

License:Open Source License

/**
 * Get a gson builder with the probe factory built in
 * @return//from  w w  w  .ja  v a2 s .com
 */
public static GsonBuilder getGsonBuilder(Context context) {
    return new GsonBuilder().registerTypeAdapterFactory(getProbeFactory(context))
            .registerTypeAdapterFactory(getActionFactory(context))
            .registerTypeAdapterFactory(getPipelineFactory(context))
            .registerTypeAdapterFactory(getDataSourceFactory(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))
            .registerTypeAdapterFactory(
                    new ConfigurableRuntimeTypeAdapterFactory<DataListener>(context, DataListener.class, null))
            .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:edu.mit.media.funf.json.JsonUtils.java

License:Open Source License

/**
 * Returns a deep copy of the JsonElement.  This is not thread safe.
 * @param el//  w  w w .  jav  a2 s.  co m
 * @return
 */
@SuppressWarnings("unchecked")
public static <T extends JsonElement> T deepCopy(T el) {
    if (el == null) {
        return (T) JsonNull.INSTANCE;
    } else if (el.isJsonArray()) {
        JsonArray array = new JsonArray();
        for (JsonElement subEl : el.getAsJsonArray()) {
            array.add(deepCopy(subEl));
        }
        return (T) array;
    } else if (el.isJsonObject()) {
        JsonObject object = new JsonObject();
        for (Map.Entry<String, JsonElement> entry : el.getAsJsonObject().entrySet()) {
            object.add(entry.getKey(), deepCopy(entry.getValue()));
        }
        return (T) object;
    } else {
        return (T) el;
    }
}