Java Code Examples for org.json.simple.JSONObject#writeJSONString()

The following examples show how to use org.json.simple.JSONObject#writeJSONString() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: netbeans   File: PackageJsonTest.java    License: Apache License 2.0 5 votes vote down vote up
private void writeFile(Map<String, Object> data) throws IOException {
    File file = getFile();
    try (Writer out = new FileWriter(file)) {
        JSONObject.writeJSONString(data, out);
    }
    assertTrue(file.isFile());
    refreshForFile(file);
}
 
Example 2
Source Project: netbeans   File: BowerJsonTest.java    License: Apache License 2.0 5 votes vote down vote up
private void writeFile(Map<String, Object> data) throws IOException {
    File file = getFile();
    try (Writer out = new FileWriter(file)) {
        JSONObject.writeJSONString(data, out);
    }
    assertTrue(file.isFile());
    refreshForFile(file);
}
 
Example 3
Source Project: netbeans   File: JsonFileTest.java    License: Apache License 2.0 5 votes vote down vote up
private void writeFile(File file, Map<String, Object> data) throws IOException {
    try (Writer out = new FileWriter(file)) {
        JSONObject.writeJSONString(data, out);
    }
    assertTrue(file.isFile());
    refreshForFile(file);
}
 
Example 4
Source Project: netbeans   File: InspectContainerAction.java    License: Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages({
    "# {0} - container id",
    "MSG_Inspecting=Inspecting container {0}"
})
@Override
public void run() {
    JsonFormattingWriter formattedJsonWriter = new JsonFormattingWriter(2);

    try {
        DockerAction facade = new DockerAction(container.getInstance());
        JSONObject rawDetails = facade.getRawDetails(container.getType(), container.getId());

        rawDetails.writeJSONString(formattedJsonWriter);

        InputOutput io = IOProvider.getDefault().getIO(Bundle.MSG_Inspecting(container.getShortId()), false);
        io.getOut().reset();
        io.getOut().println(formattedJsonWriter.toString());
        io.getOut().close();
        io.getErr().close();
        io.select();
    } catch (IOException | DockerException ex) {
        LOGGER.log(Level.INFO, null, ex);
        String msg = ex.getLocalizedMessage();
        NotifyDescriptor desc = new NotifyDescriptor.Message(msg, NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(desc);
    }
}
 
Example 5
Source Project: Indra   File: JSONUtil.java    License: MIT License 5 votes vote down vote up
public static void writeMapAsJson(Map<String, Object> map, File file) throws IOException {
    JSONObject metadataJson = toJSONObject(map);

    Writer writer = new FileWriter(file);
    metadataJson.writeJSONString(writer);
    writer.close();
}
 
Example 6
Source Project: hadoop   File: JSONMapProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Map map, Class<?> aClass, Type type, Annotation[] annotations,
                    MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap,
                    OutputStream outputStream) throws IOException, WebApplicationException {
  Writer writer = new OutputStreamWriter(outputStream,  Charsets.UTF_8);
  JSONObject.writeJSONString(map, writer);
  writer.write(ENTER);
  writer.flush();
}
 
Example 7
Source Project: big-c   File: JSONMapProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(Map map, Class<?> aClass, Type type, Annotation[] annotations,
                    MediaType mediaType, MultivaluedMap<String, Object> stringObjectMultivaluedMap,
                    OutputStream outputStream) throws IOException, WebApplicationException {
  Writer writer = new OutputStreamWriter(outputStream,  Charsets.UTF_8);
  JSONObject.writeJSONString(map, writer);
  writer.write(ENTER);
  writer.flush();
}
 
Example 8
@Test
@TestDir
@SuppressWarnings("unchecked")
public void service() throws Exception {
  String dir = TestDirHelper.getTestDir().getAbsolutePath();
  String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName()));
  Configuration conf = new Configuration(false);
  conf.set("server.services", services);
  Server server = new Server("server", dir, dir, dir, dir, conf);
  server.init();

  Instrumentation instrumentation = server.get(Instrumentation.class);
  assertNotNull(instrumentation);
  instrumentation.incr("g", "c", 1);
  instrumentation.incr("g", "c", 2);
  instrumentation.incr("g", "c1", 2);

  Instrumentation.Cron cron = instrumentation.createCron();
  cron.start();
  sleep(100);
  cron.stop();
  instrumentation.addCron("g", "t", cron);
  cron = instrumentation.createCron();
  cron.start();
  sleep(200);
  cron.stop();
  instrumentation.addCron("g", "t", cron);

  Instrumentation.Variable<String> var = new Instrumentation.Variable<String>() {
    @Override
    public String getValue() {
      return "foo";
    }
  };
  instrumentation.addVariable("g", "v", var);

  Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {
    @Override
    public Long getValue() {
      return 1L;
    }
  };
  instrumentation.addSampler("g", "s", 10, varToSample);

  Map<String, ?> snapshot = instrumentation.getSnapshot();
  assertNotNull(snapshot.get("os-env"));
  assertNotNull(snapshot.get("sys-props"));
  assertNotNull(snapshot.get("jvm"));
  assertNotNull(snapshot.get("counters"));
  assertNotNull(snapshot.get("timers"));
  assertNotNull(snapshot.get("variables"));
  assertNotNull(snapshot.get("samplers"));
  assertNotNull(((Map<String, String>) snapshot.get("os-env")).get("PATH"));
  assertNotNull(((Map<String, String>) snapshot.get("sys-props")).get("java.version"));
  assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("free.memory"));
  assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("max.memory"));
  assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("total.memory"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c1"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g").get("t"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g").get("v"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g").get("s"));

  StringWriter writer = new StringWriter();
  JSONObject.writeJSONString(snapshot, writer);
  writer.close();
  server.destroy();
}
 
Example 9
@Test
@TestDir
@SuppressWarnings("unchecked")
public void service() throws Exception {
  String dir = TestDirHelper.getTestDir().getAbsolutePath();
  String services = StringUtils.join(",", Arrays.asList(InstrumentationService.class.getName()));
  Configuration conf = new Configuration(false);
  conf.set("server.services", services);
  Server server = new Server("server", dir, dir, dir, dir, conf);
  server.init();

  Instrumentation instrumentation = server.get(Instrumentation.class);
  assertNotNull(instrumentation);
  instrumentation.incr("g", "c", 1);
  instrumentation.incr("g", "c", 2);
  instrumentation.incr("g", "c1", 2);

  Instrumentation.Cron cron = instrumentation.createCron();
  cron.start();
  sleep(100);
  cron.stop();
  instrumentation.addCron("g", "t", cron);
  cron = instrumentation.createCron();
  cron.start();
  sleep(200);
  cron.stop();
  instrumentation.addCron("g", "t", cron);

  Instrumentation.Variable<String> var = new Instrumentation.Variable<String>() {
    @Override
    public String getValue() {
      return "foo";
    }
  };
  instrumentation.addVariable("g", "v", var);

  Instrumentation.Variable<Long> varToSample = new Instrumentation.Variable<Long>() {
    @Override
    public Long getValue() {
      return 1L;
    }
  };
  instrumentation.addSampler("g", "s", 10, varToSample);

  Map<String, ?> snapshot = instrumentation.getSnapshot();
  assertNotNull(snapshot.get("os-env"));
  assertNotNull(snapshot.get("sys-props"));
  assertNotNull(snapshot.get("jvm"));
  assertNotNull(snapshot.get("counters"));
  assertNotNull(snapshot.get("timers"));
  assertNotNull(snapshot.get("variables"));
  assertNotNull(snapshot.get("samplers"));
  assertNotNull(((Map<String, String>) snapshot.get("os-env")).get("PATH"));
  assertNotNull(((Map<String, String>) snapshot.get("sys-props")).get("java.version"));
  assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("free.memory"));
  assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("max.memory"));
  assertNotNull(((Map<String, ?>) snapshot.get("jvm")).get("total.memory"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("counters")).get("g").get("c1"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("timers")).get("g").get("t"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("variables")).get("g").get("v"));
  assertNotNull(((Map<String, Map<String, Object>>) snapshot.get("samplers")).get("g").get("s"));

  StringWriter writer = new StringWriter();
  JSONObject.writeJSONString(snapshot, writer);
  writer.close();
  server.destroy();
}
 
Example 10
/**
 * Writes a {@link Geometry} in GeoJson format into a {@link Writer}.
 * 
 * @param geometry
 *          Geometry to encode
 * @param writer
 *          Stream to encode to.
 * @throws IOException
 *           throws an IOException when unable to write the JSON string
 */
public void write(Geometry geometry, Writer writer) throws IOException {
  Map<String, Object> map = create(geometry, isEncodeCRS);
  JSONObject.writeJSONString(map, writer);
  writer.flush();
}