public static void main(String[] args) {

    StringWriter s = new StringWriter();
    JsonWriter writer = Json.createWriter(s);
    writer.writeObject(buildJSon());
    System.out.println(s);
  }
Example #2
0
  public <T extends AbstractDbEntity> String buildJsonStringByTypeAndId(long Id, Class<T> type) {
    String result = "";

    T DBobject = objectsController.select(type, Id);
    if (DBobject == null) {
      return result;
    }
    // may return null
    JsonObjectBuilder transJOB;

    try {
      transJOB = JsonObjectFiller.getJsonObjectBuilderFromClassInstance(DBobject);
    } catch (IllegalArgumentException | IllegalAccessException e) {
      return result;
    }

    JsonObject JObject = transJOB.build();

    Map<String, Boolean> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory factory = Json.createWriterFactory(config);

    // http://blog.eisele.net/2013/02/test-driving-java-api-for-processing.html
    StringWriter sw = new StringWriter();
    try (JsonWriter jw = factory.createWriter(sw)) {
      jw.writeObject(JObject);
    }

    result = sw.toString();

    return result;
  }
Example #3
0
  public String toJSON() {
    JsonArrayBuilder nodearray = Json.createArrayBuilder();
    if (nodeTemplatelist == null) logger.error("nodeTemplatelist is empty");
    for (NodeTemplateCount process : nodeTemplatelist) {

      nodearray.add(process.getCount());
    }
    JsonObject model =
        Json.createObjectBuilder()
            .add("name", this.name)
            .add("plugin_name", this.pluginName)
            .add("hadoop_version", this.hadoopVersion)
            .add("node_processes", nodearray)
            .build();

    StringWriter stWriter = new StringWriter();
    try (JsonWriter jsonWriter = Json.createWriter(stWriter)) {
      jsonWriter.writeObject(model);
    }

    String jsonData = stWriter.toString();
    System.out.println(jsonData);

    return jsonData;
  }
Example #4
0
  @Override
  public void execute() throws BuildException {
    if (index == null) {
      throw new BuildException("Required attributes: file");
    }

    try {
      JsonObjectBuilder jsonRoot = Json.createObjectBuilder();

      JsonArrayBuilder jsonKeyrings = Json.createArrayBuilder();
      getKeyRings().forEach(jsonKeyrings::add);
      jsonRoot.add("keyrings", jsonKeyrings);

      JsonArrayBuilder jsonPackages = Json.createArrayBuilder();
      getPackages()
          .forEach(
              (p) -> {
                JsonObjectBuilder jsonPackage = Json.createObjectBuilder();
                p.forEach(
                    (k, v) -> {
                      if (v instanceof Boolean) {
                        jsonPackage.add(k, (Boolean) v); // Boolean
                      } else if (v instanceof Number) {
                        jsonPackage.add(k, ((Number) v).longValue()); // Integer
                      } else if (v instanceof String[]) {
                        JsonArrayBuilder array = Json.createArrayBuilder(); // String Array
                        for (String s : (String[]) v) {
                          array.add(s);
                        }
                        jsonPackage.add(k, array);
                      } else if (v == null) {
                        jsonPackage.addNull(k); // null
                      } else {
                        jsonPackage.add(k, v.toString()); // String
                      }
                    });
                jsonPackages.add(jsonPackage);
              });
      jsonRoot.add("packages", jsonPackages);

      log("Write Package Source: " + index);
      StringWriter json = new StringWriter();
      try (JsonWriter writer =
          Json.createWriterFactory(singletonMap(JsonGenerator.PRETTY_PRINTING, true))
              .createWriter(json)) {
        writer.writeObject(jsonRoot.build());
      }
      Files.write(index.toPath(), json.toString().trim().getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
      throw new BuildException(e);
    }
  }
Example #5
0
  public String JSonObjectBuilderToString(JsonObjectBuilder transJOB) {
    String returnString = "";

    JsonObject JObject = transJOB.build();

    Map<String, Boolean> config = new HashMap<>();
    config.put(JsonGenerator.PRETTY_PRINTING, true);
    JsonWriterFactory factory = Json.createWriterFactory(config);

    // http://blog.eisele.net/2013/02/test-driving-java-api-for-processing.html
    StringWriter sw = new StringWriter();
    try (JsonWriter jw = factory.createWriter(sw)) {
      jw.writeObject(JObject);
    }

    returnString = sw.toString();

    return returnString;
  }