From 11287d721c4505a63f84995bec0b1cf058b45dfd Mon Sep 17 00:00:00 2001 From: Bhavya Srivastava Date: Fri, 29 May 2020 17:38:21 +0530 Subject: [PATCH] Solves issue #45 Add jsonStringFixer.py, which has a function to add quotes around any required text in a json string. Used this in jsonld.py to handle invalid jsonld string. --- extruct/jsonStringFixer.py | 101 +++++++++++++++++++++++++++++++++++++ extruct/jsonld.py | 2 + 2 files changed, 103 insertions(+) create mode 100644 extruct/jsonStringFixer.py diff --git a/extruct/jsonStringFixer.py b/extruct/jsonStringFixer.py new file mode 100644 index 00000000..a9a7624c --- /dev/null +++ b/extruct/jsonStringFixer.py @@ -0,0 +1,101 @@ +def addMissingQuotesInJsonString(s): + + s = s.split("\n") + finalstring='' + for i in range(len(s)): + s1 = s[i] + s2 = '' + startflag=0 + j=0 + while j=48 and ord(s1[j])<=57) or (ord(s1[j])>=65 and ord(s1[j])<=90) or (ord(s1[j])>=97 and ord(s1[j])<=122): + s2=s2+'"'+s1[j] + j=j+1 + startflag=1 + break + elif s1[j]=='"': + s2=s2+s1[j] + j=j+1 + startflag=1 + break + else: + s2=s2+s1[j] + j=j+1 + + + elif startflag==1: + while True: + if j==len(s1): + s2=s2+'"' + startflag=0 + break + if s1[j]=="]" or s1[j]==";" or s1[j]=="," or (s1[j]==":" and s1[j+1]!="/") or s1[j]=="}" : + s2=s2+'"'+s1[j] + j=j+1 + startflag=0 + break + elif s1[j]=='"': + s2=s2+s1[j] + j=j+1 + startflag=0 + break + else: + s2=s2+s1[j] + j=j+1 + if startflag==1 and j==len(s1): + s2=s2+'"' + startflag=0 + + finalstring = finalstring + s2 +'\n' + + s = finalstring.split("\n") + finalstring='' + for i in range(len(s)): + s1 = s[i] + s2 = '' + colonflag=0 + j=0 + while j