Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def toJson(obj):
- string = False
- escape = False
- output = str(obj)
- i = 0
- add = 0
- while True:
- if i == len(str(obj)):
- break
- if str(obj)[i] == '{':
- escape = False
- output = output[0:i+add] + '{' + str(obj)[i+1:]
- if str(obj)[i] == '}':
- escape = False
- output = output[0:i+add] + '}' + str(obj)[i+1:]
- if str(obj)[i] == '[':
- escape = False
- output = output[0:i+add] + '[' + str(obj)[i+1:]
- if str(obj)[i] == ']':
- escape = False
- output = output[0:i+add] + ']' + str(obj)[i+1:]
- if str(obj)[i] == '"' and string == True:
- escape = False
- output = output[0:i+add] + '\\"' + str(obj)[i+1:]
- add += 1
- if str(obj)[i] == ':':
- escape = False
- output = output[0:i+add] + ':' + str(obj)[i+1:]
- if str(obj)[i] == ',':
- escape = False
- output = output[0:i+add] + ',' + str(obj)[i+1:]
- if str(obj)[i] == '\\' and escape == True:
- escape = False
- output = output[0:i+add] + '\\' + str(obj)[i+1:]
- if str(obj)[i] == '\\' and escape == False:
- escape = True
- output = output[0:i+add] + '\\' + str(obj)[i+1:]
- if output[i+add-3:i+add+1] == 'True' and string == False:
- escape = False
- output = output[0:i+add-3] + 'true' + str(obj)[i+1:]
- if output[i+add-4:i+add+1] == 'False' and string == False:
- escape = False
- output = output[0:i+add-4] + 'false' + str(obj)[i+1:]
- if output[i+add-3:i+add+1] == 'None' and string == False:
- escape = False
- output = output[0:i+add-3] + 'null' + str(obj)[i+1:]
- if str(obj)[i] == '\'' and string == False:
- escape = False
- output = output[0:i+add] + '"' + str(obj)[i+1:]
- string = True
- i += 1
- continue
- if str(obj)[i] == '\'' and string == True and escape == False:
- escape = False
- string = False
- output = output[0:i+add] + '"' + str(obj)[i+1:]
- if str(obj)[i] == '\'' and string == True and escape == True:
- escape = False
- add -= 1
- output = output[0:i+add] + '\'' + str(obj)[i+1:]
- i += 1
- return output
Advertisement
Add Comment
Please, Sign In to add comment