예제 #1
0
 /**
  * Method for constructing JSON generator for writing JSON content using specified output stream.
  * Encoding to use must be specified, and needs to be one of available types (as per JSON
  * specification).
  *
  * <p>Underlying stream <b>is NOT owned</b> by the generator constructed, so that generator will
  * NOT close the output stream when {@link JsonGenerator#close} is called (unless auto-closing
  * feature, {@link com.fasterxml.jackson.core.JsonGenerator.Feature#AUTO_CLOSE_TARGET} is
  * enabled). Using application needs to close it explicitly if this is the case.
  *
  * <p>Note: there are formats that use fixed encoding (like most binary data formats) and that
  * ignore passed in encoding.
  *
  * @param out OutputStream to use for writing JSON content
  * @param enc Character encoding to use
  * @since 2.1
  */
 public JsonGenerator createGenerator(OutputStream out, JsonEncoding enc) throws IOException {
   // false -> we won't manage the stream unless explicitly directed to
   IOContext ctxt = _createContext(out, false);
   ctxt.setEncoding(enc);
   if (enc == JsonEncoding.UTF8) {
     // [JACKSON-512]: allow wrapping with _outputDecorator
     if (_outputDecorator != null) {
       out = _outputDecorator.decorate(ctxt, out);
     }
     return _createUTF8Generator(out, ctxt);
   }
   Writer w = _createWriter(out, enc, ctxt);
   // [JACKSON-512]: allow wrapping with _outputDecorator
   if (_outputDecorator != null) {
     w = _outputDecorator.decorate(ctxt, w);
   }
   return _createGenerator(w, ctxt);
 }
예제 #2
0
 /**
  * Method for constructing JSON generator for writing JSON content to specified file, overwriting
  * contents it might have (or creating it if such file does not yet exist). Encoding to use must
  * be specified, and needs to be one of available types (as per JSON specification).
  *
  * <p>Underlying stream <b>is owned</b> by the generator constructed, i.e. generator will handle
  * closing of file when {@link JsonGenerator#close} is called.
  *
  * @param f File to write contents to
  * @param enc Character encoding to use
  * @since 2.1
  */
 public JsonGenerator createGenerator(File f, JsonEncoding enc) throws IOException {
   OutputStream out = new FileOutputStream(f);
   // true -> yes, we have to manage the stream since we created it
   IOContext ctxt = _createContext(out, true);
   ctxt.setEncoding(enc);
   if (enc == JsonEncoding.UTF8) {
     // [JACKSON-512]: allow wrapping with _outputDecorator
     if (_outputDecorator != null) {
       out = _outputDecorator.decorate(ctxt, out);
     }
     return _createUTF8Generator(out, ctxt);
   }
   Writer w = _createWriter(out, enc, ctxt);
   // [JACKSON-512]: allow wrapping with _outputDecorator
   if (_outputDecorator != null) {
     w = _outputDecorator.decorate(ctxt, w);
   }
   return _createGenerator(w, ctxt);
 }