top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Mongodb: Is it possible to save javafx properties as key:value pair and not as object?

0 votes
642 views
Mongodb: Is it possible to save javafx properties as key:value pair and not as object?
posted Mar 19, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

yes it is possible and you can do it by following these steps.You need to register a GSON type adapter to perform a custom serialization and deserialization of the property:first create 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()
        );
    }
}

then create main in 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);
    }
}

our data entry class,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);
    }
}

when you run the PropertySerializationTest.java then you will get this output:
Serialized Data:
{"companyName":"toolhouse","_id":16,"employees":18,"testString":"Property"}
De-serialized/Re-serialized Data:
{"companyName":"toolhouse","_id":16,"employees":18,"testString":"Property"}
Reference: http://stackoverflow.com/questions/36046679/saving-javafx-properties-in-mongodb

answer Mar 22, 2016 by Shivam Kumar Pandey
Similar Questions
+1 vote

Query description: I have a collection name student_db and added a document in a collection with entries (name: "alok, age : 31 and profession: "xyz"). After inserting the document into student_db database, I want to add another (key:value ) pair i.e. salary: 50000.

Can someone help me out to resolve this problem ?

0 votes

I want to implement the encryption and that to only to value in key-value pair in mongodb .Can anyone please provide a workable solution?

+2 votes

As we can write ".sql" file in sql for creating schema and other commands. can we also write the similar something like ".nosql" or ".mongodb" file??

...