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
+56-3Lines changed: 56 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,9 +13,18 @@
13
13
[中文](README_CN.md)
14
14
15
15
## Introduction
16
-
This library helps to parse partial JSON (that is, incomplete JSON) in Kotlin. It is implemented in **pure Kotlin** so that can be used in KMP project.
16
+
This library helps to **parse and repair partial JSON** (that is, incomplete JSON) in Kotlin. Perfect for handling streaming JSON from LLMs like ChatGPT. It is implemented in **pure Kotlin** so that can be used in KMP project.
17
+
18
+
## Features
19
+
-**Parse** incomplete JSON strings into usable objects
20
+
-**Complete/Repair** partial JSON to valid JSON format
21
+
- Handle streaming JSON data in real-time
22
+
- Pure Kotlin implementation for KMP compatibility
val completedJson =PartialJsonParser.complete(partialJson)
43
+
println(completedJson) // {"key":"Hello, "}
44
+
45
+
// More examples
46
+
val incomplete ="{\"name\":\"John\", \"age\":"
47
+
val repaired =PartialJsonParser.complete(incomplete)
48
+
println(repaired) // {"name":"John"}
49
+
```
50
+
51
+
The `parse` method will throw `JsonParseException` if the JSON is invalid. The `complete` method repairs partial JSON into valid JSON format by removing incomplete elements.
52
+
53
+
(In some extreme cases, it might also throw `IndexOutOfBoundsException`, which should not occur although. If that did happen, feel free to open an issue with your sample.)
54
+
29
55
30
56
Actually, `parse` is just a combination of `tokenize` and `parseTokens`, you can use them separately if you want
To see more examples, please run `ParseTest.kt` and `CompleteTest.kt`.
66
119
67
120
## Origin Source
68
121
Interestingly, the code is converted from the TypeScript library [here](https://github.com/SimonTart/json-fragment-parser) by GitHub Copilot, I make it suitable for Kotlin style, modify some extreme cases, write some tests and publish it to Maven Central. Thanks for it.
0 commit comments