【问题标题】:Saving javafx properties in mongodb在 mongodb 中保存 javafx 属性
【发布时间】:2016-07-02 23:34:31
【问题描述】:

这次我在玩 mongodb。我知道如何插入文档、更新、json格式等等。最后,我使用 javafx 字符串属性创建了我的数据持有者类,以在表格视图中显示查询结果。所以现在的问题是,如果我将类保存在 mongodb 中,它会将属性保存为具有三个子值(属性、值、有效)的对象。是否有可能将其保存为键:值,就像普通字符串一样?对象的问题是,查询比 key:value 复杂得多。

首先我创建我的对象,使用 Google gson 库将其转换为 json,然后通过数据库中的文档类将其保存。

最小示例:数据类

public class DatabaseEntry {

private String companyName;
private int _id;
private int emploees;
private ArrayList<Contact> contacts = new ArrayList();
private ArrayList<Invoice> invoices = new ArrayList();
private StringProperty testString = new SimpleStringProperty();

public DatabaseEntry(String companyName, int emploees, int _id, String test){
    this.companyName=companyName;
    this.emploees=emploees;
    this._id=_id;
    this.testString.setValue(test);
}

主类;

    public class MongoDB {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        DatabaseEntry entry = new DatabaseEntry("toolhouse", 18, 16,"Property");
        System.out.println(new Gson().toJson(objToSave));
    }
}

输出是:

"testString":{"name":"", “价值”:“财产”, “有效”:假}

【问题讨论】:

  • 我是否正确地说 mongodb 与问题稍微无关,而您要解决的直接问题是:“我如何使用 gson 将 JavaFX 类中的属性序列化和反序列化为键/值 json 对而不是属性/值/有效对象?”。另外,即使您没有提到它,我猜 ObservableLists 也会为您呈现类似的序列化/反序列化问题?您可能希望为此部分添加mcve:“我创建我的对象,使用 Google gson 库将其转换为 json”。
  • 我添加了一个 mcve。是的,你是对的,mongodb 与 json 格式保存无关......但我想提一下,因为可能有一个构建解决方案,或者有人遇到这个问题并用一些 mongodb 函数解决了它。
  • 有人有解决方案吗?

标签: java mongodb javafx gson


【解决方案1】:

您需要注册一个GSON type adapter 来执行属性的自定义序列化和反序列化:

StringPropertyAdapter.java

import com.google.gson.*;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import java.lang.reflect.Type;

public class StringPropertyAdapter implements
        JsonSerializer<StringProperty>, 
        JsonDeserializer<StringProperty> {
    @Override
    public JsonElement serialize(
            StringProperty property, 
            Type type, 
            JsonSerializationContext jsonSerializationContext
    ) {
        return new JsonPrimitive(
                property.getValue()
        );
    }

    @Override
    public StringProperty deserialize(
            JsonElement json, 
            Type type, 
            JsonDeserializationContext jsonDeserializationContext
    ) throws JsonParseException {
        return new SimpleStringProperty(
                json.getAsJsonPrimitive().getAsString()
        );
    }
}

PropertySerializationTest.java

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javafx.beans.property.StringProperty;

public class PropertySerializationTest {
    public static void main(String[] args) {
        final DatabaseEntry entry = new DatabaseEntry("toolhouse", 18, 16, "Property");
        final GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.registerTypeAdapter(StringProperty.class, new StringPropertyAdapter());
        final Gson gson = gsonBuilder.create();

        System.out.println("Serialized Data:");
        String json = gson.toJson(entry);
        System.out.println(json);

        System.out.println("De-serialized/Re-serialized Data:");
        DatabaseEntry rebuiltEntry = gson.fromJson(json, DatabaseEntry.class);
        String rebuiltJson = gson.toJson(rebuiltEntry);
        System.out.println(rebuiltJson);
    }
}

DatabaseEntry.java

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class DatabaseEntry {

    private String companyName;
    private int _id;
    private int employees;
    private StringProperty testString = new SimpleStringProperty();

    public DatabaseEntry(String companyName, int employees, int _id, String test) {
        this.companyName = companyName;
        this.employees = employees;
        this._id = _id;
        this.testString.setValue(test);
    }
}

程序输出

Serialized Data:
{"companyName":"toolhouse","_id":16,"employees":18,"testString":"Property"}
De-serialized/Re-serialized Data:
{"companyName":"toolhouse","_id":16,"employees":18,"testString":"Property"}

【讨论】:

  • 谢谢你,它工作得非常好。下次我应该更深入地研究 api ...首先我认为我可以使用 Externasizable 接口处理这个问题,但 json 不使用 java 方式。
猜你喜欢
  • 1970-01-01
  • 2017-06-09
  • 2014-11-09
  • 2018-01-22
  • 1970-01-01
  • 2016-03-01
  • 1970-01-01
  • 2012-10-21
  • 2012-08-03
相关资源
最近更新 更多