FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Adds support for SC_WRITE_METHOD by urucoder · Pull Request #38 · tcalmant/python-javaobj · GitHub

Adds support for SC_WRITE_METHOD - #38

Merged
tcalmant merged 3 commits into
tcalmant:masterfrom
urucoder:master
Apr 13, 2020
Merged

Adds support for SC_WRITE_METHOD#38
tcalmant merged 3 commits into
tcalmant:masterfrom
urucoder:master

Conversation

Copy link
Copy Markdown

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 commented Apr 12, 2020
edited by tcalmant
Loading

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Owner

3 tests are not working with this PR:

I'm investigating

Copy link
Copy Markdown
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 ?

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Author

All test fixed

tcalmant commented Apr 13, 2020
edited
Loading

Copy link
Copy Markdown
Owner

OK, good for me
Thanks 👍

tcalmant merged commit a0bd51c into tcalmant:master Apr 13, 2020

Copy link
Copy Markdown
Owner

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

Copy link
Copy Markdown
Author

Federico Alves

Copy link
Copy Markdown
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 ?

Copy link
Copy Markdown
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

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL