Skip to content

Conversation

@urucoder
Copy link

Changes from this PR:

  • Check the flags and set the class type
  • Extends the API to add a custom read method to support WRCLASS
  • Adds previous object support as an array ClassDesc
  • _do_classdesc and _read_class_data refactors
  • Removes unused "must_be_new" parameter

@urucoder
Copy link
Author

urucoder commented Apr 12, 2020

Test case:

  • Java custom serialization:
public class App implements Serializable {
	int val = 3;
	Obj custom_obj = new Obj(1, 4.5);
    public static void main( String[] args ) throws Exception
    {
       App programm = new App();
       programm.start();
    }

    public void start() throws Exception {
        FileOutputStream fileOut = new FileOutputStream("/tmp/serialized.ser");
        ObjectOutputStream out = new ObjectOutputStream(fileOut);
        this.writeObject(out);
        out.close();
        fileOut.close();
    }

    private void writeObject (ObjectOutputStream out) throws IOException {
        out.writeObject (this.custom_obj);
        out.writeInt (this.val);
    }
}

class Obj implements Serializable{
	int num;
	double doub;
	
	Obj(int num, double doub) {
		this.num = num;
		this.doub = doub;
	}
	private void writeObject (ObjectOutputStream out) throws IOException {
        ObjectOutputStream.PutField fields = out.putFields();
        fields.put("num", this.num);
        fields.put("doub", this.doub);
        out.writeFields();
    }
}

Python custom transformer:

class CustomTransformer:
    def __init__(self):
        self.name = "topics.Obj"
        self.fields = {
            'doub': javaobj.beans.FieldType.DOUBLE,
            'num': javaobj.beans.FieldType.INTEGER,
        }

    def create_instance(self, classdesc):
        if classdesc.name == self.name:
            # We can handle this class description
            return CustomTransformer()

        return None

    def load_custom_writeObject(self, parser, reader, name):
        if name == self.name:
            return self.get_class_description(parser)
        return None

    def get_class_description(self, parser):
        fields = []
        values = []
        for field_name, field_type in self.fields.items():
            values.append(parser._read_field_value(field_type))
            fields.append(javaobj.beans.JavaField(field_type, field_name))

        class_desc = javaobj.beans.JavaClassDesc(javaobj.beans.ClassDescType.NORMALCLASS)
        class_desc.name = self.name
        class_desc.desc_flags = javaobj.beans.ClassDataType.EXTERNAL_CONTENTS
        class_desc.fields = fields
        class_desc.field_data = values
        return class_desc

@tcalmant
Copy link
Owner

Hi,
I've just updated your PR comment for a better format.
I'm reviewing the code, thanks for your contribution :)

@tcalmant
Copy link
Owner

3 tests are not working with this PR:

I'm investigating

@tcalmant
Copy link
Owner

OK so the issue is that the new transformer API wasn't backported to the DefaultObjectTransformer.
Could you update the javaobj.v2.transformers module accordingly, please ?

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 79.361% when pulling 574b076 on UruDev:master into cd94bff on tcalmant:master.

@urucoder
Copy link
Author

All test fixed

@tcalmant
Copy link
Owner

tcalmant commented Apr 13, 2020

OK, good for me
Thanks 👍

@tcalmant tcalmant merged commit a0bd51c into tcalmant:master Apr 13, 2020
@tcalmant
Copy link
Owner

Last thing: which names do I have to add to the AUTHORS file ?

@urucoder
Copy link
Author

Federico Alves

@tcalmant
Copy link
Owner

Hi again,
Could you provide a sample where the transformer is used ?
I can't find a situation where it's the case.
Also, what is load_custom_writeObject supposed to returned ?

@urucoder
Copy link
Author

Hi! Yes sure, I'll make a new PR with a test implementation for a custom case.
As in the example of the first comment the load_custom_writeObject is intended to return a classDesc instance, I'll fix the class annotations too. Sorry about the missing of doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants