private void generateFieldsAndMethods(
      final Writer writer,
      final JsonObject object,
      final String prefix,
      final Collection<String> imports)
      throws IOException {
    final Map<String, JsonObject> nestedTypes = new TreeMap<String, JsonObject>();
    {
      final Iterator<Map.Entry<String, JsonValue>> iterator = object.entrySet().iterator();
      while (iterator.hasNext()) {
        final Map.Entry<String, JsonValue> entry = iterator.next();
        final String key = entry.getKey();
        final String fieldName = toJavaFieldName(key);
        switch (entry.getValue().getValueType()) {
          case ARRAY:
            imports.add("java.util.List");
            handleArray(writer, prefix, nestedTypes, entry.getValue(), key, fieldName, 1, imports);
            break;
          case OBJECT:
            final String type = toJavaName(fieldName);
            nestedTypes.put(type, JsonObject.class.cast(entry.getValue()));
            fieldGetSetMethods(writer, key, fieldName, type, prefix, 0, imports);
            break;
          case TRUE:
          case FALSE:
            fieldGetSetMethods(writer, key, fieldName, "Boolean", prefix, 0, imports);
            break;
          case NUMBER:
            fieldGetSetMethods(writer, key, fieldName, "Double", prefix, 0, imports);
            break;
          case STRING:
            fieldGetSetMethods(writer, key, fieldName, "String", prefix, 0, imports);
            break;
          case NULL:
          default:
            throw new UnsupportedOperationException("Unsupported " + entry.getValue() + ".");
        }
        if (iterator.hasNext()) {
          writer.write("\n");
        }
      }
    }

    if (!object.isEmpty() && !nestedTypes.isEmpty()) {
      writer.write("\n");
    }

    final Iterator<Map.Entry<String, JsonObject>> entries = nestedTypes.entrySet().iterator();
    while (entries.hasNext()) {
      final Map.Entry<String, JsonObject> entry = entries.next();
      writer.write(prefix + "public static class " + entry.getKey() + " {\n");
      generateFieldsAndMethods(writer, entry.getValue(), "    " + prefix, imports);
      writer.write(prefix + "}\n");
      if (entries.hasNext()) {
        writer.write("\n");
      }
    }
  }
Example #2
0
  @Test
  @InSequence(2)
  public void testEchoObject() throws MalformedURLException {
    JsonObject jsonObject =
        Json.createObjectBuilder().add("apple", "red").add("banana", "yellow").build();

    WebTarget targetObject =
        client.target(URI.create(new URL(deploymentURL, "resources/object").toExternalForm()));
    JsonObject json =
        targetObject
            .request()
            .post(Entity.entity(jsonObject, MediaType.APPLICATION_JSON), JsonObject.class);
    assertNotNull(json);
    assertFalse(json.isEmpty());
    assertTrue(json.containsKey("apple"));
    assertEquals("red", json.getString("apple"));
    assertTrue(json.containsKey("banana"));
    assertEquals("yellow", json.getString("banana"));
  }