question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

toJson converts map's Integer key into a String

See original GitHub issue
What 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:closed
  • Created 9 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
abatkincommented, Dec 4, 2017

For reference, RFC4627 makes clear:

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

(emphasis mine)

0reactions
GoogleCodeExportercommented, Mar 19, 2015
"new Gson().toJson(data, new TypeToken<Map<Integer, String>>() {}.getType())"
The above method is not work.

Original comment by androida...@gmail.com on 31 Dec 2014 at 4:15

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found