I'm continuing to see this, even though the resulting json file (after
i add a few brackets to close the arrays) is perfectly valid. The
means there isn't actually anything wrong in the syntax of the file up
to this point.
I'm using gson 2.2.1; though I have seen the same result with 1.7.
The file is a log file used to stream very long sessions of data.
Hence we use the JsonWriter class. There are two block of code which
stream a large number of objects into an array container in the json
file.
Here is the EventLog code. This is called whenever an event needs to
be logged.
public void LogEventPrivate(String name, TimerLogItem timer, String
desc) throws IOException {
double currentTime = ((double)System.currentTimeMillis())/1000; //
seconds
double time = ((currentTime - timer.getZeroTime())) ;
writer.flush();
writer.beginObject();
writer.name(timer.getName());
writer.beginArray();
writer.value(time); // Write time value
writer.endArray();
writer.name("event");
writer.beginObject();
writer.name("name").value(name);
writer.name("desc").value(desc);
writer.endObject();
writer.endObject();
}
and here is the TimerLog code, this is called for each timer tick:
public static void PrintToStreamSingle(String name, double d){
try {
writer.flush();
writer.beginObject(); <--- Fatal exception
here
writer.name(name);
writer.beginArray();
writer.value((float) d);
writer.endArray();
writer.endObject();
writer.flush();
}
catch ....
The resulting stream looks something like this:
{data:[
{
"nt1":[
15.232756614685059
]
},
{
"nt1":[
15.234862327575684
]
},
{
"nt1":[
15.241281509399414
]
},
{
"nt1":[
15.246678352355957
]
},
{
"nt1":[
15.250899314880371
]
},
{
"nt1":[
15.254889488220215
]
},
{
"nt1":[
15.259788513183594
]
},
{
"nt1":[
15.260541915893555
]
},
{
"et1":[
11.162000179290771
],
"event":{
"name":"Events",
"desc":"Text to speech utterance completed."
}
}
<----- FAULT OCCURS HERE. The remaining lines
were added to run it through a prettyprint processor
]
}
Here is the logcat result:
06-01 13:41:00.386: D/AndroidRuntime(1746): Shutting down VM
06-01 13:41:00.386: W/dalvikvm(1746): threadid=1: thread exiting with
uncaught exception (group=0x40015560)
06-01 13:41:00.398: E/AndroidRuntime(1746): FATAL EXCEPTION: main
06-01 13:41:00.398: E/AndroidRuntime(1746):
java.lang.IllegalStateException: Nesting problem: [NONEMPTY_DOCUMENT,
NONEMPTY_OBJECT, NONEMPTY_OBJECT, NONEMPTY_ARRAY, NONEMPTY_OBJECT,
NONEMPTY_OBJECT]
06-01 13:41:00.398: E/AndroidRuntime(1746): at
com.google.gson.stream.JsonWriter.beforeValue(JsonWriter.java:550)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
com.google.gson.stream.JsonWriter.open(JsonWriter.java:263)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
com.google.gson.stream.JsonWriter.beginObject(JsonWriter.java:246)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
com.sensorplatforms.json.JsonSensorLogger.PrintToStreamSingle(JsonSensorLogger.java:
635)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
com.sensorplatforms.ActionLogger.TestingService
$2.run(TestingService.java:384)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
android.os.Handler.handleCallback(Handler.java:587)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
android.os.Handler.dispatchMessage(Handler.java:92)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
android.os.Looper.loop(Looper.java:130)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
android.app.ActivityThread.main(ActivityThread.java:3683)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
java.lang.reflect.Method.invokeNative(Native Method)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
java.lang.reflect.Method.invoke(Method.java:507)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
com.android.internal.os.ZygoteInit
$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-01 13:41:00.398: E/AndroidRuntime(1746): at
dalvik.system.NativeStart.main(Native Method)