Skip to content

Commit

Permalink
fix: true, false and null values in JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
iw4p committed Mar 27, 2024
1 parent d6545d3 commit 207d524
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 9 deletions.
105 changes: 102 additions & 3 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,112 @@

parser = JSONParser()

incomplete_json = '{"name": "John", "age": 18, "family":'
incomplete_json = """
{"web-app":
{"servlet": [
{
"servlet-name3": "xyz",
"servlet-name4": null,
"servlet-name2": true,
"servlet-name1": false,
"servlet-class": "org.cofax.cds.CDSServlet",
"init-param": {
"configGlossary:installationAt": "Philadelphia, PA",
"configGlossary:adminEmail": "[email protected]",
"configGlossary:poweredBy": "Cofax",
"configGlossary:poweredByIcon": "/images/cofax.gif",
"configGlossary:staticPath": "/content/static",
"templateProcessorClass": "org.cofax.WysiwygTemplate",
"templateLoaderClass": "org.cofax.FilesTemplateLoader",
"templatePath": "templates",
"templateOverridePath": "",
"defaultListTemplate": "listTemplate.htm",
"defaultFileTemplate": "articleTemplate.htm",
"useJSP": false,
"jspListTemplate": "listTemplate.jsp",
"jspFileTemplate": "articleTemplate.jsp",
"cachePackageTagsTrack": 200,
"cachePackageTagsStore": 200,
"cachePackageTagsRefresh": 60,
"cacheTemplatesTrack": 100,
"cacheTemplatesStore": 50,
"cacheTemplatesRefresh": 15,
"cachePagesTrack": 200,
"cachePagesStore": 100,
"cachePagesRefresh": 10,
"cachePagesDirtyRead": 10,
"searchEngineListTemplate": "forSearchEnginesList.htm",
"searchEngineFileTemplate": "forSearchEngines.htm",
"searchEngineRobotsDb": "WEB-INF/robots.db",
"useDataStore": true,
"dataStoreClass": "org.cofax.SqlDataStore",
"redirectionClass": "org.cofax.SqlRedirection",
"dataStoreName": "cofax",
"dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
"dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
"dataStoreUser": "sa",
"dataStorePassword": "dataStoreTestQuery",
"dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
"dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
"dataStoreInitConns": 10,
"dataStoreMaxConns": 100,
"dataStoreConnUsageLimit": 100,
"dataStoreLogLevel": "debug",
"maxUrlLength": 500}},
{
"servlet-name": "cofaxEmail",
"servlet-class": "org.cofax.cds.EmailServlet",
"init-param": {
"mailHost": "mail1",
"mailHostOverride": "mail2"}},
{
"servlet-name": "cofaxAdmin",
"servlet-class": "org.cofax.cds.AdminServlet"},
{
"servlet-name": "fileServlet",
"servlet-class": "org.cofax.cds.FileServlet"},
{
"servlet-name": "cofaxTools",
"servlet-class": "org.cofax.cms.CofaxToolsServlet",
"init-param": {
"templatePath": "toolstemplates/",
"log": 1,
"logLocation": "/usr/local/tomcat/logs/CofaxTools.log",
"logMaxSize": "",
"dataLog": 1,
"dataLogLocation": "/usr/local/tomcat/logs/dataLog.log",
"dataLogMaxSize": "",
"removePageCache": "/content/admin/remove?cache=pages&id=",
"removeTemplateCache": "/content/admin/remove?cache=templates&id=",
"fileTransferFolder": "/usr/local/tomcat/webapps/content/fileTransferFolder",
"lookInContext": 1,
"adminGroupID": 4,
"betaServer": true}}],
"servlet-mapping": {
"cofaxCDS": "/",
"cofaxEmail": "/cofaxutil/aemail/*",
"cofaxAdmin": "/admin/*",
"fileServlet": "/static/*",
"cofaxTools": "/tools/*"},
"taglib": {
"taglib-uri": "cofax.tld",
"taglib-location": "/WEB-INF/tlds/cofax.tld"}}}
"""


json = ""

for char in incomplete_json:
for char in incomplete_json.strip():
json += char
print(f'\nIncomplete or streaming json:\n{json}')
print(f'Final and usable JSON without crashing:\n{parser.parse(json)}')
sys.stdout.flush()
time.sleep(0.3)
time.sleep(0.01)
6 changes: 3 additions & 3 deletions partialjson/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
torrentp.
Partial Json.
A great wrapped library for downloading from torrent.
Parsing ChatGPT JSON stream response — Partial and incomplete JSON parser python library for OpenAI
"""
from .json_parser import JSONParser

__version__ = "0.0.5"
__version__ = "0.0.6"
__author__ = 'Nima Akbarzadeh'
__author_email__ = "[email protected]"
__license__ = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions partialjson/json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,17 @@ def parse_number(self, s, e):
return num, s

def parse_true(self, s, e):
if s.startswith('true'):
if s.startswith('t') or s.startswith('T'):
return True, s[4:]
raise e

def parse_false(self, s, e):
if s.startswith('false'):
if s.startswith('f') or s.startswith('F'):
return False, s[5:]
raise e

def parse_null(self, s, e):
if s.startswith('null'):
if s.startswith('n'):
return None, s[4:]
raise e

0 comments on commit 207d524

Please sign in to comment.