Ejemplo n.º 1
0
 /**
  * Method that can be used to serialize any Java value as a String. Functionally equivalent to
  * calling {@link #writeValue(Writer,Object)} with {@link java.io.StringWriter} and constructing
  * String, but more efficient.
  *
  * <p>Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
  */
 public String writeValueAsString(Object value) throws JsonProcessingException {
   // alas, we have to pull the recycler directly here...
   SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
   try {
     _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value);
   } catch (JsonProcessingException e) { // to support [JACKSON-758]
     throw e;
   } catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
     throw JsonMappingException.fromUnexpectedIOE(e);
   }
   return sw.getAndClear();
 }
Ejemplo n.º 2
0
 /**
  * Method that can be used to serialize any Java value as a byte array. Functionally equivalent to
  * calling {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream} and
  * getting bytes, but more efficient. Encoding used will be UTF-8.
  *
  * <p>Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
  */
 public byte[] writeValueAsBytes(Object value) throws JsonProcessingException {
   ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
   try {
     _configAndWriteValue(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value);
   } catch (JsonProcessingException e) { // to support [JACKSON-758]
     throw e;
   } catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
     throw JsonMappingException.fromUnexpectedIOE(e);
   }
   byte[] result = bb.toByteArray();
   bb.release();
   return result;
 }
Ejemplo n.º 3
0
  private void process(File input) throws IOException {
    JsonFactory jsonF = new JsonFactory();
    JsonParser jp = jsonF.createJsonParser(input);
    TwitterEntry entry = read(jp);

    // let's write to a file, using UTF-8 encoding (only sensible one)
    StringWriter strw = new StringWriter();
    JsonGenerator jg = jsonF.createJsonGenerator(strw);
    jg.useDefaultPrettyPrinter(); // enable indentation just to make debug/testing easier

    // Here we would modify it... for now, will just (re)indent it

    write(jg, entry);

    System.out.println("Result = [" + strw.toString() + "]");
  }
Ejemplo n.º 4
0
 /**
  * Method that can be used to serialize any Java value as JSON output, using Writer provided.
  *
  * <p>Note: method does not close the underlying stream explicitly here; however, {@link
  * JsonFactory} this mapper uses may choose to close the stream depending on its settings (by
  * default, it will try to close it when {@link JsonGenerator} we construct is closed).
  */
 public void writeValue(Writer w, Object value)
     throws IOException, JsonGenerationException, JsonMappingException {
   _configAndWriteValue(_jsonFactory.createJsonGenerator(w), value);
 }
Ejemplo n.º 5
0
 /**
  * Method that can be used to serialize any Java value as JSON output, using output stream
  * provided (using encoding {@link JsonEncoding#UTF8}).
  *
  * <p>Note: method does not close the underlying stream explicitly here; however, {@link
  * JsonFactory} this mapper uses may choose to close the stream depending on its settings (by
  * default, it will try to close it when {@link JsonGenerator} we construct is closed).
  */
 public void writeValue(OutputStream out, Object value)
     throws IOException, JsonGenerationException, JsonMappingException {
   _configAndWriteValue(_jsonFactory.createJsonGenerator(out, JsonEncoding.UTF8), value);
 }
Ejemplo n.º 6
0
 /**
  * Method that can be used to serialize any Java value as JSON output, written to File provided.
  */
 public void writeValue(File resultFile, Object value)
     throws IOException, JsonGenerationException, JsonMappingException {
   _configAndWriteValue(_jsonFactory.createJsonGenerator(resultFile, JsonEncoding.UTF8), value);
 }