Example #1
0
  @Override
  public void write(JsonWriter out) throws IOException {
    out.startArray();

    for (int i = 0; i < values.length; i++) {
      if (i > 0) {
        out.writeArraySeparator();
      }

      if (values[i] == null) {
        out.write(Json.NULL);
      } else {
        values[i].write(out);
      }
    }

    out.endArray();
  }
 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());
   }
 }
Example #3
0
 @Override
 protected void write(JsonWriter writer) throws IOException {
   writer.write(string);
 }
 public void write(OutputStream out) throws IOException {
   JsonWriter jsonWriter = new JsonWriter();
   jsonWriter.write(this, out);
 }
  public void execute(
      AnnoResource resource,
      OutputStream out,
      Range range,
      Map<String, String> params,
      String contentType)
      throws IOException, NotAuthorizedException, BadRequestException, NotFoundException {
    Object source = resource.getSource();
    ControllerMethod cm = getBestMethod(source.getClass(), contentType, params, null);
    if (cm == null) {
      throw new RuntimeException("Method not found: " + getClass() + " - " + source.getClass());
    }
    log.trace("execute GET method: " + cm.method.getName());
    try {
      // Object[] args = annoResourceFactory.buildInvokeArgs(resource, cm.method, range, params,
      // contentType, out);
      // Object result = cm.method.invoke(cm.controller, args);
      Object result = invoke(cm, resource, range, params, contentType, out);
      if (result != null) {
        log.trace("method returned a value, so write it to output");
        if (result instanceof String) {
          ModelAndView modelAndView = new ModelAndView("resource", source, result.toString());
          processTemplate(modelAndView, resource, out);
        } else if (result instanceof JsonResult) {
          JsonResult jsonr = (JsonResult) result;
          JsonWriter jsonWriter = new JsonWriter();
          jsonWriter.write(jsonr, out);

        } else if (result instanceof InputStream) {
          InputStream contentIn = (InputStream) result;
          if (range != null) {
            StreamUtils.readTo(contentIn, out, true, false, range.getStart(), range.getFinish());
          } else {
            try {
              IOUtils.copy(contentIn, out);
            } finally {
              IOUtils.closeQuietly(contentIn);
            }
          }
        } else if (result instanceof byte[]) {
          byte[] bytes = (byte[]) result;
          out.write(bytes);
        } else if (result instanceof ModelAndView) {
          processTemplate((ModelAndView) result, resource, out);
        } else {
          throw new RuntimeException(
              "Unsupported return type from method: "
                  + cm.method.getName()
                  + " in "
                  + cm.controller.getClass()
                  + " should return String or byte[]");
        }
      }
      out.flush();
    } catch (IOException e) {
      throw e;
    } catch (NotAuthorizedException e) {
      throw e;
    } catch (BadRequestException e) {
      throw e;
    } catch (NotFoundException e) {
      throw e;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }