Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Serializable? #18

Closed
kainoa21 opened this issue Jul 5, 2018 · 9 comments
Closed

Implement Serializable? #18

kainoa21 opened this issue Jul 5, 2018 · 9 comments
Milestone

Comments

@kainoa21
Copy link

kainoa21 commented Jul 5, 2018

Has there been previous consideration given to making these classes serializable? I was attempting to use this library in a distributed system and being able to serialize and deserialize the citygml classes would greatly improve the usability.

@clausnagel
Copy link
Member

No considerations so far because I am using CityGML or CityJSON for de-/serialization. I like your idea and it shouldn't be too hard to implement it. Btw, your are talking about the classes in org.citygml4j.model, right?

@clausnagel clausnagel added this to the v2.8 milestone Jul 6, 2018
@kainoa21
Copy link
Author

kainoa21 commented Jul 6, 2018

Correct! I think it is just a matter of adding the interface and potentially adding serialVersionUID. Agreed that CityGML or CityJSON also works for serialization but a bit bloated for inter-process communication. If you are open to it I can submit a PR.

@clausnagel
Copy link
Member

Yes, a PR would be great. I think, the root interface of all model classes is org.citygml4j.model.common.base.ModelObject. So it should be sufficient to let this interface extend Serializable.

I am not sure about the serialVersionUID field. Is it required for every non-abstract class? Or is it sufficient to be present for the (abstract) root class of an inheritance chain to be considered for all classes in that inheritance chain? If the latter is possible, this would substantially reduce the effort.

@kainoa21
Copy link
Author

kainoa21 commented Jul 6, 2018

I am not an expert in this space, but my sense is that any class which contains fields that you intend to be restored after deserialization need to be marked as Serializable otherwise the default empty constructor will be called. So I think this means that most of the classes in the org.citygml4j.model will need to explicitly implement Serializable (and therefore should probably maintain their own serialVersionUID).

@clausnagel
Copy link
Member

clausnagel commented Jul 6, 2018

I did a quick test. It seems to be sufficient to let the interface org.citygml4j.model.common.base.ModelObject and the abstract class org.citygml4j.model.module.AbstractModule extend resp. implement the Serializable interface. After that I was able to serialize and deserialize a citygml4j object. Well, I have not worked with Java serialization before so I was surprised to learn that it is quite slow.

Afterwards, I added a serialVersionUID to org.citygml4j.model.gml.base.AbstractGML which is a base class for many citygml4j classes. With that change, I was not able to deserialize the object from my previous test anymore. So it seems to be sufficient to use serialVersionUID on root classes of an inheritance chain.

@mprins
Copy link

mprins commented Jul 7, 2018

You should read https://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it

Afterwards, I added a serialVersionUID to org.citygml4j.model.gml.base.AbstractGML which is a base class for many citygml4j classes. With that change, I was not able to deserialize the object from my previous test anymore. So it seems to be sufficient to use serialVersionUID on root classes of an inheritance chain.

this conforms to the spec; because you now have a different version of the object so it is assumed incompatible.
if you always want to create compatible java serializations each generated class should have a serialVersionUID, it can be 1L, otherwise data may not be deserialized in a different JVM

@clausnagel
Copy link
Member

@kainoa21 Sorry for not being very active on this lately. I pushed the implementation of the Serializable interface to the serializable branch for testing. It required just a small change to the model classes as written above. And when using buffered input and output streams, serialization performance is just fine. So feel free to test the branch and provide feedback or edits.

@mprins Thanks for your input.

@clausnagel
Copy link
Member

merged serializable branch into master (7c32e28)

@kainoa21
Copy link
Author

This is great @clausnagel and I have verified that is working for my use case. Thank you for enabling this!

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

No branches or pull requests

3 participants