You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+12-6Lines changed: 12 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ Adds [JSON Pointer (RFC 6901)](https://tools.ietf.org/html/rfc6901) support to
6
6
and modifying original fork by [John Lombardo](https://github.com/johnnylambada/gson).
7
7
8
8
## Usage
9
-
This library provides a simple lookup implementation, that tries to always provide a valid Kotlin object for quick and easy dereferencing and direct usage of JSON pointers to read values from Gson's JsonParser when you don't want to keep making specific Kotlin data classes for one off use cases or specific error handling.
9
+
This library provides a simple lookup implementation, that tries to always provide a valid Kotlin object for quick and easy dereferencing and direct usage of JSON pointers to read values from Gson's JsonParser when you don't want to keep making specific Kotlin data classes for one off use cases or specific error handling. By using the provided _safeX_ extension properties on JsonElement, you can rest assured that you'll always get a good value back instead of an Exception at runtime.
10
10
11
11
### Parse JSON using Gson
12
12
Consider a JSON error response that you're receiving from your API like this:
@@ -36,16 +36,22 @@ Once you have a JsonElement you can get a JsonPointer with it as root, using the
36
36
//pointer is JsonPointer
37
37
```
38
38
39
-
Once you have a JsonPointer, you can directly access values anywhere in the whole JSON tree using pointer syntax:
39
+
Once you have a JsonPointer, you can directly access a JsonPointer anywhere in the whole JSON tree using pointer syntax:
40
40
```kotlin
41
-
val message = pointer.at("/email/0")
41
+
val message = pointer.at("/email/0").safeString
42
42
//message is the first error String, "Enter a valid email address"
43
43
```
44
44
45
-
**Note:** The `at()` function always tries to return a valid value. It will never return a null. In case of a JSON null, it will return an empty string, `""`
45
+
###Safe value retreival
46
+
The `at()` function always returns a valid JsonElement, even when there is no match. You can use the standard `isJsonPrimitive` or other similar methods to determine what you got back. You can more easily use the provided convenience get properties to get either the actual value at the node or a default value that you can check and flow against.
47
+
48
+
*`JsonElement.safeNumber` returns any `Number` found at the pointer, or a `0`
49
+
*`JsonElement.safeString` returns any `String` found at the pointer, or a `""`
50
+
*`JsonElement.safeBoolean` returns any `Boolean` found at the pointer, or a `false`
51
+
*`JsonElement.safeJsonObject` returns any `JsonObject` found at the pointer, or a `JsonObject()`, which works out to an empty JSON object: `{}`
52
+
*`JsonElement.safeJsonArray` returns any `JsonArray` found at the pointer, or a `JsonArray()`, which works out to an empty JSON Array, `[]`
53
+
46
54
47
-
### Access a JsonElement
48
-
**(Optional)** In case you _do_ need to get access to a JsonElement and not the actual value within it, you can use the `dereference()` method which will return a normal `JsonElement` for further evaluation.
49
55
50
56
## Installation
51
57
GsonPointer uses JitPack.io to distribute the library.
0 commit comments