首页 > 解决方案 > 在 ctype 结构中使用无符号整数时报告无效 JSON

问题描述

使用 Python 2.6,我创建了一个嵌套的 ctypes 结构,如下所示:

class TestStruct1(BigEndianStructure):
_pack_ = 1
_fields_ = [
    ("temp1", c_uint32),
    ("temp2", c_uint32)
]

class TestStruct2(BigEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("temp1", c_uint32),
        ("temp2", c_uint32)
    ]

class TestStruct3(BigEndianStructure):
    _pack_ = 1
    _fields_ = [
        ("TestStruct1", TestStruct1),
        ("TestStruct2", TestStruct2)
    ]

然后我将数据推送到这个结构中:

elements = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF]
data = bytes(elements)

foo = TestStruct3()
memmove(addressof(foo), data, sizeof(foo))

我可以一一打印出这些值,从而得到预期的输出:

print(format(foo.TestStruct1.temp1, '08X'))
print(format(foo.TestStruct1.temp2, '08X'))
print(format(foo.TestStruct2.temp1, '08X'))
print(format(foo.TestStruct2.temp2, '08X'))

00112233 44556677 8899AABB CCDDEEFF

但想利用 json.dumps 以 JSON 格式转储结构。这在 Python 3 中运行良好,如下所示:

https://onlinegdb.com/rJvQ0fFvS

但在 Python 2.6 中为无符号类型返回以下错误:

encoder.py”,第 184 行,默认提高 TypeError(repr(o) + “ is not JSON serializable”) TypeError: 1122867L is not JSON serializable

我猜它被 1122867 L (0x00112233)中附加的“L”弄糊涂了

我是否必须在我的 JSONEncoder 类中添加某种无符号类型?

下面是我创建的一个例子,它显示了这个问题。运行脚本的主机上的字节序不同,因此输出不匹配,但它至少显示了操作中的错误:

https://rextester.com/UAWZK83752

任何帮助将不胜感激。

谢谢!

标签: pythonctypes

解决方案


希望这对其他人有帮助。再调试一下,我最终找到了错误对应的文件(encoder.py),发现我在没有处理实例的情况下调用了 JSONEncoder.default。在这种情况下,我假设 c_uint32 是 int 的一个实例:

class ReportEncoder(JSONEncoder):

    def default(self, obj):

        if isinstance(obj, (Array, list)):
            return [self.default(e) for e in obj]

        if isinstance(obj, _Pointer):
            return self.default(obj.contents) if obj else None

        if isinstance(obj, _SimpleCData):
            return self.default(obj.value)

        if isinstance(obj, (bool, int, float, str)):
            return obj

        if obj is None:
            return obj

        if isinstance(obj, (Structure, Union)):
            result = {}
            anonymous = getattr(obj, '_anonymous_', [])

            for key, _ in getattr(obj, '_fields_', []):
                value = getattr(obj, key)

                # private fields don't encode
                if key.startswith('_'):
                    continue

                if key in anonymous:
                    result.update(self.default(value))
                else:
                    result[key] = self.default(value)

            return result

        return JSONEncoder.default(self, obj)

但显然 c_uint32 在 long 的实例中,而不是 int。我在实例列表中添加了 long :

if isinstance(obj, (bool, int, float, long, str)):
    return obj

一切都从那里开始。完整的 JSONEncoder 类可以在下面找到:

class ReportEncoder(JSONEncoder):

    def default(self, obj):

        if isinstance(obj, (Array, list)):
            return [self.default(e) for e in obj]

        if isinstance(obj, _Pointer):
            return self.default(obj.contents) if obj else None

        if isinstance(obj, _SimpleCData):
            return self.default(obj.value)

        if isinstance(obj, (bool, int, float, long, str)):
            return obj

        if obj is None:
            return obj

        if isinstance(obj, (Structure, Union)):
            result = {}
            anonymous = getattr(obj, '_anonymous_', [])

            for key, _ in getattr(obj, '_fields_', []):
                value = getattr(obj, key)

                # private fields don't encode
                if key.startswith('_'):
                    continue

                if key in anonymous:
                    result.update(self.default(value))
                else:
                    result[key] = self.default(value)

            return result

        return JSONEncoder.default(self, obj)

推荐阅读