title: How to serialize java.util.Properties to and from JSON file?
body: <p>I have variable of type <code>java.util.Properties</code>. I am trying to write it to a JSON file, and as well as read from that file.</p>
  
  <p>The <code>Properties</code> variable looks something like below:</p>
  
  <pre><code>Properties inner3 = new Properties();
  inner3.put("i1", 1);
  inner3.put("i2", 100);
  
  Properties inner2 = new Properties();
  inner2.put("aStringProp", "aStringValue");
  inner2.put("inner3", inner3);
  
  Properties inner1 = new Properties();
  inner1.put("aBoolProp", true);
  inner1.put("inner2", inner2);
  
  Properties topLevelProp = new Properties();
  topLevelProp.put("count", 1000000);
  topLevelProp.put("size", 1);
  topLevelProp.put("inner1", inner1);
  </code></pre>
  
  <p>Naturally, when I serialize the <code>topLevelProp</code> to JSON I expect the result to be as below.</p>
  
  <pre><code>{
    "inner1": {
      "inner2": {
        "aStringProp": "aStringValue",
        "inner3": {
          "i2": 100,
          "i1": 1
        }
      },
      "aBoolProp": true
    },
    "size": 1,
    "count": 1000000
  }
  </code></pre>
  
  <p>The above JSON result can be produced by using <a href="https://github.com/google/gson" rel="nofollow noreferrer">Gson</a> in a pretty straight forward way, but when it is fed the same JSON string to desrialize, it fails.</p>
  
  <pre><code>Gson gson = new GsonBuilder().create();
  String json = gson.toJson(topLevelProp); //{"inner1":{"inner2":{"aStringProp":"aStringValue","inner3":{"i2":100,"i1":1}},"aBoolProp":true},"size":1,"count":1000000}
  
  //following line throws error: Expected a string but was BEGIN_OBJECT at line 1 column 12 path $.
  Properties propObj = gson.fromJson(json, Properties.class); 
  </code></pre>
  
  <p>Tried with Jackson as well:</p>
  
  <pre><code>ObjectMapper mapper = new ObjectMapper();
  mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
  mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
  mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
  File file = new File("configs/config1.json");
  mapper.writeValue(file, topLevelProp);
  </code></pre>
  
  <p>The last line throws error:</p>
  
  <blockquote>
    <p>com.fasterxml.jackson.databind.JsonMappingException: java.util.Properties cannot be cast to java.lang.String (through reference chain: java.util.Properties["inner1"])</p>
  </blockquote>
  
  <p>Tried to desrialize from the string as follows and it failed with the following error:</p>
  
  <pre><code>Properties jckProp = JsonSerializer.mapper.readValue(json, Properties.class);
  </code></pre>
  
  <blockquote>
    <p>Can not deserialize instance of java.lang.String out of START_OBJECT token
     at [Source: {"inner1":{"inner2":{"aStringProp":"aStringValue","inner3":{"i2":100,"i1":1}},"aBoolProp":true},"size":1,"count":1000000}; line: 1, column: 11] (through reference chain: java.util.Properties["inner1"])</p>
  </blockquote>
  
  <p>How this can be handled? </p>
  
  <p><strong>Update:</strong> Following the <a href="https://stackoverflow.com/questions/41730115/how-to-serialize-java-util-properties-to-and-from-json-file#comment70654979_41730115">idea of cricket_007</a>, found <code>com.fasterxml.jackson.databind.node.ObjectNode</code>, can be used as follows:</p>
  
  <pre><code>ObjectNode jckProp = JsonSerializer.mapper.readValue(json, ObjectNode.class);
  System.out.println(jckProp.get("size").asInt());
  System.out.println("jckProp: " + jckProp);
  System.out.println("jckProp.inner: " + jckProp.get("inner1"));
  </code></pre>
  
  <p>I think this can be the way forward for me, as I mostly have to read from JSON file.</p>