在 ctype 结构中使用无符号整数时报告无效 JSON

Invalid JSON reported when using unsigned integers in ctype structure

使用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

但 returns Python 2.6 中无符号类型的以下错误:

encoder.py", line 184, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: 1122867L is not JSON serializable

我猜它被 1122867L (0x00112233)

中附加的 'L' 弄糊涂了

我是否必须向我的 JSON 编码器 class 添加某种无符号类型?

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

https://rextester.com/UAWZK83752

如有任何帮助,我们将不胜感激。

谢谢!

希望这对其他人有帮助。经过更多调试后,我最终在与错误 (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 可以在下面找到:

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)