3
3
import com .fasterxml .jackson .core .*;
4
4
import com .fasterxml .jackson .core .exc .StreamWriteException ;
5
5
import com .fasterxml .jackson .core .io .CharacterEscapes ;
6
+ import com .fasterxml .jackson .core .io .DataOutputAsStream ;
7
+ import com .fasterxml .jackson .core .io .SegmentedStringWriter ;
6
8
import com .fasterxml .jackson .core .type .TypeReference ;
9
+ import com .fasterxml .jackson .core .util .ByteArrayBuilder ;
7
10
import com .fasterxml .jackson .databind .DatabindException ;
8
11
import com .fasterxml .jackson .databind .JavaType ;
9
12
import com .fasterxml .jackson .databind .JsonMappingException ;
21
24
22
25
import java .io .DataOutput ;
23
26
import java .io .File ;
27
+ import java .io .FileOutputStream ;
24
28
import java .io .IOException ;
25
29
import java .io .OutputStream ;
26
30
import java .io .Writer ;
31
+ import java .nio .charset .Charset ;
27
32
import java .text .DateFormat ;
28
33
import java .util .Locale ;
29
34
import java .util .Map ;
@@ -419,11 +424,21 @@ public void writeValue(File resultFile, Object value) throws IOException, Stream
419
424
_objectWriter .writeValue (resultFile , value );
420
425
}
421
426
427
+ public void writeValue (File resultFile , Object value , Charset encoding )
428
+ throws IOException , StreamWriteException , DatabindException {
429
+ _writeValueAndClose (createGenerator (resultFile , encoding ), value );
430
+ }
431
+
422
432
@ Override
423
433
public void writeValue (OutputStream out , Object value ) throws IOException , StreamWriteException , DatabindException {
424
434
_objectWriter .writeValue (out , value );
425
435
}
426
436
437
+ public void writeValue (OutputStream out , Object value , Charset encoding )
438
+ throws IOException , StreamWriteException , DatabindException {
439
+ _writeValueAndClose (createGenerator (out , encoding ), value );
440
+ }
441
+
427
442
@ Override
428
443
public void writeValue (Writer w , Object value ) throws IOException , StreamWriteException , DatabindException {
429
444
_objectWriter .writeValue (w , value );
@@ -434,6 +449,11 @@ public void writeValue(DataOutput out, Object value) throws IOException, StreamW
434
449
_objectWriter .writeValue (out , value );
435
450
}
436
451
452
+ public void writeValue (DataOutput out , Object value , Charset encoding )
453
+ throws IOException , StreamWriteException , DatabindException {
454
+ _writeValueAndClose (createGenerator (out , encoding ), value );
455
+ }
456
+
437
457
@ Override
438
458
public String writeValueAsString (Object value ) throws JsonProcessingException {
439
459
return _objectWriter .writeValueAsString (value );
@@ -444,6 +464,33 @@ public byte[] writeValueAsBytes(Object value) throws JsonProcessingException {
444
464
return _objectWriter .writeValueAsBytes (value );
445
465
}
446
466
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
+
447
494
@ Override
448
495
public void acceptJsonFormatVisitor (JavaType type , JsonFormatVisitorWrapper visitor ) throws JsonMappingException {
449
496
_objectWriter .acceptJsonFormatVisitor (type , visitor );
@@ -463,4 +510,21 @@ public boolean canSerialize(Class<?> type) {
463
510
public boolean canSerialize (Class <?> type , AtomicReference <Throwable > cause ) {
464
511
return _objectWriter .canSerialize (type , cause );
465
512
}
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
+ }
466
530
}
0 commit comments