@@ -639,6 +639,77 @@ public ObjectWriter withRootValueSeparator(SerializableString sep) {
639
639
return _new (_generatorSettings .withRootValueSeparator (sep ), _prefetch );
640
640
}
641
641
642
+ /*
643
+ /**********************************************************
644
+ /* Factory methods for creating JsonGenerators (added in 2.11)
645
+ /**********************************************************
646
+ */
647
+
648
+ /**
649
+ * Factory method for constructing properly initialized {@link JsonGenerator}
650
+ * to write content using specified {@link OutputStream}.
651
+ * Generator is not managed (or "owned") by mapper: caller is responsible
652
+ * for properly closing it once content generation is complete.
653
+ *
654
+ * @since 2.11
655
+ */
656
+ public JsonGenerator createGenerator (OutputStream out ) throws IOException {
657
+ _assertNotNull ("out" , out );
658
+ return _generatorFactory .createGenerator (out , JsonEncoding .UTF8 );
659
+ }
660
+
661
+ /**
662
+ * Factory method for constructing properly initialized {@link JsonGenerator}
663
+ * to write content using specified {@link OutputStream} and encoding.
664
+ * Generator is not managed (or "owned") by mapper: caller is responsible
665
+ * for properly closing it once content generation is complete.
666
+ *
667
+ * @since 2.11
668
+ */
669
+ public JsonGenerator createGenerator (OutputStream out , JsonEncoding enc ) throws IOException {
670
+ _assertNotNull ("out" , out );
671
+ return _generatorFactory .createGenerator (out , enc );
672
+ }
673
+
674
+ /**
675
+ * Factory method for constructing properly initialized {@link JsonGenerator}
676
+ * to write content using specified {@link Writer}.
677
+ * Generator is not managed (or "owned") by mapper: caller is responsible
678
+ * for properly closing it once content generation is complete.
679
+ *
680
+ * @since 2.11
681
+ */
682
+ public JsonGenerator createGenerator (Writer w ) throws IOException {
683
+ _assertNotNull ("w" , w );
684
+ return _generatorFactory .createGenerator (w );
685
+ }
686
+
687
+ /**
688
+ * Factory method for constructing properly initialized {@link JsonGenerator}
689
+ * to write content to specified {@link File}, using specified encoding.
690
+ * Generator is not managed (or "owned") by mapper: caller is responsible
691
+ * for properly closing it once content generation is complete.
692
+ *
693
+ * @since 2.11
694
+ */
695
+ public JsonGenerator createGenerator (File outputFile , JsonEncoding enc ) throws IOException {
696
+ _assertNotNull ("outputFile" , outputFile );
697
+ return _generatorFactory .createGenerator (outputFile , enc );
698
+ }
699
+
700
+ /**
701
+ * Factory method for constructing properly initialized {@link JsonGenerator}
702
+ * to write content using specified {@link DataOutput}.
703
+ * Generator is not managed (or "owned") by mapper: caller is responsible
704
+ * for properly closing it once content generation is complete.
705
+ *
706
+ * @since 2.11
707
+ */
708
+ public JsonGenerator createGenerator (DataOutput out ) throws IOException {
709
+ _assertNotNull ("out" , out );
710
+ return _generatorFactory .createGenerator (out );
711
+ }
712
+
642
713
/*
643
714
/**********************************************************
644
715
/* Factory methods for sequence writers (2.5)
@@ -659,9 +730,7 @@ public ObjectWriter withRootValueSeparator(SerializableString sep) {
659
730
* @since 2.5
660
731
*/
661
732
public SequenceWriter writeValues (File out ) throws IOException {
662
- _assertNotNull ("out" , out );
663
- return _newSequenceWriter (false ,
664
- _generatorFactory .createGenerator (out , JsonEncoding .UTF8 ), true );
733
+ return _newSequenceWriter (false , createGenerator (out , JsonEncoding .UTF8 ), true );
665
734
}
666
735
667
736
/**
@@ -699,9 +768,7 @@ public SequenceWriter writeValues(JsonGenerator g) throws IOException {
699
768
* @since 2.5
700
769
*/
701
770
public SequenceWriter writeValues (Writer out ) throws IOException {
702
- _assertNotNull ("out" , out );
703
- return _newSequenceWriter (false ,
704
- _generatorFactory .createGenerator (out ), true );
771
+ return _newSequenceWriter (false , createGenerator (out ), true );
705
772
}
706
773
707
774
/**
@@ -718,18 +785,14 @@ public SequenceWriter writeValues(Writer out) throws IOException {
718
785
* @since 2.5
719
786
*/
720
787
public SequenceWriter writeValues (OutputStream out ) throws IOException {
721
- _assertNotNull ("out" , out );
722
- return _newSequenceWriter (false ,
723
- _generatorFactory .createGenerator (out , JsonEncoding .UTF8 ), true );
788
+ return _newSequenceWriter (false , createGenerator (out , JsonEncoding .UTF8 ), true );
724
789
}
725
790
726
791
/**
727
792
* @since 2.8
728
793
*/
729
794
public SequenceWriter writeValues (DataOutput out ) throws IOException {
730
- _assertNotNull ("out" , out );
731
- return _newSequenceWriter (false ,
732
- _generatorFactory .createGenerator (out ), true );
795
+ return _newSequenceWriter (false , createGenerator (out ), true );
733
796
}
734
797
735
798
/**
@@ -748,9 +811,7 @@ public SequenceWriter writeValues(DataOutput out) throws IOException {
748
811
* @since 2.5
749
812
*/
750
813
public SequenceWriter writeValuesAsArray (File out ) throws IOException {
751
- _assertNotNull ("out" , out );
752
- return _newSequenceWriter (true ,
753
- _generatorFactory .createGenerator (out , JsonEncoding .UTF8 ), true );
814
+ return _newSequenceWriter (true , createGenerator (out , JsonEncoding .UTF8 ), true );
754
815
}
755
816
756
817
/**
@@ -790,8 +851,7 @@ public SequenceWriter writeValuesAsArray(JsonGenerator gen) throws IOException {
790
851
* @since 2.5
791
852
*/
792
853
public SequenceWriter writeValuesAsArray (Writer out ) throws IOException {
793
- _assertNotNull ("out" , out );
794
- return _newSequenceWriter (true , _generatorFactory .createGenerator (out ), true );
854
+ return _newSequenceWriter (true , createGenerator (out ), true );
795
855
}
796
856
797
857
/**
@@ -810,17 +870,14 @@ public SequenceWriter writeValuesAsArray(Writer out) throws IOException {
810
870
* @since 2.5
811
871
*/
812
872
public SequenceWriter writeValuesAsArray (OutputStream out ) throws IOException {
813
- _assertNotNull ("out" , out );
814
- return _newSequenceWriter (true ,
815
- _generatorFactory .createGenerator (out , JsonEncoding .UTF8 ), true );
873
+ return _newSequenceWriter (true , createGenerator (out , JsonEncoding .UTF8 ), true );
816
874
}
817
875
818
876
/**
819
877
* @since 2.8
820
878
*/
821
879
public SequenceWriter writeValuesAsArray (DataOutput out ) throws IOException {
822
- _assertNotNull ("out" , out );
823
- return _newSequenceWriter (true , _generatorFactory .createGenerator (out ), true );
880
+ return _newSequenceWriter (true , createGenerator (out ), true );
824
881
}
825
882
826
883
/*
@@ -938,8 +995,7 @@ public void writeValue(JsonGenerator g, Object value) throws IOException
938
995
public void writeValue (File resultFile , Object value )
939
996
throws IOException , JsonGenerationException , JsonMappingException
940
997
{
941
- _assertNotNull ("resultFile" , resultFile );
942
- _configAndWriteValue (_generatorFactory .createGenerator (resultFile , JsonEncoding .UTF8 ), value );
998
+ _configAndWriteValue (createGenerator (resultFile , JsonEncoding .UTF8 ), value );
943
999
}
944
1000
945
1001
/**
@@ -956,8 +1012,7 @@ public void writeValue(File resultFile, Object value)
956
1012
public void writeValue (OutputStream out , Object value )
957
1013
throws IOException , JsonGenerationException , JsonMappingException
958
1014
{
959
- _assertNotNull ("out" , out );
960
- _configAndWriteValue (_generatorFactory .createGenerator (out , JsonEncoding .UTF8 ), value );
1015
+ _configAndWriteValue (createGenerator (out , JsonEncoding .UTF8 ), value );
961
1016
}
962
1017
963
1018
/**
@@ -973,8 +1028,7 @@ public void writeValue(OutputStream out, Object value)
973
1028
public void writeValue (Writer w , Object value )
974
1029
throws IOException , JsonGenerationException , JsonMappingException
975
1030
{
976
- _assertNotNull ("w" , w );
977
- _configAndWriteValue (_generatorFactory .createGenerator (w ), value );
1031
+ _configAndWriteValue (createGenerator (w ), value );
978
1032
}
979
1033
980
1034
/**
@@ -983,8 +1037,7 @@ public void writeValue(Writer w, Object value)
983
1037
public void writeValue (DataOutput out , Object value )
984
1038
throws IOException
985
1039
{
986
- _assertNotNull ("out" , out );
987
- _configAndWriteValue (_generatorFactory .createGenerator (out ), value );
1040
+ _configAndWriteValue (createGenerator (out ), value );
988
1041
}
989
1042
990
1043
/**
@@ -1002,7 +1055,7 @@ public String writeValueAsString(Object value)
1002
1055
// alas, we have to pull the recycler directly here...
1003
1056
SegmentedStringWriter sw = new SegmentedStringWriter (_generatorFactory ._getBufferRecycler ());
1004
1057
try {
1005
- _configAndWriteValue (_generatorFactory . createGenerator (sw ), value );
1058
+ _configAndWriteValue (createGenerator (sw ), value );
1006
1059
} catch (JsonProcessingException e ) {
1007
1060
throw e ;
1008
1061
} catch (IOException e ) { // shouldn't really happen, but is declared as possibility so:
@@ -1026,7 +1079,7 @@ public byte[] writeValueAsBytes(Object value)
1026
1079
{
1027
1080
ByteArrayBuilder bb = new ByteArrayBuilder (_generatorFactory ._getBufferRecycler ());
1028
1081
try {
1029
- _configAndWriteValue (_generatorFactory . createGenerator (bb , JsonEncoding .UTF8 ), value );
1082
+ _configAndWriteValue (createGenerator (bb , JsonEncoding .UTF8 ), value );
1030
1083
} catch (JsonProcessingException e ) { // to support [JACKSON-758]
1031
1084
throw e ;
1032
1085
} catch (IOException e ) { // shouldn't really happen, but is declared as possibility so:
0 commit comments