在mongodb中保存javafx属性

时间:2016-03-16 20:51:00

标签: java mongodb javafx gson

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

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

最小示例:数据类

include

主类;

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);
}

输出结果为:

&#34;的TestString&#34; {&#34;名称&#34;:&#34;&#34 ;,               &#34;值&#34;:&#34;属性&#34 ;,               &#34;有效&#34;:假}

1 个答案:

答案 0 :(得分:0)

您需要注册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"}