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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: partialjson
3
- Version: 0.0.7
3
+ Version: 0.0.8
4
4
  Summary: Parse incomplete or partial json
5
5
  Home-page: https://github.com/iw4p/partialjson
6
6
  Author: Nima Akbarzadeh
@@ -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.7"
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 json.loads(json.dumps(data))
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
- # Return the incomplete string without the opening quote
111
- return s[1:], ""
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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: partialjson
3
- Version: 0.0.7
3
+ Version: 0.0.8
4
4
  Summary: Parse incomplete or partial json
5
5
  Home-page: https://github.com/iw4p/partialjson
6
6
  Author: Nima Akbarzadeh
File without changes
File without changes
File without changes
File without changes