33import com .fasterxml .jackson .core .*;
44import com .fasterxml .jackson .core .exc .StreamWriteException ;
55import com .fasterxml .jackson .core .io .CharacterEscapes ;
6+ import com .fasterxml .jackson .core .io .DataOutputAsStream ;
7+ import com .fasterxml .jackson .core .io .SegmentedStringWriter ;
68import com .fasterxml .jackson .core .type .TypeReference ;
9+ import com .fasterxml .jackson .core .util .ByteArrayBuilder ;
710import com .fasterxml .jackson .databind .DatabindException ;
811import com .fasterxml .jackson .databind .JavaType ;
912import com .fasterxml .jackson .databind .JsonMappingException ;
2124
2225import java .io .DataOutput ;
2326import java .io .File ;
27+ import java .io .FileOutputStream ;
2428import java .io .IOException ;
2529import java .io .OutputStream ;
2630import java .io .Writer ;
31+ import java .nio .charset .Charset ;
2732import java .text .DateFormat ;
2833import java .util .Locale ;
2934import java .util .Map ;
@@ -419,11 +424,21 @@ public void writeValue(File resultFile, Object value) throws IOException, Stream
419424 _objectWriter .writeValue (resultFile , value );
420425 }
421426
427+ public void writeValue (File resultFile , Object value , Charset encoding )
428+ throws IOException , StreamWriteException , DatabindException {
429+ _writeValueAndClose (createGenerator (resultFile , encoding ), value );
430+ }
431+
422432 @ Override
423433 public void writeValue (OutputStream out , Object value ) throws IOException , StreamWriteException , DatabindException {
424434 _objectWriter .writeValue (out , value );
425435 }
426436
437+ public void writeValue (OutputStream out , Object value , Charset encoding )
438+ throws IOException , StreamWriteException , DatabindException {
439+ _writeValueAndClose (createGenerator (out , encoding ), value );
440+ }
441+
427442 @ Override
428443 public void writeValue (Writer w , Object value ) throws IOException , StreamWriteException , DatabindException {
429444 _objectWriter .writeValue (w , value );
@@ -434,6 +449,11 @@ public void writeValue(DataOutput out, Object value) throws IOException, StreamW
434449 _objectWriter .writeValue (out , value );
435450 }
436451
452+ public void writeValue (DataOutput out , Object value , Charset encoding )
453+ throws IOException , StreamWriteException , DatabindException {
454+ _writeValueAndClose (createGenerator (out , encoding ), value );
455+ }
456+
437457 @ Override
438458 public String writeValueAsString (Object value ) throws JsonProcessingException {
439459 return _objectWriter .writeValueAsString (value );
@@ -444,6 +464,33 @@ public byte[] writeValueAsBytes(Object value) throws JsonProcessingException {
444464 return _objectWriter .writeValueAsBytes (value );
445465 }
446466
467+ /**
468+ * Method that can be used to serialize any Java value as
469+ * a byte array. Functionally equivalent to calling
470+ * {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream}
471+ * and getting bytes, but more efficient.
472+ *
473+ * @param value value to serialize as XML bytes
474+ * @param encoding character encoding for the XML output
475+ * @return byte array representing the XML output
476+ * @throws JsonProcessingException
477+ */
478+ public byte [] writeValueAsBytes (Object value , Charset encoding )
479+ throws JsonProcessingException
480+ {
481+ // Although 'close()' is NOP, use auto-close to avoid lgtm complaints
482+ try (ByteArrayBuilder bb = new ByteArrayBuilder (_generatorFactory ._getBufferRecycler ())) {
483+ _writeValueAndClose (createGenerator (bb , encoding ), value );
484+ final byte [] result = bb .toByteArray ();
485+ bb .release ();
486+ return result ;
487+ } catch (JsonProcessingException e ) { // to support [JACKSON-758]
488+ throw e ;
489+ } catch (IOException e ) { // shouldn't really happen, but is declared as possibility so:
490+ throw JsonMappingException .fromUnexpectedIOE (e );
491+ }
492+ }
493+
447494 @ Override
448495 public void acceptJsonFormatVisitor (JavaType type , JsonFormatVisitorWrapper visitor ) throws JsonMappingException {
449496 _objectWriter .acceptJsonFormatVisitor (type , visitor );
@@ -463,4 +510,21 @@ public boolean canSerialize(Class<?> type) {
463510 public boolean canSerialize (Class <?> type , AtomicReference <Throwable > cause ) {
464511 return _objectWriter .canSerialize (type , cause );
465512 }
513+
514+ private JsonGenerator createGenerator (OutputStream out , Charset charset ) throws IOException {
515+ _assertNotNull ("out" , out );
516+ return _configureGenerator (((XmlFactory ) _generatorFactory ).createGenerator (out , charset ));
517+ }
518+
519+ private JsonGenerator createGenerator (DataOutput out , Charset charset ) throws IOException {
520+ _assertNotNull ("out" , out );
521+ return _configureGenerator (((XmlFactory ) _generatorFactory )
522+ .createGenerator (new DataOutputAsStream (out ), charset ));
523+ }
524+
525+ private JsonGenerator createGenerator (File out , Charset charset ) throws IOException {
526+ _assertNotNull ("out" , out );
527+ return _configureGenerator (((XmlFactory ) _generatorFactory )
528+ .createGenerator (new FileOutputStream (out ), charset ));
529+ }
466530}
0 commit comments