0

I have a Object bounds controls.

public class Record {
    private final SimpleStringProperty uid = new SimpleStringProperty();
    private final SimpleStringProperty name = new SimpleStringProperty();
    private final SimpleStringProperty person = new SimpleStringProperty();
    //getters and setters

Then, i use com.google.gson to deserialize: new Gson().fromJson(lineStr, Record.class);
It takes a reflect exception:Unable to make field private final java.lang.Object javafx.beans.property.SimpleStringProperty.bean accessible: module javafx.base does not "opens javafx.beans.property" to module com.google.gson
In module-info.java:

module myModule {
    requires javafx.controls;
    requires javafx.fxml;
    requires javafx.base;
    requires org.controlsfx.controls;
    
    opens myModule  to javafx.fxml,com.google.gson;

    exports ......
}

I tried to add opens javafx.beans.property to com.google.gson;, the IDE shows Package not found: javafx.beans.property.
I don't know much about javafx.beans module's dependency relations.I guess the correct solution is to open the javafx.beans.property to gson.Who can help me :) ?

HawkinHu
  • 21
  • 4
  • 2
    Opening the `javafx.base` module to Gson is not the proper solution. The proper solution is to configure Gson to somehow ignore the JavaFX property objects and just deal with the **values** of the JavaFX properties. I'm not greatly familiar with Gson, but there might be a way to tell Gson to use getters/setters instead of field access. Or you can create a custom serializer/deserializer for your model class. Or you can create a DTO-like class specifically for (de)serializing with Gson. – Slaw Apr 27 '23 at 04:39
  • 1
    I don't believe GSON supports "property access" (i.e. using `getX()` and `setX()` methods, instead of directly accessing the fields), which (very much my opinion) betrays a misunderstanding of encapsulation on the part of the library designers. If you can switch to Jackson for your JSON [de]serialization, you can configure it to use the property accessor methods, which will solve the problem. If you are stuck with GSON, the cleanest solution is probably to define custom [de]serializers for your `Record` class. See https://stackoverflow.com/q/76091092/ for something similar. – James_D Apr 27 '23 at 12:47
  • Another option is to write [de]serializers for all the JavaFX property types, and register those. That's quite a bit of work, but fortunately someone [already seems to have done this](https://stackoverflow.com/questions/32794500/serialize-javafx-model-with-gson?rq=2). – James_D Apr 27 '23 at 15:07
  • Thank you all!I wrote a deserializer to solve it, it seems simple and effective, even if not elegant. : ) – HawkinHu Apr 28 '23 at 07:58

0 Answers0