@@ -221,6 +221,14 @@ private Feature(boolean defaultState) {
221
221
*/
222
222
protected int _nextColumnByName = -1 ;
223
223
224
+ /**
225
+ * Decorator to use for decorating the column value to follow, if any;
226
+ * {@code null} if none.
227
+ *
228
+ * @since 2.18
229
+ */
230
+ protected CsvValueDecorator _nextColumnDecorator ;
231
+
224
232
/**
225
233
* Flag set when property to write is unknown, and the matching value
226
234
* is to be skipped quietly.
@@ -461,14 +469,16 @@ private final void _writeFieldName(String name) throws IOException
461
469
if (_skipWithin != null ) { // new in 2.7
462
470
_skipValue = true ;
463
471
_nextColumnByName = -1 ;
472
+ _nextColumnDecorator = null ;
464
473
return ;
465
474
}
466
475
// note: we are likely to get next column name, so pass it as hint
467
476
CsvSchema .Column col = _schema .column (name , _nextColumnByName +1 );
468
477
if (col == null ) {
478
+ _nextColumnByName = -1 ;
479
+ _nextColumnDecorator = null ;
469
480
if (isEnabled (JsonGenerator .Feature .IGNORE_UNKNOWN )) {
470
481
_skipValue = true ;
471
- _nextColumnByName = -1 ;
472
482
return ;
473
483
}
474
484
// not a low-level error, so:
@@ -477,6 +487,7 @@ private final void _writeFieldName(String name) throws IOException
477
487
_skipValue = false ;
478
488
// and all we do is just note index to use for following value write
479
489
_nextColumnByName = col .getIndex ();
490
+ _nextColumnDecorator = col .getValueDecorator ();
480
491
}
481
492
482
493
/*
@@ -606,8 +617,13 @@ public final void writeEndArray() throws IOException
606
617
return ;
607
618
}
608
619
if (!_arraySeparator .isEmpty ()) {
620
+ String value = _arrayContents .toString ();
621
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
622
+ if (_nextColumnDecorator != null ) {
623
+ value = _nextColumnDecorator .decorateValue (this , value );
624
+ }
609
625
_arraySeparator = CsvSchema .NO_ARRAY_ELEMENT_SEPARATOR ;
610
- _writer .write (_columnIndex (), _arrayContents . toString () );
626
+ _writer .write (_columnIndex (), value );
611
627
}
612
628
// 20-Nov-2014, tatu: When doing "untyped"/"raw" output, this means that row
613
629
// is now done. But not if writing such an array field, so:
@@ -673,6 +689,10 @@ public void writeString(String text) throws IOException
673
689
if (!_arraySeparator .isEmpty ()) {
674
690
_addToArray (text );
675
691
} else {
692
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
693
+ if (_nextColumnDecorator != null ) {
694
+ text = _nextColumnDecorator .decorateValue (this , text );
695
+ }
676
696
_writer .write (_columnIndex (), text );
677
697
}
678
698
}
@@ -681,6 +701,12 @@ public void writeString(String text) throws IOException
681
701
@ Override
682
702
public void writeString (char [] text , int offset , int len ) throws IOException
683
703
{
704
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
705
+ if (_nextColumnDecorator != null ) {
706
+ writeString (new String (text , offset , len ));
707
+ return ;
708
+ }
709
+
684
710
_verifyValueWrite ("write String value" );
685
711
if (!_skipValue ) {
686
712
if (!_arraySeparator .isEmpty ()) {
@@ -699,7 +725,12 @@ public final void writeString(SerializableString sstr) throws IOException
699
725
if (!_arraySeparator .isEmpty ()) {
700
726
_addToArray (sstr .getValue ());
701
727
} else {
702
- _writer .write (_columnIndex (), sstr .getValue ());
728
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
729
+ String text = sstr .getValue ();
730
+ if (_nextColumnDecorator != null ) {
731
+ text = _nextColumnDecorator .decorateValue (this , text );
732
+ }
733
+ _writer .write (_columnIndex (), text );
703
734
}
704
735
}
705
736
}
@@ -780,6 +811,7 @@ public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int l
780
811
writeNull ();
781
812
return ;
782
813
}
814
+
783
815
_verifyValueWrite ("write Binary value" );
784
816
if (!_skipValue ) {
785
817
// ok, better just Base64 encode as a String...
@@ -791,6 +823,10 @@ public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int l
791
823
if (!_arraySeparator .isEmpty ()) {
792
824
_addToArray (encoded );
793
825
} else {
826
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
827
+ if (_nextColumnDecorator != null ) {
828
+ encoded = _nextColumnDecorator .decorateValue (this , encoded );
829
+ }
794
830
_writer .write (_columnIndex (), encoded );
795
831
}
796
832
}
@@ -810,7 +846,13 @@ public void writeBoolean(boolean state) throws IOException
810
846
if (!_arraySeparator .isEmpty ()) {
811
847
_addToArray (state ? "true" : "false" );
812
848
} else {
813
- _writer .write (_columnIndex (), state );
849
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
850
+ if (_nextColumnDecorator != null ) {
851
+ String text = _nextColumnDecorator .decorateValue (this , state ? "true" : "false" );
852
+ _writer .write (_columnIndex (), text );
853
+ } else {
854
+ _writer .write (_columnIndex (), state );
855
+ }
814
856
}
815
857
}
816
858
}
@@ -824,6 +866,15 @@ public void writeNull() throws IOException
824
866
if (!_arraySeparator .isEmpty ()) {
825
867
_addToArray (_schema .getNullValueOrEmpty ());
826
868
} else if (_tokenWriteContext .inObject ()) {
869
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
870
+ if (_nextColumnDecorator != null ) {
871
+ String nvl = _nextColumnDecorator .decorateNull (this );
872
+ if (nvl != null ) {
873
+ _writer .write (_columnIndex (), nvl );
874
+ return ;
875
+ }
876
+ }
877
+
827
878
_writer .writeNull (_columnIndex ());
828
879
} else if (_tokenWriteContext .inArray ()) {
829
880
// [dataformat-csv#106]: Need to make sure we don't swallow nulls in arrays either
@@ -833,6 +884,14 @@ public void writeNull() throws IOException
833
884
// based on either schema property, or CsvGenerator.Feature.
834
885
// Note: if nulls are to be written that way, would need to call `finishRow()` right after `writeNull()`
835
886
if (!_tokenWriteContext .getParent ().inRoot ()) {
887
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
888
+ if (_nextColumnDecorator != null ) {
889
+ String nvl = _nextColumnDecorator .decorateNull (this );
890
+ if (nvl != null ) {
891
+ _writer .write (_columnIndex (), nvl );
892
+ return ;
893
+ }
894
+ }
836
895
_writer .writeNull (_columnIndex ());
837
896
}
838
897
@@ -852,6 +911,10 @@ public void writeNumber(int v) throws IOException
852
911
if (!_skipValue ) {
853
912
if (!_arraySeparator .isEmpty ()) {
854
913
_addToArray (String .valueOf (v ));
914
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
915
+ } else if (_nextColumnDecorator != null ) {
916
+ _writer .write (_columnIndex (),
917
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
855
918
} else {
856
919
_writer .write (_columnIndex (), v );
857
920
}
@@ -870,6 +933,10 @@ public void writeNumber(long v) throws IOException
870
933
if (!_skipValue ) {
871
934
if (!_arraySeparator .isEmpty ()) {
872
935
_addToArray (String .valueOf (v ));
936
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
937
+ } else if (_nextColumnDecorator != null ) {
938
+ _writer .write (_columnIndex (),
939
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
873
940
} else {
874
941
_writer .write (_columnIndex (), v );
875
942
}
@@ -887,6 +954,10 @@ public void writeNumber(BigInteger v) throws IOException
887
954
if (!_skipValue ) {
888
955
if (!_arraySeparator .isEmpty ()) {
889
956
_addToArray (String .valueOf (v ));
957
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
958
+ } else if (_nextColumnDecorator != null ) {
959
+ _writer .write (_columnIndex (),
960
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
890
961
} else {
891
962
_writer .write (_columnIndex (), v );
892
963
@@ -901,6 +972,10 @@ public void writeNumber(double v) throws IOException
901
972
if (!_skipValue ) {
902
973
if (!_arraySeparator .isEmpty ()) {
903
974
_addToArray (String .valueOf (v ));
975
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
976
+ } else if (_nextColumnDecorator != null ) {
977
+ _writer .write (_columnIndex (),
978
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
904
979
} else {
905
980
_writer .write (_columnIndex (), v );
906
981
}
@@ -914,6 +989,10 @@ public void writeNumber(float v) throws IOException
914
989
if (!_skipValue ) {
915
990
if (!_arraySeparator .isEmpty ()) {
916
991
_addToArray (String .valueOf (v ));
992
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
993
+ } else if (_nextColumnDecorator != null ) {
994
+ _writer .write (_columnIndex (),
995
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
917
996
} else {
918
997
_writer .write (_columnIndex (), v );
919
998
}
@@ -932,6 +1011,11 @@ public void writeNumber(BigDecimal v) throws IOException
932
1011
boolean plain = isEnabled (JsonGenerator .Feature .WRITE_BIGDECIMAL_AS_PLAIN );
933
1012
if (!_arraySeparator .isEmpty ()) {
934
1013
_addToArray (plain ? v .toPlainString () : v .toString ());
1014
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
1015
+ } else if (_nextColumnDecorator != null ) {
1016
+ String numStr = plain ? v .toPlainString () : v .toString ();
1017
+ _writer .write (_columnIndex (),
1018
+ _nextColumnDecorator .decorateValue (this , numStr ));
935
1019
} else {
936
1020
_writer .write (_columnIndex (), v , plain );
937
1021
}
@@ -949,6 +1033,10 @@ public void writeNumber(String encodedValue) throws IOException
949
1033
if (!_skipValue ) {
950
1034
if (!_arraySeparator .isEmpty ()) {
951
1035
_addToArray (encodedValue );
1036
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
1037
+ } else if (_nextColumnDecorator != null ) {
1038
+ _writer .write (_columnIndex (),
1039
+ _nextColumnDecorator .decorateValue (this , encodedValue ));
952
1040
} else {
953
1041
_writer .write (_columnIndex (), encodedValue );
954
1042
}
0 commit comments