Skip to content

Commit cdba8b9

Browse files
committed
Add support for custom marshallers in load() and loads()
1 parent 99e082d commit cdba8b9

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

javaobj.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,42 @@ def read_to_str(data):
146146
# ------------------------------------------------------------------------------
147147

148148

149-
def load(file_object, ignore_remaining_data=False):
149+
def load(file_object, *transformers, ignore_remaining_data=False):
150150
"""
151151
Deserializes Java primitive data and objects serialized using
152152
ObjectOutputStream from a file-like object.
153153
154154
:param file_object: A file-like object
155+
:param transformers: Custom transformers to use
155156
:param ignore_remaining_data: If True, don't log an error when unused
156157
trailing bytes are remaining
157158
:return: The deserialized object
158159
"""
159160
marshaller = JavaObjectUnmarshaller(file_object)
161+
162+
# Add custom transformers first
163+
for transformer in transformers:
164+
marshaller.add_transformer(transformer)
160165
marshaller.add_transformer(DefaultObjectTransformer())
166+
167+
# Read the file object
161168
return marshaller.readObject(ignore_remaining_data=ignore_remaining_data)
162169

163170

164-
def loads(string, ignore_remaining_data=False):
171+
def loads(string, *transformers, ignore_remaining_data=False):
165172
"""
166173
Deserializes Java objects and primitive data serialized using
167174
ObjectOutputStream from a string.
168175
169176
:param string: A Java data string
177+
:param transformers: Custom transformers to use
170178
:param ignore_remaining_data: If True, don't log an error when unused
171179
trailing bytes are remaining
172180
:return: The deserialized object
173181
"""
174-
file_like = BytesIO(string)
175-
marshaller = JavaObjectUnmarshaller(file_like)
176-
marshaller.add_transformer(DefaultObjectTransformer())
177-
return marshaller.readObject(ignore_remaining_data=ignore_remaining_data)
182+
# Reuse the load method (avoid code duplication)
183+
return load(BytesIO(string), *transformers,
184+
ignore_remaining_data=ignore_remaining_data)
178185

179186

180187
def dumps(obj):

0 commit comments

Comments
 (0)