-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2290 from wordpress-mobile/woo/update-shipping-an…
…d-fee-lines [Woo] ability to serialize null values for shipping and fee lines to support deletion
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 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
3 changes: 3 additions & 0 deletions
3
plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/order/ShippingLine.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
32 changes: 32 additions & 0 deletions
32
...ns/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/utils/NullStringJsonAdapter.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.wordpress.android.fluxc.utils | ||
|
||
import com.google.gson.TypeAdapter | ||
import com.google.gson.stream.JsonReader | ||
import com.google.gson.stream.JsonToken.NULL | ||
import com.google.gson.stream.JsonToken.STRING | ||
import com.google.gson.stream.JsonWriter | ||
import com.google.gson.stream.MalformedJsonException | ||
|
||
class NullStringJsonAdapter : TypeAdapter<String>() { | ||
override fun write(out: JsonWriter, value: String?) { | ||
val defaultSerializeNullValue = out.serializeNulls | ||
out.serializeNulls = true | ||
if (value == null) { | ||
out.nullValue() | ||
} else { | ||
out.value(value) | ||
} | ||
out.serializeNulls = defaultSerializeNullValue | ||
} | ||
|
||
override fun read(input: JsonReader): String? { | ||
return when (val token = input.peek()) { | ||
STRING -> input.nextString() | ||
NULL -> { | ||
input.nextNull() | ||
null | ||
} | ||
else -> throw MalformedJsonException("Unexpected token: $token") | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...woocommerce/src/test/java/org/wordpress/android/fluxc/utils/NullStringJsonAdapterTests.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.wordpress.android.fluxc.utils | ||
|
||
import com.google.gson.Gson | ||
import com.google.gson.annotations.JsonAdapter | ||
import com.google.gson.annotations.SerializedName | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.Test | ||
|
||
class NullStringJsonAdapterTests { | ||
data class Example( | ||
@JsonAdapter(NullStringJsonAdapter::class, nullSafe = false) | ||
@SerializedName("an_id") | ||
val id: String? | ||
) | ||
|
||
private val gson = Gson() | ||
|
||
@Test | ||
fun `when passing null in json, then it should be deserialized to null value`() { | ||
val json = """{ | ||
"an_id": null | ||
}""" | ||
|
||
val example = gson.fromJson(json, Example::class.java) | ||
|
||
assertThat(example.id).isNull() | ||
} | ||
|
||
@Test | ||
fun `when serializing a null value, then it should be exposed to the json`() { | ||
val example = Example(null) | ||
|
||
val json = gson.toJson(example) | ||
|
||
assertThat(json).contains(""""an_id":null""") | ||
} | ||
|
||
@Test | ||
fun `when passing non-null value in json, then it should be deserialized to the correct value`() { | ||
val json = """{ | ||
"an_id": "some_id" | ||
}""" | ||
|
||
val example = gson.fromJson(json, Example::class.java) | ||
|
||
assertThat(example.id).isEqualTo("some_id") | ||
} | ||
|
||
@Test | ||
fun `when serializing a non-null value, then it should be correctly serialized`() { | ||
val example = Example("some_id") | ||
|
||
val json = gson.toJson(example) | ||
|
||
assertThat(json).contains(""""an_id":"some_id"""") | ||
} | ||
} |