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

JavaFX Properties Lose Meta-Data #32

Open
WildeTechSolutions opened this issue Jul 12, 2023 · 5 comments
Open

JavaFX Properties Lose Meta-Data #32

WildeTechSolutions opened this issue Jul 12, 2023 · 5 comments

Comments

@WildeTechSolutions
Copy link

JavaFX Properties are typically initialized like this:

private final StringProperty comment = new SimpleStringProperty(this, "comment");

Unfortunately, FxGson reconstructs JavaFX properties like this:

private final StringProperty comment = new SimpleStringProperty();

thus losing its reference to its owning bean as well as its name.

One of the biggest benefits of including a reference to the bean is that generic listeners can be placed on properties and access the owning bean from within the listener. For me, this renders FxGson unusable, as this is a very common usage of JavaFX. Otherwise, FxGson works very well.

@joffrey-bion
Copy link
Owner

joffrey-bion commented Jul 12, 2023

Hi, thanks for the report. This is indeed a shortcoming in the current design of FX Gson.

By design, FX Gson's intention is to serialize/deserialize properties as their simple value (instead of serializing all the things that come with it like its name, containing bean, and all internal fields). This means that there is no way to know at deserialization time what the name of the property used to be, nor whether the enclosing bean was provided to the constructor in the past.

Maybe we could add a way to customize this behavior globally so FXGson automatically sets the property name to the name of the corresponding field, and/or sets the bean to the enclosing object.

Although I'm a bit reluctant for the property name heuristic, because that would technically be incorrect. The discrepancy would be made obvious if someone defines a property like this:

private final StringProperty myField = new SimpleStringProperty(this, "somethingThatIsNotMyField");

How can the deserializer know that the property name is not supposed to be myField in this case?

@joffrey-bion
Copy link
Owner

joffrey-bion commented Jul 12, 2023

I'm surprised people are still using Gson, by the way! What made you use this one over other JSON libraries that are more actively maintained?

@WildeTechSolutions
Copy link
Author

WildeTechSolutions commented Jul 13, 2023

I'm using Jackson as well which I think will work better for my use case in JavaFX since it utilizes getters and setters (thus avoiding the issue in this thread). There are a few things I don't like as much about Jackson and other than with JavaFX, Gson does work very well.

@joffrey-bion
Copy link
Owner

Yeah using setter injection is the main reason why Jackson is safer. Gson is kinda "hacky" in the sense that it uses field injection, which doesn't respect the initialization of the objects as they are defined in the source code. This is a problem for every class that has some sort of initialization, not just for JavaFX, that's why I don't like it that much anymore.

@WildeTechSolutions
Copy link
Author

Yeah, I wish Gson had a better option for that. Otherwise your library works very well, but it seems Jackson as-is is a good serializer/deserializer for JavaFX.

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

No branches or pull requests

2 participants