Skip to content

Commit 6445cc6

Browse files
author
Oleg
committed
Add doc for the public API classes and methods
1 parent b4edbb3 commit 6445cc6

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

src/commonMain/kotlin/com/github/optimumcode/json/pointer/JsonPointer.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ package com.github.optimumcode.json.pointer
33
import kotlin.jvm.JvmField
44
import kotlin.jvm.JvmStatic
55

6+
/**
7+
* Function to create [JsonPointer].
8+
* It is a more convenient way to create a JSON pointer rather than using [JsonPointer.compile] function
9+
*/
610
public fun JsonPointer(path: String): JsonPointer =
711
JsonPointer.compile(path)
812

13+
/**
14+
* Implementation of a JSON pointer described in the specification
15+
* [RFC6901](https://datatracker.ietf.org/doc/html/rfc6901).
16+
*/
917
public sealed class JsonPointer(
1018
private val fullPath: String,
1119
private val pathOffset: Int,
@@ -40,11 +48,22 @@ public sealed class JsonPointer(
4048
internal const val SEPARATOR: Char = '/'
4149
internal const val QUOTATION: Char = '~'
4250

51+
/**
52+
* An empty [JsonPointer]. The empty JSON pointer corresponds to the current JSON element.s
53+
*/
4354
@JvmField
4455
public val ROOT: JsonPointer = EmptyPointer
4556

4657
private const val DEFAULT_BUFFER_CAPACITY = 32
4758

59+
/**
60+
* Returns instance of the [JsonPointer] class.
61+
* If the [expr] is an empty string the [JsonPointer.ROOT] will be returned.
62+
*
63+
* If the [expr] is not an empty string it must start from the `/` character.
64+
*
65+
* @throws IllegalArgumentException the [expr] does not start from `/`
66+
*/
4867
@JvmStatic
4968
public fun compile(expr: String): JsonPointer {
5069
return if (expr.isEmpty()) {

src/commonMain/kotlin/com/github/optimumcode/json/pointer/extensions.kt

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ import kotlinx.serialization.json.JsonObject
99
import kotlinx.serialization.json.JsonPrimitive
1010
import kotlin.jvm.JvmName
1111

12+
/**
13+
* Creates a new [JsonPointer] that points to an [index] in the array.
14+
*
15+
* Example:
16+
* ```kotlin
17+
* val pointer = JsonPointer("/test")
18+
* val index = pointer[0] // "/test/0"
19+
* ```
20+
*/
1221
public operator fun JsonPointer.get(index: Int): JsonPointer =
1322
JsonPointer(
1423
buildString {
@@ -21,6 +30,15 @@ public operator fun JsonPointer.get(index: Int): JsonPointer =
2130
},
2231
)
2332

33+
/**
34+
* Creates a new [JsonPointer] that points to a [property] passed as a parameter.
35+
*
36+
* Example:
37+
*
38+
* ```kotlin
39+
* val pointer = JsonPointer.ROOT / "prop1" / "prop2" // "/prop1/prop2"
40+
* ```
41+
*/
2442
public operator fun JsonPointer.div(property: String): JsonPointer =
2543
JsonPointer(
2644
buildString {
@@ -33,6 +51,23 @@ public operator fun JsonPointer.div(property: String): JsonPointer =
3351
},
3452
)
3553

54+
/**
55+
* Appends [otherPointer] to the current [JsonPointer].
56+
* If current or [otherPointer] JSON pointer is an empty JSON pointer. The first not-empty pointer will be returned.
57+
* (or an empty pointer if both pointers are empty).
58+
*
59+
* If both are not-empty pointers the resulting JSON pointer will start from the current one
60+
* and [otherPointer] appended at the end.
61+
*
62+
* Example:
63+
* ```kotlin
64+
* val pointer = JsonPointer.ROOT + JsonPointer.ROOT // ""
65+
*
66+
* val pointer = JsonPointer.ROOT + JsonPointer("/test") // "/test"
67+
*
68+
* val pointer = JsonPointer("/prop") + JsonPointer("/test") // "/prop/test"
69+
* ```
70+
*/
3671
public operator fun JsonPointer.plus(otherPointer: JsonPointer): JsonPointer {
3772
if (this is EmptyPointer) {
3873
return otherPointer
@@ -53,16 +88,39 @@ public operator fun JsonPointer.plus(otherPointer: JsonPointer): JsonPointer {
5388
)
5489
}
5590

91+
/**
92+
* Returns a [JsonPointer] that is a relative pointer from current pointer to [other] pointer.
93+
* If current pointer is an empty pointer the [other] pointer will be returned.
94+
*
95+
* If the [other] pointer is not starts from the current pointer the [other] pointer will be returned.
96+
*
97+
* Example:
98+
* ```kotlin
99+
* val pointer = JsonPointer("/test").relative(JsonPointer("/test/0/data") // "/0/data"
100+
* ```
101+
*
102+
* @throws IllegalArgumentException when [other] is an empty pointer
103+
*/
56104
public fun JsonPointer.relative(other: JsonPointer): JsonPointer {
57105
if (this is EmptyPointer) {
58106
return other
59107
}
60108
require(other !is EmptyPointer) { "empty pointer is not relative to any" }
61109
val currentValue = this.toString()
62110
val otherValue = other.toString()
63-
return JsonPointer(otherValue.substringAfter(currentValue))
111+
val relative = otherValue.substringAfter(currentValue)
112+
return if (relative == otherValue) {
113+
other
114+
} else {
115+
JsonPointer(relative)
116+
}
64117
}
65118

119+
/**
120+
* Extracts [JsonElement] from the current JSON element that corresponds to the specified [JsonPointer].
121+
*
122+
* If [pointer] path does not exist in the current [JsonElement] the `null` will be returned.
123+
*/
66124
public tailrec fun JsonElement.at(pointer: JsonPointer): JsonElement? {
67125
return when (pointer) {
68126
is EmptyPointer -> this

src/commonMain/kotlin/com/github/optimumcode/json/schema/ErrorCollector.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public fun interface ErrorCollector {
1313
public fun onError(error: ValidationError)
1414

1515
public companion object {
16+
/**
17+
* Empty [ErrorCollector] can be used if you need only a simple `true`/`false` as a result of the validation
18+
*/
1619
@JvmField
1720
public val EMPTY: ErrorCollector = ErrorCollector { }
1821
}

src/commonMain/kotlin/com/github/optimumcode/json/schema/JsonSchema.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,30 @@ import kotlinx.serialization.json.Json
99
import kotlinx.serialization.json.JsonElement
1010
import kotlin.jvm.JvmStatic
1111

12+
/**
13+
* JSON schema implementation. It allows you to validate the [JsonElement] against this schema
14+
* and collect errors using [ErrorCollector]
15+
*/
1216
public class JsonSchema internal constructor(
1317
private val assertion: JsonSchemaAssertion,
1418
private val references: Map<RefId, JsonSchemaAssertion>,
1519
) {
20+
/**
21+
* Validates [value] against this [JsonSchema].
22+
* If the [value] is valid against the schema the function returns `true`.
23+
* Otherwise, it returns `false`.
24+
*
25+
* All reported errors will be reported to [ErrorCollector.onError]
26+
*/
1627
public fun validate(value: JsonElement, errorCollector: ErrorCollector): Boolean {
1728
val context = DefaultAssertionContext(JsonPointer.ROOT, references)
1829
return assertion.validate(value, context, errorCollector)
1930
}
2031

2132
public companion object {
33+
/**
34+
* Loads JSON schema from the [schema] definition
35+
*/
2236
@JvmStatic
2337
public fun fromDescription(schema: String): JsonSchema {
2438
val schemaElement: JsonElement = Json.parseToJsonElement(schema)

0 commit comments

Comments
 (0)