partialjson 0.0.7__tar.gz → 0.0.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {partialjson-0.0.7 → partialjson-0.0.8}/PKG-INFO +1 -1
- {partialjson-0.0.7 → partialjson-0.0.8}/partialjson/__init__.py +1 -1
- {partialjson-0.0.7 → partialjson-0.0.8}/partialjson/json_parser.py +11 -10
- {partialjson-0.0.7 → partialjson-0.0.8}/partialjson.egg-info/PKG-INFO +1 -1
- {partialjson-0.0.7 → partialjson-0.0.8}/LICENSE +0 -0
- {partialjson-0.0.7 → partialjson-0.0.8}/README.md +0 -0
- {partialjson-0.0.7 → partialjson-0.0.8}/partialjson.egg-info/SOURCES.txt +0 -0
- {partialjson-0.0.7 → partialjson-0.0.8}/partialjson.egg-info/dependency_links.txt +0 -0
- {partialjson-0.0.7 → partialjson-0.0.8}/partialjson.egg-info/top_level.txt +0 -0
- {partialjson-0.0.7 → partialjson-0.0.8}/setup.cfg +0 -0
- {partialjson-0.0.7 → partialjson-0.0.8}/setup.py +0 -0
|
@@ -5,7 +5,7 @@ Parsing ChatGPT JSON stream response — Partial and incomplete JSON parser pyth
|
|
|
5
5
|
"""
|
|
6
6
|
from .json_parser import JSONParser
|
|
7
7
|
|
|
8
|
-
__version__ = "0.0.
|
|
8
|
+
__version__ = "0.0.8"
|
|
9
9
|
__author__ = 'Nima Akbarzadeh'
|
|
10
10
|
__author_email__ = "iw4p@protonmail.com"
|
|
11
11
|
__license__ = "MIT"
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import json
|
|
2
2
|
|
|
3
3
|
class JSONParser:
|
|
4
|
-
def __init__(self):
|
|
4
|
+
def __init__(self, strict=True):
|
|
5
|
+
self.strict = strict
|
|
5
6
|
self.parsers = {
|
|
6
7
|
' ': self.parse_space,
|
|
7
8
|
'\r': self.parse_space,
|
|
@@ -14,7 +15,6 @@ class JSONParser:
|
|
|
14
15
|
'f': self.parse_false,
|
|
15
16
|
'n': self.parse_null
|
|
16
17
|
}
|
|
17
|
-
# Adding parsers for numbers
|
|
18
18
|
for c in '0123456789.-':
|
|
19
19
|
self.parsers[c] = self.parse_number
|
|
20
20
|
|
|
@@ -33,7 +33,7 @@ class JSONParser:
|
|
|
33
33
|
self.last_parse_reminding = reminding
|
|
34
34
|
if self.on_extra_token and reminding:
|
|
35
35
|
self.on_extra_token(s, data, reminding)
|
|
36
|
-
return
|
|
36
|
+
return data
|
|
37
37
|
else:
|
|
38
38
|
return json.loads("{}")
|
|
39
39
|
|
|
@@ -75,19 +75,16 @@ class JSONParser:
|
|
|
75
75
|
key, s = self.parse_any(s, e)
|
|
76
76
|
s = s.strip()
|
|
77
77
|
|
|
78
|
-
# Handle case where object ends after a key
|
|
79
78
|
if not s or s[0] == '}':
|
|
80
79
|
acc[key] = None
|
|
81
80
|
break
|
|
82
81
|
|
|
83
|
-
# Expecting a colon after the key
|
|
84
82
|
if s[0] != ':':
|
|
85
83
|
raise e # or handle this scenario as per your requirement
|
|
86
84
|
|
|
87
85
|
s = s[1:] # skip ':'
|
|
88
86
|
s = s.strip()
|
|
89
87
|
|
|
90
|
-
# Handle case where value is missing or incomplete
|
|
91
88
|
if not s or s[0] in ',}':
|
|
92
89
|
acc[key] = None
|
|
93
90
|
if s.startswith(','):
|
|
@@ -107,10 +104,15 @@ class JSONParser:
|
|
|
107
104
|
while end != -1 and s[end - 1] == '\\': # Handle escaped quotes
|
|
108
105
|
end = s.find('"', end + 1)
|
|
109
106
|
if end == -1:
|
|
110
|
-
#
|
|
111
|
-
|
|
107
|
+
# Incomplete string: handle it based on strict mode
|
|
108
|
+
if not self.strict:
|
|
109
|
+
return s[1:], ""
|
|
110
|
+
else:
|
|
111
|
+
return json.loads(f'"{s[1:]}"'), ""
|
|
112
112
|
str_val = s[:end + 1]
|
|
113
113
|
s = s[end + 1:]
|
|
114
|
+
if not self.strict:
|
|
115
|
+
return str_val[1:-1], s # Remove surrounding quotes for strict mode
|
|
114
116
|
return json.loads(str_val), s
|
|
115
117
|
|
|
116
118
|
def parse_number(self, s, e):
|
|
@@ -143,5 +145,4 @@ class JSONParser:
|
|
|
143
145
|
def parse_null(self, s, e):
|
|
144
146
|
if s.startswith('n'):
|
|
145
147
|
return None, s[4:]
|
|
146
|
-
raise e
|
|
147
|
-
|
|
148
|
+
raise e
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|