json.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:isf 作者: w3h 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号