Skip to content

Commit 870d3b5

Browse files
update README for v.1.0.3
1 parent 759135c commit 870d3b5

File tree

2 files changed

+110
-8
lines changed

2 files changed

+110
-8
lines changed

README.md

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@
1313
[中文](README_CN.md)
1414

1515
## 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
23+
- Robust error handling for malformed JSON
1724

1825
## Usage
26+
27+
### Parse Partial JSON
1928
```kotlin
2029
import com.funnysaltyfish.partialjsonparser.PartialJsonParser
2130

@@ -25,7 +34,24 @@ println(map) // {key=Hello, }
2534
println(map?.get("key")) // Hello,
2635
```
2736

28-
The method will throw `JsonParseException` if the JSON is invalid. In some extreme cases, it might also throw `IndexOutOfBoundsException`.
37+
### Complete/Repair Partial JSON
38+
```kotlin
39+
import com.funnysaltyfish.partialjsonparser.PartialJsonParser
40+
41+
val partialJson = "{\"key\":\"Hello, "
42+
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+
2955

3056
Actually, `parse` is just a combination of `tokenize` and `parseTokens`, you can use them separately if you want
3157

@@ -44,6 +70,8 @@ implementation("io.github.funnysaltyfish:partial-json-parser:1.0.3")
4470
```
4571

4672
## Examples
73+
74+
### Parse Examples
4775
Below are some examples: (originJsonString -> parsed map)
4876
```
4977
{"ke -> {}
@@ -62,7 +90,32 @@ Below are some examples: (originJsonString -> parsed map)
6290
{"k":[{"k2":1, "k3":2 -> {k=[{k2=1.0, k3=2.0}]}
6391
```
6492

65-
To see more examples, please run `ParseTest.kt`.
93+
### Complete/Repair Examples
94+
Below are examples of JSON completion/repair: (partial JSON -> completed JSON)
95+
```
96+
{"key -> {}
97+
{"key": -> {}
98+
{"key":"te -> {"key":"te"}
99+
{"key":"text", "key2 -> {"key":"text"}
100+
{"key":"text", "key2":"t -> {"key":"text","key2":"t"}
101+
{"key":123, -> {"key":123}
102+
{"key":[1,2,3 -> {"key":[1,2,3]}
103+
{"key":[ -> {"key":[]}
104+
{"key":{"inner -> {"key":{}}
105+
{"key":{"inner":"val", -> {"key":{"inner":"val"}}
106+
[1,2,3 -> [1,2,3]
107+
["a","b -> ["a","b"]
108+
[{"key":"val -> [{"key":"val"}]
109+
[{"key":"val", -> [{"key":"val"}]
110+
{"a":{"b":{"c":"val -> {"a":{"b":{"c":"val"}}}
111+
{"a":{"b":[1,2,{"c":"val -> {"a":{"b":[1,2,{"c":"val"}]}}
112+
{"a":[{"b":{"c":"val -> {"a":[{"b":{"c":"val"}}]}
113+
{"outer":{"inner":{"key":"value", "key2 -> {"outer":{"inner":{"key":"value"}}}
114+
{"numbers":[1,2.5,3], "incomplete": -> {"numbers":[1,2.5,3]}
115+
{"a":1, "b": -> {"a":1}
116+
```
117+
118+
To see more examples, please run `ParseTest.kt` and `CompleteTest.kt`.
66119

67120
## Origin Source
68121
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.

README_CN.md

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212

1313
## 简介
1414

15-
这个库帮助在 Kotlin 中解析部分JSON(即不完整的JSON,比如由类似 ChatGPT 的大语言模型在流式返回中生成的)。它是用 **纯 Kotlin** 实现的,可在 KMP 项目中使用。
15+
这个库帮助在 Kotlin 中**解析和修复部分JSON**(即不完整的JSON,比如由类似 ChatGPT 的大语言模型在流式返回中生成的)。特别适合处理来自 LLM 的流式 JSON 数据。它是用 **纯 Kotlin** 实现的,可在 KMP 项目中使用。
16+
17+
## 特性
18+
- **解析**不完整的 JSON 字符串为可用对象
19+
- **补全/修复**部分 JSON 为有效的 JSON 格式
20+
- 实时处理流式 JSON 数据
21+
- 纯 Kotlin 实现,支持 KMP
22+
- 强健的错误处理机制
1623

1724
## 用法
1825

19-
使用起来就一行:
26+
### 解析部分 JSON
2027

2128
```kotlin
2229
import com.funnysaltyfish.partialjsonparser.PartialJsonParser
@@ -27,8 +34,24 @@ println(map) // {key=Hello, }
2734
println(map?.get("key")) // Hello,
2835
```
2936

30-
如果 JSON 无效,则该方法将抛出 `JsonParseException`。在某些极端情况下,它也可能抛出 `IndexOutOfBoundsException`
31-
(理论上说不会出现这种情况,如果有欢迎 issue 和 PR)。
37+
### 补全/修复部分 JSON
38+
39+
```kotlin
40+
import com.funnysaltyfish.partialjsonparser.PartialJsonParser
41+
42+
val partialJson = "{\"key\":\"Hello, "
43+
val completedJson = PartialJsonParser.complete(partialJson)
44+
println(completedJson) // {"key":"Hello, "}
45+
46+
// 更多例子
47+
val incomplete = "{\"name\":\"John\", \"age\":"
48+
val repaired = PartialJsonParser.complete(incomplete)
49+
println(repaired) // {"name":"John"}
50+
```
51+
52+
`parse` 方法在 JSON 无效时会抛出 `JsonParseException``complete` 方法通过移除不完整的元素将部分 JSON 修复为有效的 JSON 格式。
53+
54+
在某些极端情况下,它也可能抛出 `IndexOutOfBoundsException`(理论上说不会出现这种情况,如果有欢迎 issue 和 PR)。
3255

3356
实际上,`parse` 只是 `tokenize``parseTokens` 的组合,你也可以分开用:
3457

@@ -49,6 +72,7 @@ implementation("io.github.funnysaltyfish:partial-json-parser:1.0.3")
4972

5073
## 示例
5174

75+
### 解析示例
5276
以下是一些示例:(原始JSON字符串 -> 解析后的map)
5377
```
5478
{"ke -> {}
@@ -67,7 +91,32 @@ implementation("io.github.funnysaltyfish:partial-json-parser:1.0.3")
6791
{"k":[{"k2":1, "k3":2 -> {k=[{k2=1.0, k3=2.0}]}
6892
```
6993

70-
总的原则是,我们会尽可能多的解析出信息。你可以运行 ParseTest.kt 文件来查看更多情况
94+
### 补全/修复示例
95+
以下是 JSON 补全/修复的示例:(部分 JSON -> 补全后的 JSON)
96+
```
97+
{"key -> {}
98+
{"key": -> {}
99+
{"key":"te -> {"key":"te"}
100+
{"key":"text", "key2 -> {"key":"text"}
101+
{"key":"text", "key2":"t -> {"key":"text","key2":"t"}
102+
{"key":123, -> {"key":123}
103+
{"key":[1,2,3 -> {"key":[1,2,3]}
104+
{"key":[ -> {"key":[]}
105+
{"key":{"inner -> {"key":{}}
106+
{"key":{"inner":"val", -> {"key":{"inner":"val"}}
107+
[1,2,3 -> [1,2,3]
108+
["a","b -> ["a","b"]
109+
[{"key":"val -> [{"key":"val"}]
110+
[{"key":"val", -> [{"key":"val"}]
111+
{"a":{"b":{"c":"val -> {"a":{"b":{"c":"val"}}}
112+
{"a":{"b":[1,2,{"c":"val -> {"a":{"b":[1,2,{"c":"val"}]}}
113+
{"a":[{"b":{"c":"val -> {"a":[{"b":{"c":"val"}}]}
114+
{"outer":{"inner":{"key":"value", "key2 -> {"outer":{"inner":{"key":"value"}}}
115+
{"numbers":[1,2.5,3], "incomplete": -> {"numbers":[1,2.5,3]}
116+
{"a":1, "b": -> {"a":1}
117+
```
118+
119+
总的原则是,我们会尽可能多的解析出信息。你可以运行 ParseTest.kt 和 CompleteTest.kt 文件来查看更多情况
71120

72121
## 特别鸣谢
73122
这个库的代码是从 [这个 TypeScript 库](https://github.com/SimonTart/json-fragment-parser) 来的,用的 GitHub Copilot 一点点转换的, 我将它的代码略作调整以符合 Kotlin 风格、优化了一些极端情况、写了测试以及完成了发布。由衷的感谢一下!

0 commit comments

Comments
 (0)