-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate to moshi to reduce bundle size (#71)
* chore: migrate to moshi to reduce bundle size * Use proguard for moshi
- Loading branch information
1 parent
f1589ce
commit f0cfe13
Showing
13 changed files
with
100 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 57 additions & 9 deletions
66
unleashandroidsdk/src/main/java/io/getunleash/android/data/Parser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,66 @@ | ||
package io.getunleash.android.data | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.SerializationFeature | ||
import com.fasterxml.jackson.databind.util.StdDateFormat | ||
import com.fasterxml.jackson.module.kotlin.KotlinFeature | ||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.JsonReader | ||
import com.squareup.moshi.JsonWriter | ||
import com.squareup.moshi.Moshi | ||
import com.squareup.moshi.Types | ||
import com.squareup.moshi.adapters.Rfc3339DateJsonAdapter | ||
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory | ||
import io.getunleash.android.metrics.MetricsPayload | ||
import io.getunleash.android.polling.ProxyResponse | ||
import java.lang.reflect.Type | ||
import java.util.Date | ||
|
||
object Parser { | ||
val jackson: ObjectMapper = | ||
/*val jackson: ObjectMapper = | ||
jacksonObjectMapper { | ||
enable(KotlinFeature.NullIsSameAsDefault) | ||
}.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) | ||
.setDateFormat( | ||
StdDateFormat().withColonInTimeZone(true) | ||
).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
).disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)*/ | ||
|
||
|
||
|
||
val moshi: Moshi = Moshi.Builder() | ||
.add(KotlinJsonAdapterFactory()) // for Kotlin support | ||
.add(Date::class.java, Rfc3339DateJsonAdapter().nullSafe()) // for date format | ||
// .add(DefaultOnNullAdapterFactory()) | ||
.build() | ||
val proxyResponseAdapter: JsonAdapter<ProxyResponse> = moshi.adapter(ProxyResponse::class.java) | ||
val metricsBodyAdapter: JsonAdapter<MetricsPayload> = moshi.adapter(MetricsPayload::class.java) | ||
|
||
// Define an adapter to handle null values as default | ||
class DefaultOnNullAdapterFactory : JsonAdapter.Factory { | ||
override fun create( | ||
type: Type, | ||
annotations: MutableSet<out Annotation>, | ||
moshi: Moshi | ||
): JsonAdapter<*>? { | ||
val rawType = Types.getRawType(type) | ||
if (rawType.isAnnotationPresent(DefaultOnNull::class.java)) { | ||
val delegate = moshi.nextAdapter<Any>(this, type, annotations) | ||
return object : JsonAdapter<Any>() { | ||
override fun fromJson(reader: JsonReader): Any? { | ||
if (reader.peek() == JsonReader.Token.NULL) { | ||
reader.nextNull<Unit>() | ||
return rawType.getDeclaredConstructor().newInstance() | ||
} | ||
return delegate.fromJson(reader) | ||
} | ||
|
||
override fun toJson(writer: JsonWriter, value: Any?) { | ||
delegate.toJson(writer, value) | ||
} | ||
} | ||
} | ||
return null | ||
} | ||
} | ||
|
||
// Custom annotation to handle default values | ||
@Retention(AnnotationRetention.RUNTIME) | ||
@Target(AnnotationTarget.CLASS) | ||
annotation class DefaultOnNull | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
unleashandroidsdk/src/main/java/io/getunleash/android/data/Variant.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package io.getunleash.android.data | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty | ||
import com.squareup.moshi.Json | ||
|
||
data class Variant( | ||
val name: String, | ||
val enabled: Boolean = true, | ||
@JsonProperty("feature_enabled") val featureEnabled: Boolean = false, | ||
@Json(name = "feature_enabled") val featureEnabled: Boolean = false, | ||
val payload: Payload? = null | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters