0

I have following JSON,

 {
    "child1-name" : {
        "child1child1-name" : "child1child1-value",
        "child1child2-name" : "child1child2-value"
    },
    "child2" : {
        "child2child1-name" : "child2child1-value"
    },
    "child3-name" : "child3-value"
}

Now here As it is an HOCON config object i want to iterate over this and retrieve each element recursively. I want to iterate over each config object and based on its type (ArrayNode,ObjectNode,String etc) i will set appropriate value (comments) and return that node by setting final config object.

I want to achieve following Pusedo Code :

  while(iterator.hasNext()) {
        Entry<String, ConfigValue> fld = iterator.next();
        // Now here access each object and value which will be of type of Configvalue
        //If(ConfigValueType.OBJECT)
             //set the required value 
       //else If(ConfigValueType.STRING)
              //set the required value 

    }
   //Once iteration done, set the new values in config and return final config object.

Following sample code i am thinking of,

String jsonString = _mapper.writeValueAsString(jsonRoot); // jsonRoot is valid jsonNode object
       Config config = ConfigFactory.parseString(jsonString);

//Now,  I want to set comments by iterating the config object.
//I have gone through the following API’s,

ConfigObject co = config.root();
        Set<Entry<String, ConfigValue>> configNode2 =co.entrySet();
Iterator<Entry<String, ConfigValue>> itr =  configNode2.iterator();
        while(itr.hasNext()){
              Entry<String, ConfigValue> fld = itr.next();
               // **how to set comments and return the config object** .
       }

The main reason to convert JSON to HOCON for me to set comments. Now in above code, I am not sure how to set the comments.

Sohan
  • 6,252
  • 5
  • 35
  • 56
  • The example you post is not a JSON. In fact, it looks more like a HOCON file. What is the problem here? What is the output of your code? What were you expecting? – marcospereira Jan 25 '16 at 18:14
  • Now the example is edited as a valid JSON object, and the confusion shifts to why you would try to parse a JSON object using the HOCON infrastructure. It is still not clear what you are trying to do here. – gpgekko Jan 27 '16 at 15:18
  • I have updated the question more specifically. – Sohan Jan 28 '16 at 05:56

1 Answers1

0

After reading and searching for typesafe api which was complicated for me i am able to resolve this issue by doing this,

List<String> comments = Arrays.asList("A new","comment");
String jsonString = "{ \"a\" : 42 , \"b\" : 18 }";
Config config = ConfigFactory.parseString(jsonString);
ConfigObject co = config.root();
ConfigObject co2 = co;
Set<Entry<String, ConfigValue>> configNode2 = co.entrySet();
Iterator<Entry<String, ConfigValue>> itr = configNode2.iterator();
while(itr.hasNext()){
  Entry<String, ConfigValue> fld = itr.next();
  String key = fld.getKey();
  ConfigValue value = fld.getValue();
  ConfigOrigin oldOrigin = value.origin();
  ConfigOrigin newOrigin = oldOrigin.withComments(comments);
  ConfigValue newValue = value.withOrigin(newOrigin); 
  // fld.setValue(newValue); // This doesn't work: it's immutable
  co2 = co2.withValue(key,newValue);
}
config = co2.toConfig();
System.out.println(config.root().render(ConfigRenderOptions.concise().
  setComments(true).setFormatted(true)));

I hope this helps someone in future!

Sohan
  • 6,252
  • 5
  • 35
  • 56