Skip to content

Commit 207d524

Browse files
committed
fix: true, false and null values in JSON
1 parent d6545d3 commit 207d524

File tree

3 files changed

+108
-9
lines changed

3 files changed

+108
-9
lines changed

example.py

Lines changed: 102 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,112 @@
33

44
parser = JSONParser()
55

6-
incomplete_json = '{"name": "John", "age": 18, "family":'
6+
incomplete_json = """
7+
8+
{"web-app":
9+
{"servlet": [
10+
{
11+
"servlet-name3": "xyz",
12+
"servlet-name4": null,
13+
14+
"servlet-name2": true,
15+
"servlet-name1": false,
16+
"servlet-class": "org.cofax.cds.CDSServlet",
17+
"init-param": {
18+
19+
20+
"configGlossary:installationAt": "Philadelphia, PA",
21+
22+
"configGlossary:adminEmail": "[email protected]",
23+
24+
"configGlossary:poweredBy": "Cofax",
25+
"configGlossary:poweredByIcon": "/images/cofax.gif",
26+
"configGlossary:staticPath": "/content/static",
27+
"templateProcessorClass": "org.cofax.WysiwygTemplate",
28+
"templateLoaderClass": "org.cofax.FilesTemplateLoader",
29+
"templatePath": "templates",
30+
"templateOverridePath": "",
31+
"defaultListTemplate": "listTemplate.htm",
32+
"defaultFileTemplate": "articleTemplate.htm",
33+
"useJSP": false,
34+
"jspListTemplate": "listTemplate.jsp",
35+
"jspFileTemplate": "articleTemplate.jsp",
36+
"cachePackageTagsTrack": 200,
37+
"cachePackageTagsStore": 200,
38+
"cachePackageTagsRefresh": 60,
39+
"cacheTemplatesTrack": 100,
40+
"cacheTemplatesStore": 50,
41+
"cacheTemplatesRefresh": 15,
42+
"cachePagesTrack": 200,
43+
"cachePagesStore": 100,
44+
"cachePagesRefresh": 10,
45+
"cachePagesDirtyRead": 10,
46+
"searchEngineListTemplate": "forSearchEnginesList.htm",
47+
"searchEngineFileTemplate": "forSearchEngines.htm",
48+
"searchEngineRobotsDb": "WEB-INF/robots.db",
49+
"useDataStore": true,
50+
"dataStoreClass": "org.cofax.SqlDataStore",
51+
"redirectionClass": "org.cofax.SqlRedirection",
52+
"dataStoreName": "cofax",
53+
"dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
54+
"dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
55+
"dataStoreUser": "sa",
56+
"dataStorePassword": "dataStoreTestQuery",
57+
"dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
58+
"dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
59+
"dataStoreInitConns": 10,
60+
"dataStoreMaxConns": 100,
61+
"dataStoreConnUsageLimit": 100,
62+
"dataStoreLogLevel": "debug",
63+
"maxUrlLength": 500}},
64+
{
65+
"servlet-name": "cofaxEmail",
66+
"servlet-class": "org.cofax.cds.EmailServlet",
67+
"init-param": {
68+
"mailHost": "mail1",
69+
"mailHostOverride": "mail2"}},
70+
{
71+
"servlet-name": "cofaxAdmin",
72+
"servlet-class": "org.cofax.cds.AdminServlet"},
73+
74+
{
75+
"servlet-name": "fileServlet",
76+
"servlet-class": "org.cofax.cds.FileServlet"},
77+
{
78+
"servlet-name": "cofaxTools",
79+
"servlet-class": "org.cofax.cms.CofaxToolsServlet",
80+
"init-param": {
81+
"templatePath": "toolstemplates/",
82+
"log": 1,
83+
"logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
84+
"logMaxSize": "",
85+
"dataLog": 1,
86+
"dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
87+
"dataLogMaxSize": "",
88+
"removePageCache": "/content/admin/remove?cache=pages&id=",
89+
"removeTemplateCache": "/content/admin/remove?cache=templates&id=",
90+
"fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
91+
"lookInContext": 1,
92+
"adminGroupID": 4,
93+
"betaServer": true}}],
94+
"servlet-mapping": {
95+
"cofaxCDS": "/",
96+
"cofaxEmail": "/cofaxutil/aemail/*",
97+
"cofaxAdmin": "/admin/*",
98+
"fileServlet": "/static/*",
99+
"cofaxTools": "/tools/*"},
100+
101+
"taglib": {
102+
"taglib-uri": "cofax.tld",
103+
"taglib-location": "/WEB-INF/tlds/cofax.tld"}}}
104+
"""
105+
7106

8107
json = ""
9108

10-
for char in incomplete_json:
109+
for char in incomplete_json.strip():
11110
json += char
12111
print(f'\nIncomplete or streaming json:\n{json}')
13112
print(f'Final and usable JSON without crashing:\n{parser.parse(json)}')
14113
sys.stdout.flush()
15-
time.sleep(0.3)
114+
time.sleep(0.01)

partialjson/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"""
2-
torrentp.
2+
Partial Json.
33
4-
A great wrapped library for downloading from torrent.
4+
Parsing ChatGPT JSON stream response — Partial and incomplete JSON parser python library for OpenAI
55
"""
66
from .json_parser import JSONParser
77

8-
__version__ = "0.0.5"
8+
__version__ = "0.0.6"
99
__author__ = 'Nima Akbarzadeh'
1010
__author_email__ = "[email protected]"
1111
__license__ = "MIT"

partialjson/json_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,17 @@ def parse_number(self, s, e):
128128
return num, s
129129

130130
def parse_true(self, s, e):
131-
if s.startswith('true'):
131+
if s.startswith('t') or s.startswith('T'):
132132
return True, s[4:]
133133
raise e
134134

135135
def parse_false(self, s, e):
136-
if s.startswith('false'):
136+
if s.startswith('f') or s.startswith('F'):
137137
return False, s[5:]
138138
raise e
139139

140140
def parse_null(self, s, e):
141-
if s.startswith('null'):
141+
if s.startswith('n'):
142142
return None, s[4:]
143143
raise e
144144

0 commit comments

Comments
 (0)