2023-01-21 15:48:17 +08:00
|
|
|
import pika_cjson as cjson
|
|
|
|
|
|
|
|
|
2023-01-21 17:17:28 +08:00
|
|
|
def _cjson_encode(cjson: cjson.cJSON):
|
2023-01-21 15:48:17 +08:00
|
|
|
if cjson == None:
|
|
|
|
return None
|
|
|
|
elif cjson.isInvalid():
|
|
|
|
return None
|
|
|
|
elif cjson.isFalse():
|
|
|
|
return False
|
|
|
|
elif cjson.isTrue():
|
|
|
|
return True
|
|
|
|
elif cjson.isNull():
|
|
|
|
return None
|
|
|
|
elif cjson.isNumber():
|
|
|
|
return cjson.getValueDouble()
|
|
|
|
elif cjson.isString():
|
|
|
|
return cjson.getValueString()
|
|
|
|
elif cjson.isArray():
|
|
|
|
res = []
|
|
|
|
for i in range(cjson.getArraySize()):
|
|
|
|
res.append(_cjson_encode(cjson.getArrayItem(i)))
|
|
|
|
return res
|
|
|
|
elif cjson.isObject():
|
|
|
|
res = {}
|
|
|
|
child = cjson.getChild()
|
|
|
|
for i in range(cjson.getArraySize()):
|
|
|
|
key = child.getString()
|
|
|
|
res[key] = _cjson_encode(child)
|
|
|
|
child = child.getNext()
|
|
|
|
return res
|
|
|
|
elif cjson.isRaw():
|
|
|
|
return cjson.getString()
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def loads(json: str) -> dict:
|
|
|
|
cj = cjson.Parse(json)
|
|
|
|
return _cjson_encode(cj)
|
2023-01-21 17:17:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
def _cjson_decode(d: dict):
|
|
|
|
if d == None:
|
|
|
|
return cjson.Null()
|
2023-01-23 23:53:21 +08:00
|
|
|
elif str(type(d)) == "<class 'int'>":
|
2023-01-21 17:17:28 +08:00
|
|
|
return cjson.Number(d)
|
2023-01-23 23:53:21 +08:00
|
|
|
elif str(type(d)) == "<class 'float'>":
|
2023-01-21 17:17:28 +08:00
|
|
|
return cjson.Number(d)
|
2023-01-23 23:53:21 +08:00
|
|
|
elif str(type(d)) == "<class 'bool'>":
|
2023-01-21 17:17:28 +08:00
|
|
|
if d:
|
|
|
|
return cjson.True_()
|
|
|
|
else:
|
|
|
|
return cjson.False_()
|
2023-01-23 23:53:21 +08:00
|
|
|
elif str(type(d)) == "<class 'str'>":
|
2023-01-21 17:17:28 +08:00
|
|
|
return cjson.String(d)
|
|
|
|
elif str(type(d)) == "<class 'list'>":
|
|
|
|
res = cjson.Array()
|
|
|
|
for i in d:
|
|
|
|
res.addItemToArray(_cjson_decode(i))
|
|
|
|
return res
|
|
|
|
elif str(type(d)) == "<class 'dict'>":
|
|
|
|
res = cjson.Object()
|
|
|
|
for k, v in d.items():
|
|
|
|
res.addItemToObject(k, _cjson_decode(v))
|
|
|
|
return res
|
|
|
|
else:
|
|
|
|
return cjson.Null()
|
|
|
|
|
|
|
|
|
|
|
|
def dumps(d: dict) -> str:
|
|
|
|
return _cjson_decode(d).print()
|