Skip to content

Commit

Permalink
📝 Add an example for single nested objects (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgreze authored Aug 21, 2020
1 parent 97ab53e commit d27c622
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,20 @@ Given you have JSON as such
"name": "Clementina DuBuque",
"age": 46,
"email": "[email protected]",
"phone": {
"name": "My Phone",
"model": "Pixel 3XL"
},
"friends": [
{
"id": 11,
"name": "Ervin Howell",
"age": 32,
"email": "[email protected]",
"phone": {
"name": "My iPhone",
"model": "iPhone X"
},
"friends": []
}
],
Expand All @@ -60,9 +68,11 @@ data class User(val id: Int,
val name: String,
val age: Int,
val email: String?,
val phone: Phone,
val friends: List<User>,
val dogs: List<Dog>?)

data class Phone(val name: String, val model: String)
data class Dog(val name: String, val breed: String, val male: Boolean)

fun userDeserializer(json: JSON) =
Expand All @@ -71,8 +81,15 @@ fun userDeserializer(json: JSON) =
apply(json at "name").
apply(json at "age").
apply(json maybeAt "email").
apply(json.at("phone", phoneDeserializer)), // phoneDeserializer is a lambda, use it directly
apply(json.list("friends", ::userDeserializer)). //userDeserializer is a function, use :: as a function reference
apply(json.maybeList("dogs", dogDeserializer)) //dogDeserializer is a lambda, use it directly
apply(json.maybeList("dogs", dogDeserializer))

val phoneDeserializer = { json: JSON ->
::Dog.create.
map(json at "name").
apply(json at "model")
}

val dogDeserializer = { json: JSON ->
::Dog.create.
Expand Down

0 comments on commit d27c622

Please sign in to comment.