Link.FYI

Pastebin

Create New My Pastes

Code (Java) pasted on 2019-01-30, 19:49 Raw Source

  1. import java.util.*;
  2. import java.util.function.Consumer;
  3.  
  4. public class JsonRecursive {
  5.  
  6.     public void main() {
  7.         JsObject tree = buildTree();
  8.  
  9.         // You are given an object tree that was generated by a JSON parser.
  10.         // It contains nested JsValue objects. Each one is of one of these subtypes:
  11.         //
  12.         //   - JsConstant (just a leaf node with a value, accessible via getValue())
  13.         //   - JsArray (contains elements, accessible via forEach(Consumer))
  14.         //   - JsObject (contains fields, accessible via getFieldNames() and getField(name))
  15.         //
  16.         // Task 1:
  17.         // For debugging purposes we want to quickly print the entire tree to the
  18.         // console. Write a method that prints entire object tree in a human-readable
  19.         // format.
  20.         //
  21.         // Task 2:
  22.         // By printing the parser result to the console, you just found out that
  23.         // the field names are all in snake_case, while you definitely need them
  24.         // in camelCase. Write a method that modifies the tree and converts all
  25.         // field names. Use your previously written method to check the result.
  26.     }
  27.  
  28.     // ------------------------------------------------------------------------
  29.     // The following code may not be edited!
  30.     // ------------------------------------------------------------------------
  31.  
  32.     private JsObject buildTree() {
  33.         JsObject object = new JsObject();
  34.         object.addField("some_silly_field", new JsConstant("Foo!"));
  35.         object.addField("yet_another_pointless_field", new JsConstant("Bar!"));
  36.  
  37.         JsObject subSubObject = new JsObject();
  38.         subSubObject.addField("field1", new JsConstant(42));
  39.         subSubObject.addField("field_4_you", new JsConstant(23));
  40.  
  41.         JsObject subObject = new JsObject();
  42.         subObject.addField("nothing", new JsConstant(false));
  43.         subObject.addField("deeper_we_go", subSubObject);
  44.  
  45.         JsArray array = new JsArray();
  46.         array.addElement(new JsConstant("This is not an object."));
  47.         array.addElement(subObject);
  48.  
  49.         JsObject tree = new JsObject();
  50.         tree.addField("schema_version", new JsConstant("1.1.0"));
  51.         tree.addField("some_object", object);
  52.         tree.addField("some_array", array);
  53.  
  54.         return tree;
  55.     }
  56.  
  57.     abstract class JsValue {
  58.         public boolean isObject() { return false; }
  59.         public boolean isArray() { return false; }
  60.         public boolean isConstant() { return false; }
  61.     }
  62.  
  63.     class JsObject extends JsValue {
  64.         private Map<String, JsValue> elements = new HashMap<>();
  65.  
  66.         @Override
  67.         public boolean isObject() { return true; }
  68.  
  69.         public JsObject addField(String name, JsValue value) {
  70.             this.elements.put(name, value);
  71.             return this;
  72.         }
  73.  
  74.         public JsObject deleteField(String name) {
  75.             this.elements.remove(name);
  76.             return this;
  77.         }
  78.  
  79.         public Set<String> getFieldNames() {
  80.             return new HashSet<String>(this.elements.keySet());
  81.         }
  82.  
  83.         public JsValue getField(String name) {
  84.             return this.elements.get(name);
  85.         }
  86.  
  87.     }
  88.  
  89.     class JsArray extends JsValue {
  90.         private List<JsValue> elements = new ArrayList<>();
  91.  
  92.         @Override
  93.         public boolean isArray() { return true; }
  94.  
  95.         public JsArray addElement(JsValue element) {
  96.             this.elements.add(element);
  97.             return this;
  98.         }
  99.  
  100.         public ListIterator<JsValue> getIterator() {
  101.             return this.elements.listIterator();
  102.         }
  103.  
  104.         public void forEach(Consumer<JsValue> action) {
  105.             this.elements.forEach(action);
  106.         }
  107.     }
  108.  
  109.     class JsConstant extends JsValue {
  110.         private Object value;
  111.  
  112.         public JsConstant(Object value) {
  113.             this.value = value;
  114.         }
  115.  
  116.         @Override
  117.         public boolean isConstant() { return true; }
  118.  
  119.         public Object getValue() {
  120.             return this.value;
  121.         }
  122.     }
  123.  
  124. }