Package com.google.gson.stream

Examples of com.google.gson.stream.JsonWriter


  public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOException {
    try {
      if (generateNonExecutableJson) {
        writer.append(JSON_NON_EXECUTABLE_PREFIX);
      }
      JsonWriter jsonWriter = new JsonWriter(Streams.writerForAppendable(writer));
      if (prettyPrinting) {
        jsonWriter.setIndent("  ");
      }
      toJson(jsonElement, jsonWriter);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
View Full Code Here


    return createJsonGenerator(new OutputStreamWriter(out, enc));
  }

  @Override
  public JsonGenerator createJsonGenerator(Writer writer) {
    return new GsonGenerator(this, new JsonWriter(writer));
  }
View Full Code Here

  public void exportData(HttpServletResponse resp, Principal prin) throws IOException {

    resp.setContentType("application/json");

    // this writer puts things out onto the wire
    JsonWriter writer = new JsonWriter(resp.getWriter());
    writer.setIndent("  ");

    try {

          writer.beginObject();

      writer.name("exported-at");
      writer.value(dateFormat.format(new Date()));

      writer.name("exported-from");
      writer.value(config.getIssuer());

      writer.name("exported-by");
      writer.value(prin.getName());

      // delegate to the service to do the actual export
      dataService_1_1.exportData(writer);

      writer.endObject(); // end root
      writer.close();

    } catch (IOException e) {
      logger.error("Unable to export data", e);
    }
  }
View Full Code Here

  protected void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {
    resp.setCharacterEncoding(ENCODING);
    resp.setContentType(CONTENT_TYPE_JSON);

    JsonWriter writer = new JsonWriter(resp.getWriter());
    String method = req.getParameter(WjrParamKey.KEY_METHOD);
    if (METHOD_LOAD_CONFIG.equals(method)) {
      writeConfig(writer, loadConfig(req));
    } else if (METHOD_LOAD_STORE.equals(method)) {
      writeStore(writer, loadStore());
View Full Code Here

    public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType,
            MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException,
            WebApplicationException {

        try (OutputStreamWriter writer = new OutputStreamWriter(entityStream, Charsets.UTF_8)) {
            try (JsonWriter jsonWriter = new JsonWriter(new BufferedWriter(writer))) {
                if (object instanceof GeneratedMessage) {
                    ProtobufJsonWriter.serialize((GeneratedMessage) object, jsonWriter);
                } else {
                    Iterable<? extends Message> iterable = (Iterable<? extends Message>) object;
                    jsonWriter.beginArray();
                    for (Message msg : iterable) {
                        ProtobufJsonWriter.serialize(msg, jsonWriter);
                    }
                    jsonWriter.endArray();
                }
            }
        }
    }
View Full Code Here

  @Override public void processTrial(Trial trial) {
    if (!writer.isPresent()) {
      try {
        Files.createParentDirs(workFile);
        JsonWriter writer =
            new JsonWriter(new OutputStreamWriter(new FileOutputStream(workFile), Charsets.UTF_8));
        writer.setIndent("  ")// always pretty print
        writer.beginArray();
        this.writer = Optional.of(writer);
      } catch (IOException e) {
        logger.log(SEVERE, String.format(
            "An error occured writing trial %s. Results in %s will be incomplete.", trial.id(),
            resultFile), e);
      }
    }
    if (writer.isPresent()) {
      gson.toJson(trial, Trial.class, writer.get());
    }
  }
View Full Code Here

    Mockito.when(tokenRepository.getAllRefreshTokens()).thenReturn(allRefreshTokens);
    Mockito.when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());
       
    // do the data export
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    writer.beginObject();
    dataService.exportData(writer);
    writer.endObject();
    writer.close();
   
    // parse the output as a JSON object for testing
    JsonElement elem = new JsonParser().parse(stringWriter.toString());
    JsonObject root = elem.getAsJsonObject();
   
View Full Code Here

    Mockito.when(tokenRepository.getAllAccessTokens()).thenReturn(allAccessTokens);
    Mockito.when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());
       
    // do the data export
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    writer.beginObject();
    dataService.exportData(writer);
    writer.endObject();
    writer.close();
   
    // parse the output as a JSON object for testing
    JsonElement elem = new JsonParser().parse(stringWriter.toString());
    JsonObject root = elem.getAsJsonObject();
   
View Full Code Here

    Mockito.when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>());
    Mockito.when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());
   
    // do the data export
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    writer.beginObject();
    dataService.exportData(writer);
    writer.endObject();
    writer.close();
   
    // parse the output as a JSON object for testing
    JsonElement elem = new JsonParser().parse(stringWriter.toString());
    JsonObject root = elem.getAsJsonObject();
   
View Full Code Here

    Mockito.when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>());
    Mockito.when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());
   
    // do the data export
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(stringWriter);
    writer.beginObject();
    dataService.exportData(writer);
    writer.endObject();
    writer.close();
   
    // parse the output as a JSON object for testing
    JsonElement elem = new JsonParser().parse(stringWriter.toString());
    JsonObject root = elem.getAsJsonObject();
View Full Code Here

TOP

Related Classes of com.google.gson.stream.JsonWriter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.