Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added json dumps/loads support. #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dissect/cstruct/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
cstruct,
ctypes,
dumpstruct,
dumpstructjson,
loadstructjson,
hexdump,
Instance,
PointerInstance,
Expand All @@ -19,6 +21,8 @@
"cstruct",
"ctypes",
"dumpstruct",
"dumpstructjson",
"loadstructjson",
"hexdump",
"Instance",
"PointerInstance",
Expand Down
15 changes: 15 additions & 0 deletions dissect/cstruct/cstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import ctypes as _ctypes
from io import BytesIO
from collections import OrderedDict
import json

try:
from builtins import bytes as newbytes
Expand Down Expand Up @@ -1892,3 +1893,17 @@ def dumpstruct(t, data=None, offset=0):
hexdump(data, palette, offset=offset)
print()
print(out)

def serialize(ft):
if isinstance(ft, Instance):
newDict = {}
newDict['_type'] = str(ft._type.name)
newDict['_values'] = ft._values
newDict['_sizes'] = ft._sizes
return newDict

def dumpstructjson(ft):
return json.dumps(ft, default=serialize)

def loadstructjson(ft):
return json.loads(dumpstructjson(ft))