Search in sources :

Example 36 with GsonBuilder

use of com.google.gson.GsonBuilder in project ninja by ninjaframework.

the class ApiControllerDocTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 37 with GsonBuilder

use of com.google.gson.GsonBuilder in project ninja by ninjaframework.

the class ApiControllerDocTesterTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 38 with GsonBuilder

use of com.google.gson.GsonBuilder in project ninja by ninjaframework.

the class ApiControllerTest method getGsonWithLongToDateParsing.

private Gson getGsonWithLongToDateParsing() {
    // Creates the json object which will manage the information received
    GsonBuilder builder = new GsonBuilder();
    // Register an adapter to manage the date types as long values
    builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {

        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }
    });
    Gson gson = builder.create();
    return gson;
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) Date(java.util.Date)

Example 39 with GsonBuilder

use of com.google.gson.GsonBuilder in project kickmaterial by byoutline.

the class GlobalModule method providesGson.

@Provides
Gson providesGson() {
    GsonBuilder builder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
    JsonDeserializer<DateTime> deserializer = (json, typeOfT, context) -> new DateTime(json.getAsJsonPrimitive().getAsLong() * 1000);
    builder.registerTypeAdapter(DateTime.class, deserializer);
    return builder.create();
}
Also used : OttoObservableCachedFieldWithArgBuilder(com.byoutline.ottocachedfield.OttoObservableCachedFieldWithArgBuilder) Bus(com.squareup.otto.Bus) CachedField(com.byoutline.cachedfield.CachedField) GsonBuilder(com.google.gson.GsonBuilder) KickMaterialService(com.byoutline.kickmaterial.api.KickMaterialService) KickMaterialRequestInterceptor(com.byoutline.kickmaterial.api.KickMaterialRequestInterceptor) Picasso(com.squareup.picasso.Picasso) FieldNamingPolicy(com.google.gson.FieldNamingPolicy) Module(dagger.Module) ObservableCachedFieldWithArg(com.byoutline.observablecachedfield.ObservableCachedFieldWithArg) Gson(com.google.gson.Gson) RetrofitHelper.apiValueProv(com.byoutline.ibuscachedfield.util.RetrofitHelper.apiValueProv) GsonConverterFactory(retrofit2.converter.gson.GsonConverterFactory) OttoCachedFieldBuilder(com.byoutline.ottocachedfield.OttoCachedFieldBuilder) KickMaterialApp(com.byoutline.kickmaterial.KickMaterialApp) LruCacheWithPlaceholders(com.byoutline.kickmaterial.utils.LruCacheWithPlaceholders) Nullable(javax.annotation.Nullable) Provides(dagger.Provides) CachedFieldWithArg(com.byoutline.cachedfield.CachedFieldWithArg) com.byoutline.kickmaterial.events(com.byoutline.kickmaterial.events) AccessTokenProvider(com.byoutline.kickmaterial.managers.AccessTokenProvider) DateTime(org.joda.time.DateTime) LoginManager(com.byoutline.kickmaterial.managers.LoginManager) Retrofit(retrofit2.Retrofit) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) SharedPreferences(android.content.SharedPreferences) JsonDeserializer(com.google.gson.JsonDeserializer) com.byoutline.kickmaterial.model(com.byoutline.kickmaterial.model) OttoCachedFieldWithArgBuilder(com.byoutline.ottocachedfield.OttoCachedFieldWithArgBuilder) GsonBuilder(com.google.gson.GsonBuilder) DateTime(org.joda.time.DateTime) Provides(dagger.Provides)

Example 40 with GsonBuilder

use of com.google.gson.GsonBuilder in project jphp by jphp-compiler.

the class JsonExtension method createGsonBuilderForDecode.

public static GsonBuilder createGsonBuilderForDecode(MemoryDeserializer memoryDeserializer) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Memory.class, memoryDeserializer);
    return builder;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)268 Gson (com.google.gson.Gson)175 IOException (java.io.IOException)44 ArrayList (java.util.ArrayList)28 JsonElement (com.google.gson.JsonElement)27 Type (java.lang.reflect.Type)27 Map (java.util.Map)26 JsonObject (com.google.gson.JsonObject)20 HashMap (java.util.HashMap)20 Test (org.junit.Test)18 JsonSyntaxException (com.google.gson.JsonSyntaxException)15 Date (java.util.Date)14 File (java.io.File)13 InputStream (java.io.InputStream)13 List (java.util.List)13 OkHttpClient (okhttp3.OkHttpClient)13 Retrofit (retrofit2.Retrofit)13 InputStreamReader (java.io.InputStreamReader)11 JsonParseException (com.google.gson.JsonParseException)9 JsonParser (com.google.gson.JsonParser)9