Search in sources :

Example 1 with JsonDeserializer

use of com.google.gson.JsonDeserializer 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 2 with JsonDeserializer

use of com.google.gson.JsonDeserializer in project kie-wb-common by kiegroup.

the class FormDefinitionSerializerImpl method deserialize.

@Override
public FormDefinition deserialize(String serializedForm) {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(FormModel.class, formModelSerializer);
    builder.registerTypeAdapter(FieldDefinition.class, fieldSerializer);
    builder.registerTypeAdapter(ModelProperty.class, (JsonDeserializer<ModelProperty>) (json, typeOfT, context) -> context.deserialize(json, ModelPropertyImpl.class));
    builder.registerTypeAdapter(TypeInfo.class, (JsonDeserializer<TypeInfo>) (json, typeOfT, context) -> context.deserialize(json, TypeInfoImpl.class));
    builder.registerTypeAdapter(ModelMetaData.class, (JsonDeserializer<ModelMetaData>) (json, typeOfT, context) -> context.deserialize(json, ModelMetaDataImpl.class));
    builder.registerTypeAdapter(MetaDataEntry.class, (JsonDeserializer<MetaDataEntry>) (json, typeOfT, context) -> {
        JsonObject jsonField = json.getAsJsonObject();
        JsonElement jsonName = jsonField.get("name");
        return context.deserialize(json, metaDataEntryManager.getMetaDataEntryClass(jsonName.getAsString()));
    });
    Gson gson = builder.create();
    return gson.fromJson(serializedForm, FormDefinition.class);
}
Also used : JsonObject(com.google.gson.JsonObject) ModelMetaData(org.kie.workbench.common.forms.model.ModelMetaData) ModelPropertyImpl(org.kie.workbench.common.forms.model.impl.ModelPropertyImpl) FieldDefinition(org.kie.workbench.common.forms.model.FieldDefinition) ModelProperty(org.kie.workbench.common.forms.model.ModelProperty) FormDefinitionSerializer(org.kie.workbench.common.forms.services.backend.serialization.FormDefinitionSerializer) TypeInfo(org.kie.workbench.common.forms.model.TypeInfo) GsonBuilder(com.google.gson.GsonBuilder) MetaDataEntryManager(org.kie.workbench.common.forms.service.shared.meta.processing.MetaDataEntryManager) Inject(javax.inject.Inject) JsonElement(com.google.gson.JsonElement) FormModel(org.kie.workbench.common.forms.model.FormModel) Dependent(javax.enterprise.context.Dependent) TypeInfoImpl(org.kie.workbench.common.forms.model.impl.TypeInfoImpl) Gson(com.google.gson.Gson) JsonDeserializer(com.google.gson.JsonDeserializer) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition) MetaDataEntry(org.kie.workbench.common.forms.model.MetaDataEntry) ModelMetaDataImpl(org.kie.workbench.common.forms.model.impl.meta.ModelMetaDataImpl) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) ModelProperty(org.kie.workbench.common.forms.model.ModelProperty) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) TypeInfo(org.kie.workbench.common.forms.model.TypeInfo) ModelMetaData(org.kie.workbench.common.forms.model.ModelMetaData) MetaDataEntry(org.kie.workbench.common.forms.model.MetaDataEntry)

Example 3 with JsonDeserializer

use of com.google.gson.JsonDeserializer in project BuildCraft by BuildCraft.

the class JsonUtil method registerNbtSerializersDeserializers.

public static GsonBuilder registerNbtSerializersDeserializers(GsonBuilder gsonBuilder) {
    return gsonBuilder.registerTypeAdapterFactory(new TypeAdapterFactory() {

        @Override
        public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
            return type.getRawType() == NBTBase.class ? new TypeAdapter<T>() {

                @Override
                public void write(JsonWriter out, T value) throws IOException {
                    // noinspection unchecked, RedundantCast
                    Streams.write(((JsonSerializer<T>) (JsonSerializer<NBTBase>) (src, typeOfSrc, context) -> {
                        if (src == NBTUtilBC.NBT_NULL) {
                            return JsonNull.INSTANCE;
                        }
                        switch(src.getId()) {
                            case Constants.NBT.TAG_BYTE:
                                return context.serialize(src, NBTTagByte.class);
                            case Constants.NBT.TAG_SHORT:
                                return context.serialize(src, NBTTagShort.class);
                            case Constants.NBT.TAG_INT:
                                return context.serialize(src, NBTTagInt.class);
                            case Constants.NBT.TAG_LONG:
                                return context.serialize(src, NBTTagLong.class);
                            case Constants.NBT.TAG_FLOAT:
                                return context.serialize(src, NBTTagFloat.class);
                            case Constants.NBT.TAG_DOUBLE:
                                return context.serialize(src, NBTTagDouble.class);
                            case Constants.NBT.TAG_BYTE_ARRAY:
                                return context.serialize(src, NBTTagByteArray.class);
                            case Constants.NBT.TAG_STRING:
                                return context.serialize(src, NBTTagString.class);
                            case Constants.NBT.TAG_LIST:
                                return context.serialize(src, NBTTagList.class);
                            case Constants.NBT.TAG_COMPOUND:
                                return context.serialize(src, NBTTagCompound.class);
                            case Constants.NBT.TAG_INT_ARRAY:
                                return context.serialize(src, NBTTagIntArray.class);
                            default:
                                throw new IllegalArgumentException(src.toString());
                        }
                    }).serialize(value, type.getType(), new JsonSerializationContext() {

                        @Override
                        public JsonElement serialize(Object src) {
                            return gson.toJsonTree(src);
                        }

                        @Override
                        public JsonElement serialize(Object src, Type typeOfSrc) {
                            return gson.toJsonTree(src, typeOfSrc);
                        }
                    }), out);
                }

                @Override
                public T read(JsonReader in) throws IOException {
                    return ((JsonDeserializer<T>) (json, typeOfT, context) -> {
                        if (json.isJsonNull()) {
                            // noinspection unchecked
                            return (T) NBTUtilBC.NBT_NULL;
                        }
                        if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
                            Number number = json.getAsJsonPrimitive().getAsNumber();
                            if (number instanceof BigInteger || number instanceof Long || number instanceof Integer || number instanceof Short || number instanceof Byte) {
                                return context.deserialize(json, NBTTagLong.class);
                            } else {
                                return context.deserialize(json, NBTTagDouble.class);
                            }
                        }
                        if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isBoolean()) {
                            return context.deserialize(new JsonPrimitive(json.getAsJsonPrimitive().getAsBoolean() ? (byte) 1 : (byte) 0), NBTTagByte.class);
                        }
                        if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isString()) {
                            return context.deserialize(json, NBTTagString.class);
                        }
                        if (json.isJsonArray()) {
                            return context.deserialize(json, NBTTagList.class);
                        }
                        if (json.isJsonObject()) {
                            return context.deserialize(json, NBTTagCompound.class);
                        }
                        throw new IllegalArgumentException(json.toString());
                    }).deserialize(Streams.parse(in), type.getType(), gson::fromJson);
                }
            } : null;
        }
    }).registerTypeAdapter(NBTTagByte.class, (JsonSerializer<NBTTagByte>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getByte())).registerTypeAdapter(NBTTagByte.class, (JsonDeserializer<NBTTagByte>) (json, typeOfT, context) -> new NBTTagByte(json.getAsJsonPrimitive().getAsByte())).registerTypeAdapter(NBTTagShort.class, (JsonSerializer<NBTTagShort>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getShort())).registerTypeAdapter(NBTTagShort.class, (JsonDeserializer<NBTTagShort>) (json, typeOfT, context) -> new NBTTagShort(json.getAsJsonPrimitive().getAsShort())).registerTypeAdapter(NBTTagInt.class, (JsonSerializer<NBTTagInt>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getInt())).registerTypeAdapter(NBTTagInt.class, (JsonDeserializer<NBTTagInt>) (json, typeOfT, context) -> new NBTTagInt(json.getAsJsonPrimitive().getAsInt())).registerTypeAdapter(NBTTagLong.class, (JsonSerializer<NBTTagLong>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getLong())).registerTypeAdapter(NBTTagLong.class, (JsonDeserializer<NBTTagLong>) (json, typeOfT, context) -> new NBTTagLong(json.getAsJsonPrimitive().getAsLong())).registerTypeAdapter(NBTTagFloat.class, (JsonSerializer<NBTTagFloat>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getFloat())).registerTypeAdapter(NBTTagFloat.class, (JsonDeserializer<NBTTagFloat>) (json, typeOfT, context) -> new NBTTagFloat(json.getAsJsonPrimitive().getAsFloat())).registerTypeAdapter(NBTTagDouble.class, (JsonSerializer<NBTTagDouble>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getDouble())).registerTypeAdapter(NBTTagDouble.class, (JsonDeserializer<NBTTagDouble>) (json, typeOfT, context) -> new NBTTagDouble(json.getAsJsonPrimitive().getAsDouble())).registerTypeAdapter(NBTTagByteArray.class, (JsonSerializer<NBTTagByteArray>) (src, typeOfSrc, context) -> {
        JsonArray jsonArray = new JsonArray();
        for (byte element : src.getByteArray()) {
            jsonArray.add(new JsonPrimitive(element));
        }
        return jsonArray;
    }).registerTypeAdapter(NBTTagByteArray.class, (JsonDeserializer<NBTTagByteArray>) (json, typeOfT, context) -> new NBTTagByteArray(ArrayUtils.toPrimitive(StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(JsonElement::getAsByte).toArray(Byte[]::new)))).registerTypeAdapter(NBTTagString.class, (JsonSerializer<NBTTagString>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getString())).registerTypeAdapter(NBTTagString.class, (JsonDeserializer<NBTTagString>) (json, typeOfT, context) -> new NBTTagString(json.getAsJsonPrimitive().getAsString())).registerTypeAdapter(NBTTagList.class, (JsonSerializer<NBTTagList>) (src, typeOfSrc, context) -> {
        JsonArray jsonArray = new JsonArray();
        for (int i = 0; i < src.tagCount(); i++) {
            NBTBase element = src.get(i);
            jsonArray.add(context.serialize(element, NBTBase.class));
        }
        return jsonArray;
    }).registerTypeAdapter(NBTTagList.class, (JsonDeserializer<NBTTagList>) (json, typeOfT, context) -> {
        NBTTagList nbtTagList = new NBTTagList();
        StreamSupport.stream(json.getAsJsonArray().spliterator(), false).map(element -> context.<NBTBase>deserialize(element, NBTBase.class)).forEach(nbtTagList::appendTag);
        return nbtTagList;
    }).registerTypeAdapter(NBTTagCompound.class, (JsonSerializer<NBTTagCompound>) (src, typeOfSrc, context) -> {
        JsonObject jsonObject = new JsonObject();
        for (String key : src.getKeySet()) {
            jsonObject.add(key, context.serialize(src.getTag(key), NBTBase.class));
        }
        return jsonObject;
    }).registerTypeAdapter(NBTTagCompound.class, (JsonDeserializer<NBTTagCompound>) (json, typeOfT, context) -> {
        NBTTagCompound nbtTagCompound = new NBTTagCompound();
        for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) {
            nbtTagCompound.setTag(entry.getKey(), context.deserialize(entry.getValue(), NBTBase.class));
        }
        return nbtTagCompound;
    }).registerTypeAdapter(NBTTagIntArray.class, (JsonSerializer<NBTTagIntArray>) (src, typeOfSrc, context) -> {
        JsonArray jsonArray = new JsonArray();
        for (int element : src.getIntArray()) {
            jsonArray.add(new JsonPrimitive(element));
        }
        return jsonArray;
    }).registerTypeAdapter(NBTTagIntArray.class, (JsonDeserializer<NBTTagIntArray>) (json, typeOfT, context) -> new NBTTagIntArray(StreamSupport.stream(json.getAsJsonArray().spliterator(), false).mapToInt(JsonElement::getAsByte).toArray()));
}
Also used : JsonObject(com.google.gson.JsonObject) TypeToken(com.google.gson.reflect.TypeToken) NBTTagByte(net.minecraft.nbt.NBTTagByte) NBTTagByteArray(net.minecraft.nbt.NBTTagByteArray) Constants(net.minecraftforge.common.util.Constants) HashMap(java.util.HashMap) JsonSerializer(com.google.gson.JsonSerializer) ArrayUtils(org.apache.commons.lang3.ArrayUtils) NBTTagString(net.minecraft.nbt.NBTTagString) GsonBuilder(com.google.gson.GsonBuilder) TypeAdapter(com.google.gson.TypeAdapter) JsonReader(com.google.gson.stream.JsonReader) NBTTagFloat(net.minecraft.nbt.NBTTagFloat) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) NBTTagList(net.minecraft.nbt.NBTTagList) ImmutableList(com.google.common.collect.ImmutableList) Gson(com.google.gson.Gson) Map(java.util.Map) JsonSerializationContext(com.google.gson.JsonSerializationContext) BigInteger(java.math.BigInteger) StreamSupport(java.util.stream.StreamSupport) JsonPrimitive(com.google.gson.JsonPrimitive) JsonWriter(com.google.gson.stream.JsonWriter) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) NBTTagInt(net.minecraft.nbt.NBTTagInt) NBTTagDouble(net.minecraft.nbt.NBTTagDouble) NBTTagShort(net.minecraft.nbt.NBTTagShort) Streams(com.google.gson.internal.Streams) ImmutableMap(com.google.common.collect.ImmutableMap) JsonSyntaxException(com.google.gson.JsonSyntaxException) NBTBase(net.minecraft.nbt.NBTBase) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) JsonNull(com.google.gson.JsonNull) Entry(java.util.Map.Entry) JsonDeserializer(com.google.gson.JsonDeserializer) NBTTagLong(net.minecraft.nbt.NBTTagLong) TypeAdapterFactory(com.google.gson.TypeAdapterFactory) JsonPrimitive(com.google.gson.JsonPrimitive) NBTTagByte(net.minecraft.nbt.NBTTagByte) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JsonSerializer(com.google.gson.JsonSerializer) NBTTagString(net.minecraft.nbt.NBTTagString) JsonDeserializer(com.google.gson.JsonDeserializer) NBTTagIntArray(net.minecraft.nbt.NBTTagIntArray) NBTTagList(net.minecraft.nbt.NBTTagList) Entry(java.util.Map.Entry) NBTBase(net.minecraft.nbt.NBTBase) JsonReader(com.google.gson.stream.JsonReader) NBTTagString(net.minecraft.nbt.NBTTagString) NBTTagShort(net.minecraft.nbt.NBTTagShort) TypeAdapterFactory(com.google.gson.TypeAdapterFactory) NBTTagFloat(net.minecraft.nbt.NBTTagFloat) NBTTagInt(net.minecraft.nbt.NBTTagInt) JsonWriter(com.google.gson.stream.JsonWriter) BigInteger(java.math.BigInteger) JsonArray(com.google.gson.JsonArray) Type(java.lang.reflect.Type) NBTTagShort(net.minecraft.nbt.NBTTagShort) JsonElement(com.google.gson.JsonElement) JsonSerializationContext(com.google.gson.JsonSerializationContext) NBTTagByte(net.minecraft.nbt.NBTTagByte) TypeAdapter(com.google.gson.TypeAdapter) NBTTagLong(net.minecraft.nbt.NBTTagLong) BigInteger(java.math.BigInteger) JsonObject(com.google.gson.JsonObject) NBTTagByteArray(net.minecraft.nbt.NBTTagByteArray) NBTTagLong(net.minecraft.nbt.NBTTagLong) NBTTagDouble(net.minecraft.nbt.NBTTagDouble)

Example 4 with JsonDeserializer

use of com.google.gson.JsonDeserializer in project robotcode by OutoftheBoxFTC.

the class LynxModuleMetaList method fromSerializationString.

public static LynxModuleMetaList fromSerializationString(String serialization) {
    JsonDeserializer deserializer = new JsonDeserializer() {

        @Override
        public Object deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
            return context.deserialize(json, LynxModuleMeta.class);
        }
    };
    Gson gson = new GsonBuilder().registerTypeAdapter(RobotCoreLynxModule.class, deserializer).create();
    return gson.fromJson(serialization, LynxModuleMetaList.class);
}
Also used : Type(java.lang.reflect.Type) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) JsonElement(com.google.gson.JsonElement) SimpleGson(org.firstinspires.ftc.robotcore.internal.collections.SimpleGson) Gson(com.google.gson.Gson) JsonDeserializer(com.google.gson.JsonDeserializer)

Example 5 with JsonDeserializer

use of com.google.gson.JsonDeserializer in project msgraph-sdk-java by microsoftgraph.

the class GsonFactory method getGsonInstance.

/**
 * Creates an instance of GSON
 *
 * @param logger the logger
 * @return the new instance
 */
public static Gson getGsonInstance(final ILogger logger) {
    final JsonSerializer<Calendar> calendarJsonSerializer = new JsonSerializer<Calendar>() {

        @Override
        public JsonElement serialize(final Calendar src, final Type typeOfSrc, final JsonSerializationContext context) {
            if (src == null) {
                return null;
            }
            try {
                return new JsonPrimitive(CalendarSerializer.serialize(src));
            } catch (final Exception e) {
                logger.logError(PARSING_MESSAGE + src, e);
                return null;
            }
        }
    };
    final JsonDeserializer<Calendar> calendarJsonDeserializer = new JsonDeserializer<Calendar>() {

        @Override
        public Calendar deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return CalendarSerializer.deserialize(json.getAsString());
            } catch (final ParseException e) {
                logger.logError(PARSING_MESSAGE + json.getAsString(), e);
                return null;
            }
        }
    };
    final JsonSerializer<byte[]> byteArrayJsonSerializer = new JsonSerializer<byte[]>() {

        @Override
        public JsonElement serialize(final byte[] src, final Type typeOfSrc, final JsonSerializationContext context) {
            if (src == null) {
                return null;
            }
            try {
                return new JsonPrimitive(ByteArraySerializer.serialize(src));
            } catch (final Exception e) {
                logger.logError(PARSING_MESSAGE + src, e);
                return null;
            }
        }
    };
    final JsonDeserializer<byte[]> byteArrayJsonDeserializer = new JsonDeserializer<byte[]>() {

        @Override
        public byte[] deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return ByteArraySerializer.deserialize(json.getAsString());
            } catch (final ParseException e) {
                logger.logError(PARSING_MESSAGE + json.getAsString(), e);
                return null;
            }
        }
    };
    final JsonSerializer<DateOnly> dateJsonSerializer = new JsonSerializer<DateOnly>() {

        @Override
        public JsonElement serialize(final DateOnly src, final Type typeOfSrc, final JsonSerializationContext context) {
            if (src == null) {
                return null;
            }
            return new JsonPrimitive(src.toString());
        }
    };
    final JsonDeserializer<DateOnly> dateJsonDeserializer = new JsonDeserializer<DateOnly>() {

        @Override
        public DateOnly deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            try {
                return DateOnly.parse(json.getAsString());
            } catch (final ParseException e) {
                logger.logError(PARSING_MESSAGE + json.getAsString(), e);
                return null;
            }
        }
    };
    final JsonSerializer<EnumSet<?>> enumSetJsonSerializer = new JsonSerializer<EnumSet<?>>() {

        @Override
        public JsonElement serialize(final EnumSet<?> src, final Type typeOfSrc, final JsonSerializationContext context) {
            if (src == null || src.isEmpty()) {
                return null;
            }
            return EnumSetSerializer.serialize(src);
        }
    };
    final JsonDeserializer<EnumSet<?>> enumSetJsonDeserializer = new JsonDeserializer<EnumSet<?>>() {

        @Override
        public EnumSet<?> deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
            if (json == null) {
                return null;
            }
            return EnumSetSerializer.deserialize(typeOfT, json.getAsString());
        }
    };
    final JsonSerializer<Duration> durationJsonSerializer = new JsonSerializer<Duration>() {

        @Override
        public JsonElement serialize(final Duration src, final Type typeOfSrc, final JsonSerializationContext context) {
            return new JsonPrimitive(src.toString());
        }
    };
    final JsonDeserializer<Duration> durationJsonDeserializer = new JsonDeserializer<Duration>() {

        @Override
        public Duration deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
            try {
                return DatatypeFactory.newInstance().newDuration(json.toString());
            } catch (Exception e) {
                return null;
            }
        }
    };
    return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Calendar.class, calendarJsonSerializer).registerTypeAdapter(Calendar.class, calendarJsonDeserializer).registerTypeAdapter(GregorianCalendar.class, calendarJsonSerializer).registerTypeAdapter(GregorianCalendar.class, calendarJsonDeserializer).registerTypeAdapter(byte[].class, byteArrayJsonDeserializer).registerTypeAdapter(byte[].class, byteArrayJsonSerializer).registerTypeAdapter(DateOnly.class, dateJsonSerializer).registerTypeAdapter(DateOnly.class, dateJsonDeserializer).registerTypeAdapter(EnumSet.class, enumSetJsonSerializer).registerTypeAdapter(EnumSet.class, enumSetJsonDeserializer).registerTypeAdapter(Duration.class, durationJsonSerializer).registerTypeAdapter(Duration.class, durationJsonDeserializer).registerTypeAdapterFactory(new FallBackEnumTypeAdapter()).create();
}
Also used : JsonPrimitive(com.google.gson.JsonPrimitive) JsonSerializer(com.google.gson.JsonSerializer) JsonDeserializer(com.google.gson.JsonDeserializer) JsonDeserializationContext(com.google.gson.JsonDeserializationContext) GsonBuilder(com.google.gson.GsonBuilder) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) EnumSet(java.util.EnumSet) GregorianCalendar(java.util.GregorianCalendar) DateOnly(com.microsoft.graph.models.extensions.DateOnly) Duration(javax.xml.datatype.Duration) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException) Type(java.lang.reflect.Type) JsonElement(com.google.gson.JsonElement) JsonSerializationContext(com.google.gson.JsonSerializationContext) JsonParseException(com.google.gson.JsonParseException) ParseException(java.text.ParseException)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)5 JsonDeserializer (com.google.gson.JsonDeserializer)5 Gson (com.google.gson.Gson)4 JsonElement (com.google.gson.JsonElement)4 JsonDeserializationContext (com.google.gson.JsonDeserializationContext)2 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 JsonSerializationContext (com.google.gson.JsonSerializationContext)2 JsonSerializer (com.google.gson.JsonSerializer)2 Type (java.lang.reflect.Type)2 SharedPreferences (android.content.SharedPreferences)1 CachedField (com.byoutline.cachedfield.CachedField)1 CachedFieldWithArg (com.byoutline.cachedfield.CachedFieldWithArg)1 RetrofitHelper.apiValueProv (com.byoutline.ibuscachedfield.util.RetrofitHelper.apiValueProv)1 KickMaterialApp (com.byoutline.kickmaterial.KickMaterialApp)1 KickMaterialRequestInterceptor (com.byoutline.kickmaterial.api.KickMaterialRequestInterceptor)1 KickMaterialService (com.byoutline.kickmaterial.api.KickMaterialService)1 com.byoutline.kickmaterial.events (com.byoutline.kickmaterial.events)1 AccessTokenProvider (com.byoutline.kickmaterial.managers.AccessTokenProvider)1 LoginManager (com.byoutline.kickmaterial.managers.LoginManager)1