Skip to content

Commit b074b09

Browse files
committed
Added json dumps/loads support.
1 parent 16ea409 commit b074b09

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

dissect/cstruct/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
cstruct,
33
ctypes,
44
dumpstruct,
5+
dumpstructjson,
6+
loadstructjson,
57
hexdump,
68
Instance,
79
PointerInstance,
@@ -19,6 +21,8 @@
1921
"cstruct",
2022
"ctypes",
2123
"dumpstruct",
24+
"dumpstructjson",
25+
"loadstructjson",
2226
"hexdump",
2327
"Instance",
2428
"PointerInstance",

dissect/cstruct/cstruct.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import ctypes as _ctypes
3333
from io import BytesIO
3434
from collections import OrderedDict
35+
import json
3536

3637
try:
3738
from builtins import bytes as newbytes
@@ -1892,3 +1893,17 @@ def dumpstruct(t, data=None, offset=0):
18921893
hexdump(data, palette, offset=offset)
18931894
print()
18941895
print(out)
1896+
1897+
def serialize(ft):
1898+
if isinstance(ft, Instance):
1899+
newDict = {}
1900+
newDict['_type'] = str(ft._type.name)
1901+
newDict['_values'] = ft._values
1902+
newDict['_sizes'] = ft._sizes
1903+
return newDict
1904+
1905+
def dumpstructjson(ft):
1906+
return json.dumps(ft, default=serialize)
1907+
1908+
def loadstructjson(ft):
1909+
return json.loads(dumpstructjson(ft))

0 commit comments

Comments
 (0)