8
8
import com .fasterxml .jackson .core .*;
9
9
import com .fasterxml .jackson .core .base .GeneratorBase ;
10
10
import com .fasterxml .jackson .core .io .CharacterEscapes ;
11
- import com .fasterxml .jackson .core .json .JsonWriteContext ;
12
11
import com .fasterxml .jackson .core .io .IOContext ;
13
12
import com .fasterxml .jackson .dataformat .csv .impl .CsvEncoder ;
14
13
import com .fasterxml .jackson .dataformat .csv .impl .SimpleTokenWriteContext ;
@@ -166,7 +165,7 @@ private Feature(boolean defaultState) {
166
165
*
167
166
* @since 2.11
168
167
*/
169
- protected SimpleTokenWriteContext _csvWriteContext ;
168
+ protected SimpleTokenWriteContext _tokenWriteContext ;
170
169
171
170
protected CharacterEscapes _characterEscapes = null ;
172
171
@@ -244,7 +243,8 @@ public CsvGenerator(IOContext ctxt, int jsonFeatures, int csvFeatures,
244
243
_formatFeatures = csvFeatures ;
245
244
_schema = schema ;
246
245
_writer = new CsvEncoder (ctxt , csvFeatures , out , schema );
247
- _csvWriteContext = SimpleTokenWriteContext .createRootContext (null );
246
+ _writeContext = null ; // just to make sure it won't be used
247
+ _tokenWriteContext = SimpleTokenWriteContext .createRootContext (null );
248
248
_writer .setOutputEscapes (CsvCharacterEscapes .fromCsvFeatures (csvFeatures ).getEscapeCodesForAscii ());
249
249
}
250
250
@@ -255,7 +255,8 @@ public CsvGenerator(IOContext ctxt, int jsonFeatures, int csvFeatures,
255
255
_ioContext = ctxt ;
256
256
_formatFeatures = csvFeatures ;
257
257
_writer = csvWriter ;
258
- _csvWriteContext = SimpleTokenWriteContext .createRootContext (null );
258
+ _writeContext = null ; // just to make sure it won't be used
259
+ _tokenWriteContext = SimpleTokenWriteContext .createRootContext (null );
259
260
}
260
261
261
262
/*
@@ -310,7 +311,7 @@ public int getOutputBuffered() {
310
311
311
312
@ Override
312
313
public SimpleTokenWriteContext getOutputContext () {
313
- return _csvWriteContext ;
314
+ return _tokenWriteContext ;
314
315
}
315
316
316
317
@ Override
@@ -395,7 +396,7 @@ public boolean canOmitFields() {
395
396
@ Override
396
397
public final void writeFieldName (String name ) throws IOException
397
398
{
398
- if (!_csvWriteContext .writeFieldName (name )) {
399
+ if (!_tokenWriteContext .writeFieldName (name )) {
399
400
_reportError ("Can not write a field name, expecting a value" );
400
401
}
401
402
_writeFieldName (name );
@@ -405,7 +406,7 @@ public final void writeFieldName(String name) throws IOException
405
406
public final void writeFieldName (SerializableString name ) throws IOException
406
407
{
407
408
// Object is a value, need to verify it's allowed
408
- if (!_csvWriteContext .writeFieldName (name .getValue ())) {
409
+ if (!_tokenWriteContext .writeFieldName (name .getValue ())) {
409
410
_reportError ("Can not write a field name, expecting a value" );
410
411
}
411
412
_writeFieldName (name .getValue ());
@@ -507,10 +508,10 @@ public final void writeStartArray() throws IOException
507
508
_verifyValueWrite ("start an array" );
508
509
// Ok to create root-level array to contain Objects/Arrays, but
509
510
// can not nest arrays in objects
510
- if (_csvWriteContext .inObject ()) {
511
+ if (_tokenWriteContext .inObject ()) {
511
512
if ((_skipWithin == null )
512
513
&& _skipValue && isEnabled (JsonGenerator .Feature .IGNORE_UNKNOWN )) {
513
- _skipWithin = _csvWriteContext ;
514
+ _skipWithin = _tokenWriteContext ;
514
515
} else if (!_skipValue ) {
515
516
// First: column may have its own separator
516
517
String sep ;
@@ -540,20 +541,20 @@ && _skipValue && isEnabled(JsonGenerator.Feature.IGNORE_UNKNOWN)) {
540
541
_reportError ("CSV generator does not support nested Array values" );
541
542
}
542
543
}
543
- _csvWriteContext = _csvWriteContext .createChildArrayContext (null );
544
+ _tokenWriteContext = _tokenWriteContext .createChildArrayContext (null );
544
545
// and that's about it, really
545
546
}
546
547
547
548
@ Override
548
549
public final void writeEndArray () throws IOException
549
550
{
550
- if (!_csvWriteContext .inArray ()) {
551
- _reportError ("Current context not Array but " +_csvWriteContext .typeDesc ());
551
+ if (!_tokenWriteContext .inArray ()) {
552
+ _reportError ("Current context not Array but " +_tokenWriteContext .typeDesc ());
552
553
}
553
- _csvWriteContext = _csvWriteContext .getParent ();
554
+ _tokenWriteContext = _tokenWriteContext .getParent ();
554
555
// 14-Dec-2015, tatu: To complete skipping of ignored structured value, need this:
555
556
if (_skipWithin != null ) {
556
- if (_csvWriteContext == _skipWithin ) {
557
+ if (_tokenWriteContext == _skipWithin ) {
557
558
_skipWithin = null ;
558
559
}
559
560
return ;
@@ -564,7 +565,7 @@ public final void writeEndArray() throws IOException
564
565
}
565
566
// 20-Nov-2014, tatu: When doing "untyped"/"raw" output, this means that row
566
567
// is now done. But not if writing such an array field, so:
567
- if (!_csvWriteContext .inObject ()) {
568
+ if (!_tokenWriteContext .inObject ()) {
568
569
finishRow ();
569
570
}
570
571
}
@@ -575,30 +576,30 @@ public final void writeStartObject() throws IOException
575
576
_verifyValueWrite ("start an object" );
576
577
// No nesting for objects; can write Objects inside logical root-level arrays.
577
578
// 14-Dec-2015, tatu: ... except, should be fine if we are ignoring the property
578
- if (_csvWriteContext .inObject () ||
579
+ if (_tokenWriteContext .inObject () ||
579
580
// 07-Nov-2017, tatu: But we may actually be nested indirectly; so check
580
- (_csvWriteContext .inArray () && !_csvWriteContext .getParent ().inRoot ())) {
581
+ (_tokenWriteContext .inArray () && !_tokenWriteContext .getParent ().inRoot ())) {
581
582
if (_skipWithin == null ) { // new in 2.7
582
583
if (_skipValue && isEnabled (JsonGenerator .Feature .IGNORE_UNKNOWN )) {
583
- _skipWithin = _csvWriteContext ;
584
+ _skipWithin = _tokenWriteContext ;
584
585
} else {
585
586
_reportMappingError ("CSV generator does not support Object values for properties (nested Objects)" );
586
587
}
587
588
}
588
589
}
589
- _csvWriteContext = _csvWriteContext .createChildObjectContext (null );
590
+ _tokenWriteContext = _tokenWriteContext .createChildObjectContext (null );
590
591
}
591
592
592
593
@ Override
593
594
public final void writeEndObject () throws IOException
594
595
{
595
- if (!_csvWriteContext .inObject ()) {
596
- _reportError ("Current context not Object but " +_csvWriteContext .typeDesc ());
596
+ if (!_tokenWriteContext .inObject ()) {
597
+ _reportError ("Current context not Object but " +_tokenWriteContext .typeDesc ());
597
598
}
598
- _csvWriteContext = _csvWriteContext .getParent ();
599
+ _tokenWriteContext = _tokenWriteContext .getParent ();
599
600
// 14-Dec-2015, tatu: To complete skipping of ignored structured value, need this:
600
601
if (_skipWithin != null ) {
601
- if (_csvWriteContext == _skipWithin ) {
602
+ if (_tokenWriteContext == _skipWithin ) {
602
603
_skipWithin = null ;
603
604
}
604
605
return ;
@@ -775,16 +776,16 @@ public void writeNull() throws IOException
775
776
if (!_skipValue ) {
776
777
if (!_arraySeparator .isEmpty ()) {
777
778
_addToArray (_schema .getNullValueOrEmpty ());
778
- } else if (_csvWriteContext .inObject ()) {
779
+ } else if (_tokenWriteContext .inObject ()) {
779
780
_writer .writeNull (_columnIndex ());
780
- } else if (_csvWriteContext .inArray ()) {
781
+ } else if (_tokenWriteContext .inArray ()) {
781
782
// [dataformat-csv#106]: Need to make sure we don't swallow nulls in arrays either
782
783
// 04-Jan-2016, tatu: but check for case of array-wrapping, in which case null stands for absence
783
784
// of Object. In this case, could either add an empty row, or skip -- for now, we'll
784
785
// just skip; can change, if so desired, to expose "root null" as empty rows, possibly
785
786
// based on either schema property, or CsvGenerator.Feature.
786
787
// Note: if nulls are to be written that way, would need to call `finishRow()` right after `writeNull()`
787
- if (!_csvWriteContext .getParent ().inRoot ()) {
788
+ if (!_tokenWriteContext .getParent ().inRoot ()) {
788
789
_writer .writeNull (_columnIndex ());
789
790
}
790
791
@@ -924,7 +925,7 @@ public void writeOmittedField(String fieldName) throws IOException
924
925
// assumed to have been removed from schema too
925
926
} else {
926
927
// basically combination of "writeFieldName()" and "writeNull()"
927
- if (!_csvWriteContext .writeFieldName (fieldName )) {
928
+ if (!_tokenWriteContext .writeFieldName (fieldName )) {
928
929
_reportError ("Can not skip a field, expecting a value" );
929
930
}
930
931
// and all we do is just note index to use for following value write
@@ -944,7 +945,7 @@ public void writeOmittedField(String fieldName) throws IOException
944
945
@ Override
945
946
protected final void _verifyValueWrite (String typeMsg ) throws IOException
946
947
{
947
- if (!_csvWriteContext .writeValue ()) {
948
+ if (!_tokenWriteContext .writeValue ()) {
948
949
_reportError ("Can not " +typeMsg +", expecting field name" );
949
950
}
950
951
if (_handleFirstLine ) {
0 commit comments