Sam Su
Sam Su

Reputation: 6832

Which Java/JSON library supports converting a JSON file that contains comments to a Java class?

My JSON file's content is :

{
"MJ" : "Michael Jordan",
"KB" : "Kobe Bryant",
"KG" : "Kevin Garnet"
}

Now it is easy to convert this file (or string) to a Java class (e.g: java.util.HashMap) if I use Gson or Jackson or JsonSimple or another Java/JSON third-party library. E.g., I can use Gson like this:

//this returns the string above
String jsonString = TestReader.getStrFromJSonFile();
Gson gson = new Gson();
Type type = new TypeToken<HashMap<String, String>>() {}.getType();
HashMap<String, String> map = gson.fromJson(jsonString, type);

But,if my JSON file contains comments, most of the third-party libraries will not work. e.g.:

  {   
       //Bulls   
       "MJ" : "Michael Jordan",   
       /*Lakers*/  
       "KB" : "Kobe Bryant",    
       //Boston Celtics    
       "KG" : "Kevin Garnet"    
   }

Now I'm even confused after googling and stackoverflowing a lot. I have two questions:

  1. Can a JSON file really contain any comments? I think it can because I see a lot of this in JSON files everyday. But >>> (links)

  2. If it can, how can I solve my problem?

Upvotes: 3

Views: 1797

Answers (1)

cmbaxter
cmbaxter

Reputation: 35463

Jackson supports comments if you enable that feature on the parser you use:

https://fasterxml.github.io/jackson-core/javadoc/2.10/com/fasterxml/jackson/core/JsonParser.Feature.html

Upvotes: 4

Related Questions