0

我试图弄清楚这是否是HtmlJson.java或我的实现中的错误。我刚刚升级到 PlayN 1.2 版。我的代码尝试将 a 加载Json.ObjectJson.Writer. 这适用于 Java 版本。

这是下面的代码。异常由行引发jsonWriter.object(jsonData);(注意代码中的注释):

private void loadStoredData() {
    // storage parameters
    String storageKey = "jsonData";
    Json.Object jsonData = PlayN.json().createObject();        

    // attempt to load stored data
    String jsonString = PlayN.storage().getItem(storageKey);

    // if not loaded, create stored data
    if ( jsonString == null ) {
        DemoApi.log("stored data not found");
        jsonData.put("firstWrite", new Date().toString());

    // else display data
    } else {
        jsonData = PlayN.json().parse(jsonString);
        DemoApi.log("stored data loaded");
        DemoApi.log("data first written at " + jsonData.getString("firstWrite"));
        DemoApi.log("data last read at " + jsonData.getString("lastRead"));
        DemoApi.log("data last written at " + jsonData.getString("lastWrite"));
    }

    // update last read
    jsonData.put("lastRead", new Date().toString());

    // write data
    Json.Writer jsonWriter = PlayN.json().newWriter();
    jsonWriter.object(jsonData);        // <--- EXCEPTION RAISED HERE
    jsonString = jsonWriter.write();

    // store data as json
    PlayN.storage().setItem(storageKey, jsonString);

    // confirm
    if ( PlayN.storage().isPersisted() ) {
        DemoApi.log("data successfully persisted");
    } else {
        DemoApi.log("failed to persist data");
    }
}

例外:

ERROR: Uncaught Exception: 
java.lang.ClassCastException: com.google.gwt.core.client.JavaScriptObject$ cannot be cast to java.lang.String
    at playn.html.HtmlJson$HtmlObject$.write$(HtmlJson.java:356)
    at com.google.gwt.core.client.JavaScriptObject$.playn_core_Json$Object_write(JavaScriptObject.java)
    at playn.core.json.JsonWriterBase.object(JsonWriterBase.java:119)
    at playn.core.json.JsonWriterBase.object(JsonWriterBase.java:90)
    at klenwell.sandbox.core.controllers.SandboxController.loadStoredData(SandboxController.java:121)

如果这是我的代码中的错误,我该如何纠正它?

4

1 回答 1

0

我找到了一个替代方案。不像原始代码那么优雅,但它在 HTML5 中测试成功:

private void loadStoredData() {
    // storage parameters
    String storageKey = "jsonData";
    Json.Object jsonData = PlayN.json().createObject();  

    // to reset storage, uncomment this line
    //PlayN.storage().removeItem(storageKey);

    // attempt to load stored data
    String jsonString = PlayN.storage().getItem(storageKey);

    // if not loaded, create stored data
    if ( jsonString == null ) {
        DemoApi.log("stored data not found");
        jsonData.put("firstWrite", new Date().toString());

    // else display data
    } else {
        jsonData = PlayN.json().parse(jsonString);
        DemoApi.log("stored data loaded");
        DemoApi.log("data first written at " + jsonData.getString("firstWrite"));
        DemoApi.log("data last read at " + jsonData.getString("lastRead"));
        DemoApi.log("data last written at " + jsonData.getString("lastWrite"));
    }

    // update last read
    jsonData.put("lastRead", new Date().toString());

    // write data (this works in Java -- not in HTML)
    // see http://stackoverflow.com/q/10425877/1093087
    /*
    Json.Writer jsonWriter = PlayN.json().newWriter();
    jsonWriter.object(jsonData).end();
    jsonString = jsonWriter.write();
    */

    // alternative write routine
    Json.Writer jsonWriter = PlayN.json().newWriter();
    jsonWriter.object();
    for ( String key : jsonData.keys() ) {
        jsonWriter.value(key, jsonData.getString(key));
    }
    jsonWriter.end();
    jsonString = jsonWriter.write();

    // store data as json
    PlayN.storage().setItem(storageKey, jsonString);

    // confirm
    if ( PlayN.storage().isPersisted() ) {
        DemoApi.log("data successfully persisted");
    } else {
        DemoApi.log("failed to persist data");
    }
}

DemoApi.log,作为记录,是我在浏览器中进行测试时将输出记录到屏幕上的一种方法。后来我发现该PlayN.log()服务可以正常记录到 Chrome 控制台。

于 2012-05-04T05:26:52.750 回答