Guest User

Untitled

a guest
Jul 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. Properties inner3 = new Properties();
  2. inner3.put("i1", 1);
  3. inner3.put("i2", 100);
  4.  
  5. Properties inner2 = new Properties();
  6. inner2.put("aStringProp", "aStringValue");
  7. inner2.put("inner3", inner3);
  8.  
  9. Properties inner1 = new Properties();
  10. inner1.put("aBoolProp", true);
  11. inner1.put("inner2", inner2);
  12.  
  13. Properties topLevelProp = new Properties();
  14. topLevelProp.put("count", 1000000);
  15. topLevelProp.put("size", 1);
  16. topLevelProp.put("inner1", inner1);
  17.  
  18. {
  19. "inner1": {
  20. "inner2": {
  21. "aStringProp": "aStringValue",
  22. "inner3": {
  23. "i2": 100,
  24. "i1": 1
  25. }
  26. },
  27. "aBoolProp": true
  28. },
  29. "size": 1,
  30. "count": 1000000
  31. }
  32.  
  33. Gson gson = new GsonBuilder().create();
  34. String json = gson.toJson(topLevelProp); //{"inner1":{"inner2":{"aStringProp":"aStringValue","inner3":{"i2":100,"i1":1}},"aBoolProp":true},"size":1,"count":1000000}
  35.  
  36. //following line throws error: Expected a string but was BEGIN_OBJECT at line 1 column 12 path $.
  37. Properties propObj = gson.fromJson(json, Properties.class);
  38.  
  39. ObjectMapper mapper = new ObjectMapper();
  40. mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
  41. mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
  42. mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
  43. File file = new File("configs/config1.json");
  44. mapper.writeValue(file, topLevelProp);
  45.  
  46. Properties jckProp = JsonSerializer.mapper.readValue(json, Properties.class);
  47.  
  48. ObjectNode jckProp = JsonSerializer.mapper.readValue(json, ObjectNode.class);
  49. System.out.println(jckProp.get("size").asInt());
  50. System.out.println("jckProp: " + jckProp);
  51. System.out.println("jckProp.inner: " + jckProp.get("inner1"));
  52.  
  53. The Properties class represents a persistent set of properties.
  54. The Properties can be saved to a stream or loaded from a stream.
  55. Each key and its corresponding value in the property list is a string.
  56. ...
  57. If the store or save method is called on a "compromised" Properties
  58. object that contains a non-String key or value, the call will fail.
  59.  
  60. topLevelProp.put("ARRAY", new String[]{ "foo", "bar" });
  61. topLevelProp.put("RAW_LIST", asList("foo", "bar"));
  62.  
  63. private static final Gson typeAwareGson = new GsonBuilder()
  64. .registerTypeAdapter(Properties.class, getTypeAwarePropertiesSerializer())
  65. .registerTypeAdapter(Properties.class, getTypeAwarePropertiesDeserializer())
  66. .create();
  67.  
  68. public class Configuration extends Properties {
  69. public void load(JsonElement json) {
  70. addJson("", json);
  71. return;
  72. }
  73.  
  74. public void addJson(String root, JsonElement json) {
  75.  
  76. // recursion for objects
  77. if (json instanceof JsonObject) {
  78. if (!root.equals("")) root += ".";
  79. final JsonObject jsonObject = json.getAsJsonObject();
  80. for ( final Entry<String, JsonElement> e : jsonObject.entrySet() ) {
  81. addJson(root + e.getKey(), e.getValue());
  82. }
  83. return;
  84. }
  85.  
  86. // recursion for arrays
  87. if (json instanceof JsonArray) {
  88. final JsonArray jsonArray = json.getAsJsonArray();
  89. if (!root.equals("")) root += ".";
  90. int count = 0;
  91. for(final JsonElement e : jsonArray) {
  92. addJson(root+count, e);
  93. count++;
  94. }
  95. return;
  96. }
  97.  
  98. // leaves: add property
  99. this.put(root, json.toString());
  100. }
  101. }
Add Comment
Please, Sign In to add comment