Code (Java) pasted on 2019-01-30, 19:49 Raw Source
- import java.util.*;
- import java.util.function.Consumer;
- public class JsonRecursive {
- public void main() {
- JsObject tree = buildTree();
- // You are given an object tree that was generated by a JSON parser.
- // It contains nested JsValue objects. Each one is of one of these subtypes:
- //
- // - JsConstant (just a leaf node with a value, accessible via getValue())
- // - JsArray (contains elements, accessible via forEach(Consumer))
- // - JsObject (contains fields, accessible via getFieldNames() and getField(name))
- //
- // Task 1:
- // For debugging purposes we want to quickly print the entire tree to the
- // console. Write a method that prints entire object tree in a human-readable
- // format.
- //
- // Task 2:
- // By printing the parser result to the console, you just found out that
- // the field names are all in snake_case, while you definitely need them
- // in camelCase. Write a method that modifies the tree and converts all
- // field names. Use your previously written method to check the result.
- }
- // ------------------------------------------------------------------------
- // The following code may not be edited!
- // ------------------------------------------------------------------------
- private JsObject buildTree() {
- JsObject object = new JsObject();
- object.addField("some_silly_field", new JsConstant("Foo!"));
- object.addField("yet_another_pointless_field", new JsConstant("Bar!"));
- JsObject subSubObject = new JsObject();
- subSubObject.addField("field1", new JsConstant(42));
- subSubObject.addField("field_4_you", new JsConstant(23));
- JsObject subObject = new JsObject();
- subObject.addField("nothing", new JsConstant(false));
- subObject.addField("deeper_we_go", subSubObject);
- JsArray array = new JsArray();
- array.addElement(new JsConstant("This is not an object."));
- array.addElement(subObject);
- JsObject tree = new JsObject();
- tree.addField("schema_version", new JsConstant("1.1.0"));
- tree.addField("some_object", object);
- tree.addField("some_array", array);
- return tree;
- }
- abstract class JsValue {
- public boolean isObject() { return false; }
- public boolean isArray() { return false; }
- public boolean isConstant() { return false; }
- }
- class JsObject extends JsValue {
- @Override
- public boolean isObject() { return true; }
- this.elements.put(name, value);
- return this;
- }
- this.elements.remove(name);
- return this;
- }
- public Set<String> getFieldNames() {
- return new HashSet<String>(this.elements.keySet());
- }
- return this.elements.get(name);
- }
- }
- class JsArray extends JsValue {
- private List<JsValue> elements = new ArrayList<>();
- @Override
- public boolean isArray() { return true; }
- public JsArray addElement(JsValue element) {
- this.elements.add(element);
- return this;
- }
- public ListIterator<JsValue> getIterator() {
- return this.elements.listIterator();
- }
- public void forEach(Consumer<JsValue> action) {
- this.elements.forEach(action);
- }
- }
- class JsConstant extends JsValue {
- this.value = value;
- }
- @Override
- public boolean isConstant() { return true; }
- return this.value;
- }
- }
- }