Module refinery.units.formats.java.deserialize

Expand source code Browse git
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from refinery.units import Unit
from refinery.lib.json import BytesAsArrayEncoder


class JavaEncoder(BytesAsArrayEncoder):

    @classmethod
    def _is_byte_array(cls, obj) -> bool:
        if super()._is_byte_array(obj):
            return True
        elif not isinstance(obj, list) or not obj:
            return False
        if not all(isinstance(t, int) for t in obj):
            return False
        if all(t in range(-0x80, 0x80) for t in obj):
            return True
        if all(t in range(0x100) for t in obj):
            return True
        return False

    def convert_key(self, key):
        if isinstance(key, dsjava._javaobj.beans.JavaString):
            return str(key)
        return key

    def preprocess(self, obj):
        if isinstance(obj, dict):
            # Recursively convert dictionary keys
            return {self.convert_key(k): self.preprocess(v) for k, v in obj.items()}
        return obj

    def encode(self, obj):
        obj = self.preprocess(obj)
        return super().encode(obj)

    def default(self, obj):
        try:
            return super().default(obj)
        except TypeError:
            if isinstance(obj, dsjava._javaobj.beans.JavaString):
                return str(obj)
            if isinstance(obj, dsjava._javaobj.beans.JavaInstance):
                cd = obj.classdesc
                fd = obj.field_data[cd]
                return dict(
                    isException=cd.is_exception,
                    isInnerClass=cd.is_inner_class,
                    isLocalInnerClass=cd.is_local_inner_class,
                    isStaticMemberClass=cd.is_static_member_class,
                    name=cd.name,
                    fields={t.name: v for t, v in fd.items()}
                )
            if isinstance(obj, dsjava._javaobj.beans.JavaField):
                return obj.class_name
            if isinstance(obj, dsjava._javaobj.beans.JavaEnum):
                return obj.value
            if isinstance(obj, dsjava._javaobj.beans.JavaArray):
                if obj.classdesc.name == '[B':
                    return bytearray(t & 0xFF for t in obj)
            raise


class dsjava(Unit):
    """
    Deserialize Java serialized data and re-serialize as JSON.
    """
    @Unit.Requires('javaobj-py3>=0.4.0.1', 'formats')
    def _javaobj():
        import javaobj.v2
        return javaobj.v2

    def process(self, data):
        with JavaEncoder as encoder:
            return encoder.dumps(self._javaobj.loads(data)).encode(self.codec)

Classes

class JavaEncoder (*args, **kwargs)

This JSON Encoder encodes byte strings as arrays of integers.

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is None and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.

If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

Expand source code Browse git
class JavaEncoder(BytesAsArrayEncoder):

    @classmethod
    def _is_byte_array(cls, obj) -> bool:
        if super()._is_byte_array(obj):
            return True
        elif not isinstance(obj, list) or not obj:
            return False
        if not all(isinstance(t, int) for t in obj):
            return False
        if all(t in range(-0x80, 0x80) for t in obj):
            return True
        if all(t in range(0x100) for t in obj):
            return True
        return False

    def convert_key(self, key):
        if isinstance(key, dsjava._javaobj.beans.JavaString):
            return str(key)
        return key

    def preprocess(self, obj):
        if isinstance(obj, dict):
            # Recursively convert dictionary keys
            return {self.convert_key(k): self.preprocess(v) for k, v in obj.items()}
        return obj

    def encode(self, obj):
        obj = self.preprocess(obj)
        return super().encode(obj)

    def default(self, obj):
        try:
            return super().default(obj)
        except TypeError:
            if isinstance(obj, dsjava._javaobj.beans.JavaString):
                return str(obj)
            if isinstance(obj, dsjava._javaobj.beans.JavaInstance):
                cd = obj.classdesc
                fd = obj.field_data[cd]
                return dict(
                    isException=cd.is_exception,
                    isInnerClass=cd.is_inner_class,
                    isLocalInnerClass=cd.is_local_inner_class,
                    isStaticMemberClass=cd.is_static_member_class,
                    name=cd.name,
                    fields={t.name: v for t, v in fd.items()}
                )
            if isinstance(obj, dsjava._javaobj.beans.JavaField):
                return obj.class_name
            if isinstance(obj, dsjava._javaobj.beans.JavaEnum):
                return obj.value
            if isinstance(obj, dsjava._javaobj.beans.JavaArray):
                if obj.classdesc.name == '[B':
                    return bytearray(t & 0xFF for t in obj)
            raise

Ancestors

Methods

def convert_key(self, key)
Expand source code Browse git
def convert_key(self, key):
    if isinstance(key, dsjava._javaobj.beans.JavaString):
        return str(key)
    return key
def preprocess(self, obj)
Expand source code Browse git
def preprocess(self, obj):
    if isinstance(obj, dict):
        # Recursively convert dictionary keys
        return {self.convert_key(k): self.preprocess(v) for k, v in obj.items()}
    return obj

Inherited members

class dsjava

Deserialize Java serialized data and re-serialize as JSON.

Expand source code Browse git
class dsjava(Unit):
    """
    Deserialize Java serialized data and re-serialize as JSON.
    """
    @Unit.Requires('javaobj-py3>=0.4.0.1', 'formats')
    def _javaobj():
        import javaobj.v2
        return javaobj.v2

    def process(self, data):
        with JavaEncoder as encoder:
            return encoder.dumps(self._javaobj.loads(data)).encode(self.codec)

Ancestors

Class variables

var required_dependencies
var optional_dependencies

Inherited members