Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- {
- "inner1": {
- "inner2": {
- "aStringProp": "aStringValue",
- "inner3": {
- "i2": 100,
- "i1": 1
- }
- },
- "aBoolProp": true
- },
- "size": 1,
- "count": 1000000
- }
- 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);
- 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);
- Properties jckProp = JsonSerializer.mapper.readValue(json, Properties.class);
- 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"));
- The Properties class represents a persistent set of properties.
- The Properties can be saved to a stream or loaded from a stream.
- Each key and its corresponding value in the property list is a string.
- ...
- If the store or save method is called on a "compromised" Properties
- object that contains a non-String key or value, the call will fail.
- topLevelProp.put("ARRAY", new String[]{ "foo", "bar" });
- topLevelProp.put("RAW_LIST", asList("foo", "bar"));
- private static final Gson typeAwareGson = new GsonBuilder()
- .registerTypeAdapter(Properties.class, getTypeAwarePropertiesSerializer())
- .registerTypeAdapter(Properties.class, getTypeAwarePropertiesDeserializer())
- .create();
- public class Configuration extends Properties {
- public void load(JsonElement json) {
- addJson("", json);
- return;
- }
- public void addJson(String root, JsonElement json) {
- // recursion for objects
- if (json instanceof JsonObject) {
- if (!root.equals("")) root += ".";
- final JsonObject jsonObject = json.getAsJsonObject();
- for ( final Entry<String, JsonElement> e : jsonObject.entrySet() ) {
- addJson(root + e.getKey(), e.getValue());
- }
- return;
- }
- // recursion for arrays
- if (json instanceof JsonArray) {
- final JsonArray jsonArray = json.getAsJsonArray();
- if (!root.equals("")) root += ".";
- int count = 0;
- for(final JsonElement e : jsonArray) {
- addJson(root+count, e);
- count++;
- }
- return;
- }
- // leaves: add property
- this.put(root, json.toString());
- }
- }
Add Comment
Please, Sign In to add comment