fx-gson
fx-gson copied to clipboard
Property object name is empty
When deserialize the property object name is empty , but value is correct. This is needed part for property object. How can we fix this ? Is there away to add name field, if it is not empty ?
Example code here :
import com.google.gson.Gson;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
import org.hildan.fxgson.FxGson;
import java.io.*;
import java.text.DecimalFormat;
public class AppPreferences {
//public static String separator = ",";
public static String separator = new DecimalFormat("##.##").format(1.5).contains(",") ? ";" : ",";
private static String filePath;
private DoubleProperty commission = new SimpleDoubleProperty(null,"Commission", 0.001);
public AppPreferences(String filePath) {
AppPreferences.filePath = filePath;
}
public void save() throws IOException {
File file = new File(filePath);
if(!file.exists()){
FileHelper.createFile(filePath);
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
Gson gson = FxGson.coreBuilder().setPrettyPrinting().disableHtmlEscaping().create();
String json = gson.toJson(this);
byte[] copyBuffer = json.getBytes();
out.write(copyBuffer, 0, copyBuffer.length);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}
}
public AppPreferences fromJson() throws Exception {
File file = new File(filePath);
if(file.exists())
{
FileInputStream inputStream = new FileInputStream(file);
try (BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream)))
{
Gson gson = FxGson.coreBuilder().setPrettyPrinting().disableHtmlEscaping().create();
AppPreferences appPreferences = gson.fromJson(inputReader, AppPreferences.class);
return appPreferences;
} catch (final IOException e) {
e.printStackTrace();
}
}
return new AppPreferences(filePath);
}
public void getVariables(ObservableList<Observable> variables, ObservableMap<ObjectProperty<?>, ObservableList<?>> options){
variables.addAll(commissionProperty());
}
public double getCommission() {
return commission.get();
}
public DoubleProperty commissionProperty() {
return commission;
}
public void setCommission(double commission) {
this.commission.set(commission);
}
}
Interesting use case. This behaviour is expected (at least for now), given that the design goal of this library was initially to serialize all properties as their values (no information about the properties themselves are preserved in the JSON).
A work around that should be possible is creating your own type adapter / (de)serializer for the types that contain named properties.
We can't make FxGson serialize the name only when there is one, because at deserialization time you can't know in advance the expected structure. However, we could make the adapters configurable so that they can preserve property names in all types, but this would completely change the payload (basically instead of a simple value you would get a wrapper object with name and value, for all properties).
That is an actual feature that require some work, but it could be done. There hasn't been new features in FX Gson for a little while because Gson itself doesn't seem to get much updates. But if you really need this I can find some time to add it.
However, we could also make the adapters configurable so that they can preserve property names in all types,
This is really accetable for us. At least, if name is not empty, that can be supported. We designed a gui form link variables dynamically, that all fields is Property. At this point needed name filed. Before we did manually, but i saw FxGson did already that. At the following link, you can see code part. https://drive.google.com/drive/folders/1FOOQo_MCKPDuE0i51Ik08yjLtezn_tsA?usp=sharing
As you said, Gson itself doesn't seem to get much updates.