Package com.google.gson.stream

Examples of com.google.gson.stream.JsonWriter


  @Override
  public void sendJson(HttpResponseStatus status, Object object, Type type, Gson gson) {
    try {
      ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
      JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(new ChannelBufferOutputStream(channelBuffer),
                                                                    Charsets.UTF_8));
      try {
        gson.toJson(object, type, jsonWriter);
      } finally {
        jsonWriter.close();
      }

      sendContent(status, channelBuffer, "application/json", ImmutableMultimap.<String, String>of());
    } catch (IOException e) {
      throw Throwables.propagate(e);
View Full Code Here


   */
  @Override
  public void sendJson(HttpResponseStatus status, Object object, Type type, Gson gson) {
    try {
      ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
      JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(new ChannelBufferOutputStream(channelBuffer),
                                                                    Charsets.UTF_8));
      try {
        gson.toJson(object, type, jsonWriter);
      } finally {
        jsonWriter.close();
      }

      sendContent(status, channelBuffer, "application/json", ImmutableMultimap.<String, String>of());
    } catch (IOException e) {
      throw Throwables.propagate(e);
View Full Code Here

                      applicationId,
                      procedureName,
                      method));
    HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
    urlConn.setDoOutput(true);
    JsonWriter writer = new JsonWriter(new OutputStreamWriter(urlConn.getOutputStream(), Charsets.UTF_8));
    try {
      new Gson().toJson(arguments, new TypeToken<Map<String, String>>() { }.getType(), writer);
    } finally {
      writer.close();
    }
    if (urlConn.getResponseCode() != 200) {
      throw new IOException("Response code != 200 (responded = " +
                              urlConn.getResponseCode() + " " + urlConn.getResponseMessage() + ")");
    }
View Full Code Here

        return new byte[0];
    }

    public void writeTo(MessageContext outMsgCtxt, OMOutputFormat omOutputFormat, OutputStream outputStream, boolean b) throws AxisFault {
        String charSetEncoding = (String) outMsgCtxt.getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
        JsonWriter jsonWriter;
        String msg;

        try {
            jsonWriter = new JsonWriter(new OutputStreamWriter(outputStream, charSetEncoding));
            Object retObj = outMsgCtxt.getProperty(JsonConstant.RETURN_OBJECT);

            if (outMsgCtxt.isProcessingFault()) {
                OMElement element = outMsgCtxt.getEnvelope().getBody().getFirstElement();
                try {
                    jsonWriter.beginObject();
                    jsonWriter.name(element.getLocalName());
                    jsonWriter.beginObject();
                    Iterator childrenIterator = element.getChildElements();
                    while (childrenIterator.hasNext()) {
                        Object next = childrenIterator.next();
                        OMElement omElement = (OMElement) next;
                        jsonWriter.name(omElement.getLocalName());
                        jsonWriter.value(omElement.getText());
                    }
                    jsonWriter.endObject();
                    jsonWriter.endObject();
                    jsonWriter.flush();
                    jsonWriter.close();
                } catch (IOException e) {
                    throw new AxisFault("Error while processing fault code in JsonWriter");
                }

            } else if (retObj == null) {
                OMElement element = outMsgCtxt.getEnvelope().getBody().getFirstElement();
                QName elementQname = outMsgCtxt.getAxisOperation().getMessage
                        (WSDLConstants.MESSAGE_LABEL_OUT_VALUE).getElementQName();

                ArrayList<XmlSchema> schemas = outMsgCtxt.getAxisService().getSchema();
                GsonXMLStreamWriter xmlsw = new GsonXMLStreamWriter(jsonWriter,
                                                                    elementQname,
                                                                    schemas,
                                                                    outMsgCtxt.getConfigurationContext());
                try {
                    xmlsw.writeStartDocument();
                    if (b) {
                        element.serialize(xmlsw);
                    } else {
                        element.serializeAndConsume(xmlsw);
                    }
                    xmlsw.writeEndDocument();
                } catch (XMLStreamException e) {
                    throw new AxisFault("Error while writing to the output stream using JsonWriter", e);
                }

            } else {
                try {
                    Gson gson = new Gson();
                    jsonWriter.beginObject();
                    jsonWriter.name(JsonConstant.RESPONSE);
                    Type returnType = (Type) outMsgCtxt.getProperty(JsonConstant.RETURN_TYPE);
                    gson.toJson(retObj, returnType, jsonWriter);
                    jsonWriter.endObject();
                    jsonWriter.flush();

                } catch (IOException e) {
                    msg = "Exception occur while writting to JsonWriter at the JsonFormatter ";
                    log.error(msg, e);
                    throw AxisFault.makeFault(e);
View Full Code Here

  private SecurityContext securityContext = null;
  private JsonWriter writer               = null;

  public StructrJsonWriter(final SecurityContext securityContext, final Writer writer) {
    this.securityContext = securityContext;
    this.writer = new JsonWriter(writer);
    this.writer.setLenient(Boolean.parseBoolean(StructrApp.getConfigurationValue("json.lenient", "false")));
  }
View Full Code Here

    @Test
    public void testGsonXMLStreamWriter() throws Exception {
        jsonString = "{\"response\":{\"return\":{\"name\":\"kate\",\"age\":\"35\",\"gender\":\"female\"}}}";
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos, "UTF-8");
        JsonWriter jsonWriter = new JsonWriter(outputStreamWriter);
        String fileName = "test-resources/custom_schema/testSchema_1.xsd";
        InputStream is = new FileInputStream(fileName);
        XmlSchemaCollection schemaCol = new XmlSchemaCollection();
        XmlSchema schema = schemaCol.read(new StreamSource(is));
        List<XmlSchema> schemaList = new ArrayList<XmlSchema>();
View Full Code Here

      }

      if (saveFile) {
        log.warn("File " + genFile + " is from an old version and will be converted to the new format.");

        JsonWriter w = null;
        try {
          w = new JsonWriter(new FileWriter(genFile));
          w.setIndent("    ");
          writer.toJson(genList, w);
        } catch (IOException e) {
          log.error("There was an error updating " + genFile.getName() + "!", e);
        } finally {
          if (w != null) {
            try {
              w.close();
            } catch (IOException e) {
              log.error("There was an error updating " + genFile.getName() + "!", e);
            }
          }
        }
View Full Code Here

            .setPrettyPrinting()
            .serializeNulls()
            .excludeFieldsWithModifiers(Modifier.STATIC,
                Modifier.TRANSIENT).create();
        FileOutputStream outputStream = new FileOutputStream(dataFile);
        JsonWriter writer = new JsonWriter(new OutputStreamWriter(
            outputStream, "UTF-8"));
        writer.setSerializeNulls(true);
        writer.setIndent("    ");
        writer.beginArray();
        for (Map.Entry<UUID, WorkerInfo> i : workerStack.entrySet()) {
          adapter.setCurrentUUID(i.getKey());
          serializer.toJson(i.getValue(), WorkerInfo.class, writer);
        }
        writer.endArray();
        writer.close();
        outputStream.close();
        getLogger().log(Level.INFO, "Villagers information serialized.");
      } catch (Exception ex) {
        getLogger().log(Level.SEVERE, "Can't serialize villagers informations.", ex);
        ex.printStackTrace();
View Full Code Here

      // Send with chunk response, as we don't want to buffer all events in memory to determine the content-length.
      responder.sendChunkStart(HttpResponseStatus.OK,
                               ImmutableMultimap.of(HttpHeaders.Names.CONTENT_TYPE, "application/json; charset=utf-8"));
      ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
      JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(new ChannelBufferOutputStream(buffer),
                                                                    Charsets.UTF_8));
      // Response is an array of stream event
      jsonWriter.beginArray();
      while (limit > 0 && eventsRead > 0) {
        limit -= eventsRead;

        for (StreamEvent event : events) {
          GSON.toJson(event, StreamEvent.class, jsonWriter);
          jsonWriter.flush();

          // If exceeded chunk size limit, send a new chunk.
          if (buffer.readableBytes() >= CHUNK_SIZE) {
            // No way to know if it is ok to send chunk or not. See ENG-4168
            responder.sendChunk(buffer);
            buffer.clear();
          }
        }
        events.clear();

        if (limit > 0) {
          eventsRead = reader.read(events, getReadLimit(limit), 0, TimeUnit.SECONDS, readFilter);
        }
      }
      jsonWriter.endArray();
      jsonWriter.close();

      // Send the last chunk that still has data
      if (buffer.readable()) {
        responder.sendChunk(buffer);
      }
View Full Code Here

      do {
        event = events.peekFirst();

        try {
          JsonWriter jsonWriter = new JsonWriter(writer);
          jsonWriter.setIndent("  ");
          jsonWriter.beginArray();
          jsonWriter.value(event.getType().name());
          gson.toJson(event, event.getClass(), jsonWriter);
          jsonWriter.endArray();
        } catch (Throwable t) {
          Closeables.closeQuietly(writer);
          writer = null;

          SlaveMain.warn("Unhandled exception in event serialization.", t);
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.