toJson converts map's Integer key into a String
See original GitHub issueWhat steps will reproduce the problem?
1. Construct a Map with Integer key
2. Put key/value pair into the map, making sure the key is an Integer
3. Write JSON stream using toJson to convert the map to JSON
4. Check the output file
What is the expected output? What do you see instead?
Expect to see the key represented as an Integer with no quotes. The key is
represented as a String with double quotes.
What version of the product are you using? On what operating system?
Goobuntu. The latest in third_party/java.
Please provide any additional information below.
Might have the same root cause as issue 524, but I'm seeing the problem in
toJson instead of fromJson.
Here's some sample code to demonstrate the bug:
private void writeJsonStreamTest(OutputStream out) throws IOException {
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
writer.setIndent(" ");
writer.beginArray();
Map<Integer, String> data = new HashMap<Integer, String>();
data.put(new Integer(4), "Hello");
new Gson().toJson(data, HashMap.class, writer);
writer.endArray();
writer.close();
}
Here's the actual output from calling that method:
[
{
"4": "Hello"
}
]
Expected this instead:
[
{
4: "Hello"
}
]
Original issue reported on code.google.com by vivienne...@google.com
on 19 Aug 2014 at 5:29
Issue Analytics
- State:
- Created 9 years ago
- Comments:5
Top Results From Across the Web
JSON convert Map with Integer keys - java - Stack Overflow
JSON convert Map with Integer keys · JSON keys are strings, by definition. · JSON keys are strings Some JSON parsers will accept...
Read more >Converting Map with number as key to JSON - B4X
After saving a map that has numbers for keys and values to a JSON string, the keys seem to be turned into strings,...
Read more >JSON.stringify() - JavaScript - MDN Web Docs
JSON.stringify() converts a value to JSON notation representing it: Boolean , Number , String , and BigInt (obtainable via Object() ) objects ...
Read more >JSON conversion in Flutter and Dart | by Suragch - Medium
We will use jsonEncode() , but it only accepts a number, boolean, string, null, list or a map with string keys. So we...
Read more >How to Convert Map to JSON to HashMap in Java?
Here we will be using a method named 'writeValueAsString()' in the code and that can be used to serialize any Java value as...
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
For reference, RFC4627 makes clear:
(emphasis mine)
Original comment by
androida...@gmail.com
on 31 Dec 2014 at 4:15