Skip to content

Commit

Permalink
Merge pull request #2290 from wordpress-mobile/woo/update-shipping-an…
Browse files Browse the repository at this point in the history
…d-fee-lines

[Woo] ability to serialize null values for shipping and fee lines to support deletion
  • Loading branch information
ThomazFB authored Feb 23, 2022
2 parents 71f2665 + 61c14a3 commit ac35815
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.wordpress.android.fluxc.model.order

import com.google.gson.annotations.JsonAdapter
import com.google.gson.annotations.SerializedName
import org.wordpress.android.fluxc.utils.NullStringJsonAdapter

/**
* Represents a fee line.
Expand All @@ -10,6 +12,7 @@ class FeeLine {
var id: Long? = null

@SerializedName("name")
@JsonAdapter(NullStringJsonAdapter::class, nullSafe = false)
var name: String? = null

@SerializedName("total")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.wordpress.android.fluxc.model.order

import com.google.gson.annotations.JsonAdapter
import com.google.gson.annotations.SerializedName
import org.wordpress.android.fluxc.utils.NullStringJsonAdapter

data class ShippingLine(
val id: Long? = null,
val total: String? = null,
@SerializedName("total_tax")
val totalTax: String? = null,
@SerializedName("method_id")
@JsonAdapter(NullStringJsonAdapter::class, nullSafe = false)
val methodId: String? = null,
@SerializedName("method_title")
val methodTitle: String? = null
Expand Down
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")
}
}
}
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"""")
}
}

0 comments on commit ac35815

Please sign in to comment.