Gson fails to convert ContentValues with Android 10
See original GitHub issueThe internal implementation of ContentValues changed from using a Hashmap to using an ArrayMap in Android 10. I’ve included some sample code below to demonstrate how it fails now.
ContentValues values = new ContentValues();
values.put("Key 1", "Key 1 value");
values.put("Key 2", "Key 2 value");
values.put("Key 3", "Key 3 value");
String json = new Gson().toJson(values);
Log.d("XXX", String.format("Content values json: '%s'", json));
On Android 10 the output looks like: Output: XXX: Content values json: ‘{}’
On Android 9 the output looks like: Output: XXX: Content values json: {“mValues”:{“Key 1”:“Key 1 value”,“Key 2”:“Key 2 value”,“Key 3”:“Key 3 value”}}
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
convert JSON to contentvalues - android - Stack Overflow
I made a static class that will convert any object into contentvalues using reflection. I'm sure there's an equivalent ...
Read more >JSONObject - Android Developers
This two-step conversion is lossy for very large values. ... This fails with a JSONException if the requested name has no value or...
Read more >SQLite - Android - DevTut
It is called when the database file already exists, but its version is lower than the one specified in the current version of...
Read more >Università degli Studi di Parma - Unipr
This mechanism on Android platform is called “Shared Preferences” and ... Gson is a Java library that can be used to convert Java...
Read more >Università degli Studi di Parma - Piazza
This mechanism on Android platform is called “Shared Preferences” and ... Gson is a Java library that can be used to convert Java...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@tethridge
I’m not into the Android API, but I don’t really believe using Gson for this case is a good choice. (Except probably an attempt of nesting/packing some data as string values, however
Parcelable
looks like a (de)serialization tool already.) If you still need using Gson, you probably might fine-tune the example below (avoid intermediate maps, avoid the map type adapter and use readers/writers directly, etc):gives
Thanks for the feedback. I’ll close this issue. I resolved the issue in my app by implementing my own serialization code.