JSON convert Map with Integer keys


I have a small sample of test code where I try to convert a Map into a JSON string and back. While parsing from the JSON string, the resulting map contains the String key "1" instead of the Integer key "1", thus making the test fail. The same happens with POJOs used as the key to this map. Is this behaviour expected or have I ommited some configuration for the JSON converters?

public class SampleConverterTest { @Test public void testIntegerKey() { // Register an Integer converter JSON.registerConvertor(Integer.class, new JSONPojoConvertor(Integer.class)); Map<Integer, String> map = new HashMap<Integer, String>(); map.put(1, "sample"); // Convert to JSON String msg = JSON.toString(map); // Retrieve original map from JSON @SuppressWarnings("unchecked") Map<Integer, String> obj = (Map<Integer, String>) JSON.parse(msg); assertTrue(obj.containsKey(1)); } }

I am using jetty-util 7.6.10.v20130312


Like @HotLicks said, when you convert objects to JSON, the key part of the JSON map will be returned as a String. I don't believe there's any way to move around this behavior. I'd also steer clear of using integers as keys in your map, if the intended behavior is as a JSON map. Instead, I'd do something like:

map.put("identifier", 1); map.put("value", "sample");

It's a little bit more verbose, but it's also easier to see how that translates to JSON.


  • Delphi 10.1 Berlin - Java2OP: class or interface expected
  • JsDoc to PDF renderer
  • How to get ReadJson to return “default” behavior - as if CanConvert returned false
  • java rest client: jax-rs & automatic type mapping
  • Generating ActionScript value objects from an xsd schema
  • How to package a jar and all dependencies within a new jar with maven
  • How do I configure Maven Cargo to use an embedded Tomcat server?
  • Symfony2 redirect to https route fails (uses wrong port)
  • How to force Composer to download a local package?
  • Hibernate in Glassfish - Ejb3Configuration NoClassDefFoundError
  • Cursor in wrong place in contenteditable
  • How to configure Cygnus in relation to Orion and Cosmos
  • MAVEN : Run Multiple Maven Project using Maven Test
  • Loading fixtures in sails tests
  • Unzip archive in .Net CORE 1.0
  • Add reference to ASP.NET 5 Class Library from Framework 4.5 Class Library Project
  • Spring integration inbound-gateway Fire an event when queue is empty
  • VSCode change debug shell to bash on windows
  • C#: Import/Export Settings into/from a File
  • How to retrieve information from antrun back to maven?
  • Cloud Code function running twice
  • How can I replace the server in Web Component Tester
  • Is there some graphical way to create my own configuration file on SonarLint?
  • Spring Cloud Microservice Architecture Confusion
  • PHP buffered output depending on server setting?
  • Functions in global context
  • How can I send an e-mail from a vbs script
  • Is possible to count alias result on mysql
  • Check if a string to interpolate provides expected placeholders
  • Akka Routing: Reply's send to router ends up as dead letters
  • Is there a mandatory requirement to switch app.yaml?
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • Java static initializers and reflection
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • How can I use threading to 'tick' a timer to be accessed by other threads?