将包含ListProperty的PropertyBusinessObject存储到JSON

时间:2019-05-18 18:19:23

标签: codenameone

我很难将包含PropertyBusinessObject的{​​{1}}存储到JSON。我创建了以下测试用例,该用例在第一次执行时工作正常,但在第二次执行时失败。我的代码错了吗?

我的测试用例由四个类组成:

TestCaseJSON

ListProperty

数据库

public void init(Object context) {
    [...]       

    DB.initPersistenceDB();
}

public void start() {
    if(current != null){
        current.show();
        return;
    }
    Form hi = new Form("Hi World", BoxLayout.y());
    hi.add(new Label("Hi World"));
    hi.show();

    DB.userDB.uniqueID.set("1");
    DB.userDB.authToken.set("myToken");
    InjuryDAO newInjury = new InjuryDAO();
    DB.userDB.injuries.add(newInjury);
    DB.saveDB();
}

UserDB

public class DB {

    // User Database
    public static UserDB userDB = new UserDB();

    /**
     * Call this method in the init(), it allows the database persistence and
     * automatically restores the saved DB.
     */
    public static void initPersistenceDB() {
        Log.p("DB.initPersistenceDB executing", Log.DEBUG);
        restoreDB();
    }

    /**
     * Saves the UserDB to the Storage
     */
    public static void saveDB() {
        Log.p("DB.saveUserDB executing", Log.INFO);
        userDB.getPropertyIndex().storeJSON("UserDB");
    }

    /**
     * Restore the UserDB from the Storage, if it was previously saved to; this
     * method is called by initPersistenceDB()
     */
    private static void restoreDB() {
        Log.p("DB.restoreUserDB executing", Log.INFO);
        if (Storage.getInstance().exists("UserDB")) {
            userDB.getPropertyIndex().loadJSON("UserDB");
        }
    }

}

伤害DAO

public class UserDB implements PropertyBusinessObject {

    public final Property<String, UserDB> uniqueID = new Property<>("uniqueID", null);
    public final Property<String, UserDB> authToken = new Property<>("authToken", null);

    public final ListProperty<InjuryDAO, UserDB> injuries = new ListProperty<>("injuries");

    public final PropertyIndex idx = new PropertyIndex(this, "UserDB",
            uniqueID, authToken,
            injuries
    );

    @Override
    public PropertyIndex getPropertyIndex() {
        return idx;
    }

}

在Simulator中执行之前,我清除了public class InjuryDAO implements PropertyBusinessObject { // extra info public final LongProperty<InjuryDAO> injuryCreationTime = new LongProperty<>("injuryCreationTime", System.currentTimeMillis()); // mandatory info public final Property<Date, InjuryDAO> dateInjury = new Property<>("dateInjury", Date.class); private final PropertyIndex idx = new PropertyIndex(this, "InjuryDAO", injuryCreationTime, dateInjury ); @Override public PropertyIndex getPropertyIndex() { return idx; } } 目录(相当于Simulator,Clean Storage)。

第一次执行后,这是日志:

.cn1

这是[EDT] 0:0:0,110 - DB.initPersistenceDB executing [EDT] 0:0:0,110 - DB.restoreUserDB executing [EDT] 0:0:0,178 - DB.saveUserDB executing 目录中UserDB文件的内容:

.cn1

是的。

第二次执行后,日志与前一个日志相同,但这是{ "injuries": [{"injuryCreationTime": 1558202520667}], "authToken": "myToken", "uniqueID": "1" } 文件的错误内容(请注意,前一个时间戳丢失了):< / p>

UserDB

预期的正确结果应该是:

{
  "injuries": [
    {"injuries": []},
    {"injuryCreationTime": 1558202625655}
  ],
  "authToken": "myToken",
  "uniqueID": "1"
}

感谢您的支持。

1 个答案:

答案 0 :(得分:1)

我认为:

public final ListProperty<InjuryDAO, UserDB> injuries = new ListProperty<>("injuries");

应更改为:

public final ListProperty<InjuryDAO, UserDB> injuries = new ListProperty<>("injuries", InjuryDAO.class);

如果这不起作用,请尝试:

public final ListProperty<InjuryDAO, UserDB> injuries = new ListProperty<>("injuries", InjuryDAO.class, null);