Package org.codehaus.jackson

Examples of org.codehaus.jackson.JsonGenerator


    private String _testBeanMarshallNegative(final Class<User> clazz, final JAXBContext context, final Object testInstance)
            throws Exception {
        final JsonFactory factory = new JsonFactory();
        final Writer sWriter = new StringWriter();
        final JsonGenerator jsonGenerator = factory.createJsonGenerator(sWriter);

        final Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(testInstance, new Stax2JacksonWriter(jsonGenerator, clazz, context));

        jsonGenerator.flush();
        return sWriter.toString();
    }
View Full Code Here


    final ObjectMapper objectMapper = jsonHandler.getMapper();
    final ServletOutputStream outputStream = response.getOutputStream();

    if (!streamResponse) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
      JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(bos, JsonEncoding.UTF8);
      objectMapper.writeValue(jsonGenerator, responseObject);
      response.setContentLength(bos.size());
      outputStream.write(bos.toByteArray());
    } else {
      JsonGenerator jsonGenerator = objectMapper.getJsonFactory().createJsonGenerator(outputStream,
          JsonEncoding.UTF8);
      objectMapper.writeValue(jsonGenerator, responseObject);
    }

    outputStream.flush();
View Full Code Here

        SmileFactory sf = new SmileFactory();
        sf.configure(SmileGenerator.Feature.CHECK_SHARED_NAMES, true);
        sf.configure(SmileGenerator.Feature.CHECK_SHARED_STRING_VALUES, true);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream(100);
       
        JsonGenerator jgen = sf.createJsonGenerator(bytes);
        jgen.writeStartArray();
        jgen.writeStartObject();
        jgen.writeStringField("key", "value");
        jgen.writeEndObject();
        jgen.writeStartObject();
        jgen.writeStringField("key", "value");
        jgen.writeEndObject();
        jgen.writeEndArray();
        jgen.close();

        SmileParser p = _smileParser(bytes.toByteArray());

        assertToken(JsonToken.START_ARRAY, p.nextToken());
        String str;
View Full Code Here

    {
        ObjectMapper mapper = new ObjectMapper();
        assertTrue(mapper.getSerializationConfig().isEnabled(SerializationConfig.Feature.FLUSH_AFTER_WRITE_VALUE));
        // default is to flush after writeValue()
        StringWriter sw = new StringWriter();
        JsonGenerator jgen = mapper.getJsonFactory().createJsonGenerator(sw);
        mapper.writeValue(jgen, Integer.valueOf(13));
        assertEquals("13", sw.toString());
        jgen.close();

        // ditto with ObjectWriter
        sw = new StringWriter();
        jgen = mapper.getJsonFactory().createJsonGenerator(sw);
        ObjectWriter ow = mapper.writer();
        ow.writeValue(jgen, Integer.valueOf(99));
        assertEquals("99", sw.toString());
        jgen.close();
    }
View Full Code Here

    {
        // but should not occur if configured otherwise
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.FLUSH_AFTER_WRITE_VALUE, false);
        StringWriter sw = new StringWriter();
        JsonGenerator jgen = mapper.getJsonFactory().createJsonGenerator(sw);

        mapper.writeValue(jgen, Integer.valueOf(13));
        // no flushing now:
        assertEquals("", sw.toString());
        // except when actually flushing
        jgen.flush();
        assertEquals("13", sw.toString());
        jgen.close();
        // Also, same should happen with ObjectWriter
        sw = new StringWriter();
        jgen = mapper.getJsonFactory().createJsonGenerator(sw);
        ObjectWriter ow = mapper.writer();
        ow.writeValue(jgen, Integer.valueOf(99));
        assertEquals("", sw.toString());
        // except when actually flushing
        jgen.flush();
        assertEquals("99", sw.toString());
        jgen.close();
    }
View Full Code Here

    JsonFactory jsonFactory = new JsonFactory();
    ObjectMapper objectMapper = new ObjectMapper();
    StringWriter stringWriter = new StringWriter();

    try {
      JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(stringWriter);
      ObjectNode objectNode = objectMapper.readValue(json, ObjectNode.class);
      objectNode.put(fieldName, id);
      objectMapper.writeTree(jsonGenerator, objectNode);
      jsonGenerator.flush();
      jsonGenerator.close();
      return stringWriter.toString();
    } catch (Exception e) {
      log.error(e);
      return json;
    }
View Full Code Here

      ObjectMapper objectMapper = new ObjectMapper();
      StringWriter stringWriter = new StringWriter();

      JsonParser jsonParser = jsonFactory.createJsonParser(new StringReader(json));
      JsonNode jsonNode = objectMapper.readTree(jsonParser);
      JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(stringWriter);
      jsonGenerator.useDefaultPrettyPrinter();

      objectMapper.writeTree(jsonGenerator, jsonNode);

      jsonGenerator.flush();
      jsonGenerator.close();

      /*
       * TODO decide upon logger usage
       */
      // String lineSeparator = System.getProperty("line.separator");
View Full Code Here

     */
    public static String getMessage(Object object, MessageFormat format) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
        try {
            if (format == MessageFormat.json) {
                JsonGenerator jsonGenerator = jsonObjectMapper.getJsonFactory().createJsonGenerator(bos, JsonEncoding.UTF8);
                jsonObjectMapper.writeValue(jsonGenerator, object);
            } else {
                xmlRopResponseMarshaller.marshaller(object, bos);
            }
            return bos.toString(UTF_8);
View Full Code Here

public class JacksonJsonRopMarshaller implements RopMarshaller {

    private static ObjectMapper objectMapper;
    public void marshaller(Object object, OutputStream outputStream) {
        try {
            JsonGenerator jsonGenerator = getObjectMapper().getJsonFactory().createJsonGenerator(outputStream, JsonEncoding.UTF8);
            getObjectMapper().writeValue(jsonGenerator, object);
        } catch (IOException e) {
            throw new RopException(e);
        }
    }
View Full Code Here

   * @throws IOException
   */
  @Nonnull
  public String createJson() throws IOException {
    StringWriter writer = new StringWriter();
    JsonGenerator generator = new JsonFactory().createJsonGenerator( writer );
    generator.writeStartObject();

    generator.writeStringField( "_id", id );
    generator.writeStringField( "language", "javascript" );

    generator.writeObjectFieldStart( "views" );

    for ( DesignDocuments.View view : views ) {
      generator.writeObjectFieldStart( view.getName() );

      generator.writeStringField( "map", view.getMappingFunction() );
      @Nullable String reduceFunction = view.getReduceFunction();
      if ( reduceFunction != null ) {
        generator.writeStringField( "reduce", reduceFunction );
      }
      generator.writeEndObject();
    }


    generator.writeEndObject();
    generator.writeEndObject();
    generator.flush();
    return writer.toString();
  }
View Full Code Here

TOP

Related Classes of org.codehaus.jackson.JsonGenerator

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.