I've inherited some code that saves our application state as JSON using Gson, and then reads it using fromJson.
Gson gson = createGson();
gson.fromJson(objString, myClass);
One of the fields being saved is a Location. Unfortunately, very occasionally the parsing of that saved data fails because my saved Location includes an mClassLoader in its mExtras, and the Gson library fails to create the ClassLoader with this error:
RuntimeException: Failed to invoke protected java.lang.ClassLoader() with no args
Does anybody know why a ClassLoader is being included in the extras for my Location, and whether it should be ending up in the JSON representation?
I'm assuming I can fix this by just saving the key fields from the Location object individually (e.g. longitude, latitude, altitude, time, accuracy), but it would be nice to save out the Location object if possible.
I saw there is an ExclusionStrategy object I could use to exclude fields, but I wasn't sure if I could/should use that to exclude the the extras from inside my Location...
FYI, here's the JSON data for my Location object (with the longitude and latitude changed to hide me):
{
<snip>
"lastKnownLocation": {
"mResults": [
0,
0
],
"mProvider": "gps",
"mExtras": {
"mParcelledData": {
"mOwnObject": 1,
"mObject": 5525040
},
"mClassLoader": {
"packages": {}
},
"mMap": {},
"mHasFds": false,
"mFdsKnown": true,
"mAllowFds": true
},
"mDistance": 0,
"mTime": 1354658984849,
"mAltitude": 5.199999809265137,
"mLongitude": -122.4376,
"mLon2": 0,
"mLon1": 0,
"mLatitude": 37.7577,
"mLat1": 0,
"mLat2": 0,
"mInitialBearing": 0,
"mHasSpeed": true,
"mHasBearing": false,
"mHasAltitude": true,
"mHasAccuracy": true,
"mAccuracy": 16,
"mSpeed": 0,
"mBearing": 0
},
<snip>
}
Here's an example what the mExtras contains when the code doesn't crash:
"mExtras": {
"mParcelledData": {
"mOwnsNativeParcelObject": true,
"mNativePtr": 1544474480
},
"mHasFds": false,
"mFdsKnown": true,
"mAllowFds": true
}