Esempio n. 1
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;
  }
  public static void main(String[] args) {

    StringWriter s = new StringWriter();
    JsonWriter writer = Json.createWriter(s);
    writer.writeObject(buildJSon());
    System.out.println(s);
  }
Esempio n. 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;
  }
  /**
   * Creates a JSON string with key as 'message' and value as 'username: message'
   *
   * @param username the user who sent message
   * @param msg the message sent by user
   * @return String JSON representation of a single message
   */
  private String buildJsonData(String username, String msg) {
    JsonObject jsonObject =
        Json.createObjectBuilder().add("message", username + ": " + msg).build();
    StringWriter sw = new StringWriter();
    try (JsonWriter jw = Json.createWriter(sw)) {
      jw.write(jsonObject);
    }

    return sw.toString();
  }
Esempio n. 5
0
 /**
  * Print JSON.
  *
  * @param src Source
  * @return JSON
  * @throws IOException If fails
  */
 private static byte[] print(final RsJSON.Source src) throws IOException {
   final ByteArrayOutputStream baos = new ByteArrayOutputStream();
   final JsonWriter writer = Json.createWriter(baos);
   try {
     writer.write(src.toJSON());
   } finally {
     writer.close();
   }
   return baos.toByteArray();
 }
Esempio n. 6
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);
    }
  }
 private static void createJsonFile() {
   JsonObject model =
       Json.createObjectBuilder()
           .add("firstName", "Martin")
           .add(
               "phoneNumbers",
               Json.createArrayBuilder()
                   .add(Json.createObjectBuilder().add("mobile", "1234 56789"))
                   .add(Json.createObjectBuilder().add("home", "2345 67890")))
           .build();
   try (JsonWriter jsonWriter =
       Json.createWriter(
           new FileWriter(
               Paths.get(System.getProperty("user.dir"), "target/myData.json").toString()))) {
     jsonWriter.write(model);
   } catch (IOException e) {
     LOGGER.severe("Failed to create file: " + e.getMessage());
   }
 }
Esempio n. 8
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;
  }
Esempio n. 9
0
 @Override
 public JsonResource setContents(JsonStructure structure) {
   if (structure == null) throw new IllegalArgumentException("JsonStructure must not be null.");
   try {
     if (!exists()) {
       getParent().mkdirs();
       if (!createNewFile()) {
         throw new IOException("Failed to create file: " + getUnderlyingResourceObject());
       }
     }
     JsonWriterFactory writerFactory =
         Json.createWriterFactory(Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));
     try (OutputStream out =
             getFileOperations().createOutputStream(getUnderlyingResourceObject());
         JsonWriter writer = writerFactory.createWriter(out)) {
       writer.write(structure);
     }
   } catch (IOException e) {
     throw new ResourceException("Error while setting the Json contents", e);
   }
   return this;
 }