javaobj-py3 is a Python library designed for serializing and de-serializing Java objects, facilitating interoperability between Python and Java applications. It is currently at version 0.4.4 and receives irregular but active updates, with the latest significant features and fixes released recently.
pip install javaobj-py3Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to deserialize common Java types (String, List) into their Python equivalents using `javaobj.loads()`. It also shows a basic example of serializing a Python string to Java format using `javaobj.dumps()` and introduces the `v2_loads` parser for advanced deserialization.
Choose the appropriate parser: use `javaobj.loads` (v1) for both loading and dumping, or `javaobj.v2.loads` for more robust loading if serialization is not needed. Be explicit in your imports to avoid confusion.
Install 'numpy' if you need NumPy array support: `pip install numpy`. Pass `use_numpy_arrays=True` to `javaobj.loads()` or `JavaObjectUnmarshaller` if you expect NumPy arrays.
If working with GZipped bytes in memory, decompress them manually before passing to `javaobj.loads()` (e.g., using `gzip.decompress`). For file paths or open file objects, prefer `javaobj.load()`.
Be aware of the type mapping. Check `type(result)` after `loads()` and use `isinstance(result, javaobj.JavaObject)` if you expect custom Java objects to access their fields.
Verify the encoding used on the Java side. If issues persist, consider inspecting the raw bytes or implementing custom object transformers to debug encoding problems.
Ensure the input bytes object or file stream contains correctly serialized Java objects. Verify the source of the data and its serialization method.
Provide a complete and uncorrupted Java serialized byte stream. Check if the entire serialized object data was read from the source (e.g., file, network stream).
If using an older version, try deserializing with `javaobj.v2` (e.g., `import javaobj.v2 as javaobj`) as it's based on `jdeserialize` and supports more cases. Otherwise, simplify the Java objects being serialized or investigate the exact Java serialization format being used.
Inspect the deserialized `JavaObject` structure (e.g., `print(pobj.__dict__)` or `dir(pobj)`) to see the available attributes. Ensure the attribute name matches the expected Java field name. For complex Java objects, custom object transformers might be needed to map Java fields to Python attributes correctly.