def dict_to_JsonObject(the_dict, name=None, ctx=None):
'''
Create a JsonObject from a dictionary.
The context parameter is used for recursive calls,
no need to pass it from outside.
:param the_dict: dictionary to base the `JsonObject` on
:param ctx: context for the parser (default: None)
:rtype: :class:`~katnip.legos.json.JsonObject`
:return: JSON object that represents the dictionary
'''
if type(the_dict) != dict:
raise ValueError('expecting dictionary as first argument')
if ctx is None:
ctx = _JsonStringContext()
members = {}
for (k, v) in the_dict.items():
if v is None:
val = JsonNull(name=ctx.uname(k), fuzzable=False)
elif isinstance(v, types.BooleanType):
val = JsonBoolean(name=ctx.uname(k), value=v, fuzzable=True)
elif isinstance(v, types.StringTypes):
val = JsonString(name=ctx.uname(k), value=v, fuzzable=True)
elif isinstance(v, types.ListType):
val = list_to_JsonArray(v, k, ctx)
elif isinstance(v, types.DictionaryType):
val = dict_to_JsonObject(v, k, ctx)
elif isinstance(v, types.IntType):
val = SInt32(v, encoder=ENC_INT_DEC, name=ctx.uname(k))
else:
raise ValueError('type not supported: %s' % type(v))
members[k] = val
if name is None:
name = 'obj'
return JsonObject(name=ctx.uname(name, False), member_dict=members, fuzz_keys=False)
评论列表
文章目录