Skip to content

Commit

Permalink
Fix #234
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbdean committed Mar 11, 2018
1 parent 1c09228 commit 19944e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,29 @@ class RedditModelAdapterFactory(
}

override fun toJson(writer: JsonWriter, value: Listing<Any>?) {
TODO("not implemented")
if (value == null) {
writer.nullValue()
return
}

writer.beginObject()
writer.name("kind")
writer.value(KindConstants.LISTING)
writer.name("data")
writeListing(writer, value)
writer.endObject()
}

private fun writeListing(writer: JsonWriter, value: Listing<Any>) {
writer.beginObject()
writer.name("after")
writer.value(value.nextName)
writer.name("children")
writer.beginArray()
for (child in value.children)
childrenDelegate.toJson(writer, child)
writer.endArray()
writer.endObject()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ class RedditModelAdapterFactoryTest : Spek({
val adapter = envelopedAdapter<Listing<NonEnvelopedChild>>(listingType(NonEnvelopedChild::class))
adapter.fromJson(nonEnvelopedListingJson).should.equal(nonEnvelopedListingValue)
}

it("should be able to serialize/deserialize a Listing without loss of meaning") {
val adapter = envelopedAdapter<Listing<Child>>(listingType(Child::class))
// Parse the JSON from our in-code string
val round1 = adapter.fromJson(listingJson)

// Serialize and immediately deserialize the object to see if it stayed the same
val round2 = adapter.fromJson(adapter.toJson(round1))

round1.should.equal(round2)
}
})

@Language("JSON") private val childJson = """{ "kind": "child", "data": { "a": "foo" } }"""
Expand Down

0 comments on commit 19944e6

Please sign in to comment.