-
Notifications
You must be signed in to change notification settings - Fork 667
feat: adding JSON elements page #3121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: doc-restructuring-master
Are you sure you want to change the base?
Conversation
| Similarly, [`JsonPrimitive`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-primitive/) has extension properties for parsing the value as Kotlin primitive types, such as | ||
| [`int`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/int.html), [`intOrNull`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/int-or-null.html), [`long`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/long.html), [`longOrNull`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/long-or-null.html), and so on. | ||
|
|
||
| Here's an example of how you can use these extension properties when processing JSON data: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original text was "This is how you can use them for processing JSON whose structure you know:". The part "whose structure you know" is important, because we are using extensions directly and they will throw an exception if actual data shape is different.
Maybe we should also add a small example on how to parse Json with unknown structure (when(element) { is JsonObject -> ... is JsonPrimitive -> ... } and so on.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point and idea! 👍
I modified it like:
Here's an example of how you can use these extension properties when processing JSON data with a known structure:
for the unknown part - would something like this work? 🤔
If you don't know the JSON structure in advance, you can check the element type and handle each
JsonElementsubtype explicitly.
For example, you can use a helper function with awhenexpression:fun checkElement(element: JsonElement): String = when (element) { is JsonObject -> "JsonObject with keys: ${element.keys}" is JsonArray -> "JsonArray with ${element.size} elements" is JsonPrimitive -> "JsonPrimitive with content: ${element.content}" }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but lets change checkElement name to processElement
sandwwraith
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see unresolved comments
| [`buildJsonArray()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/build-json-array.html) and [`buildJsonObject()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/build-json-object.html) builder functions respectively. | ||
| These provide a DSL to define the JSON structure, similar to [Kotlin's standard library collection builders](constructing-collections.md#create-with-collection-builder-functions), but with JSON-specific overloads and inner builder functions. | ||
|
|
||
| > You can also directly create a `JsonArray` from a `List` with [`JsonArray()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-array/-json-array.html) or a `JsonObject` from a `Map` with [`JsonObject()`](https://kotlinlang.org/api/kotlinx.serialization/kotlinx-serialization-json/kotlinx.serialization.json/-json-object/-json-object.html). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment to the previous conversation
sandwwraith
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job!
This is the sixth part of the Kotlin Serialization rewrite task.
This PR adds the JSON elements page to the restructured serialization documentation task.