@@ -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
}
@@ -685,6 +705,11 @@ public void writeString(char[] text, int offset, int len) throws IOException
685
705
if (!_skipValue ) {
686
706
if (!_arraySeparator .isEmpty ()) {
687
707
_addToArray (new String (text , offset , len ));
708
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
709
+ } else if (_nextColumnDecorator != null ) {
710
+ String str = new String (text , offset , len );
711
+ _writer .write (_columnIndex (),
712
+ _nextColumnDecorator .decorateValue (this , str ));
688
713
} else {
689
714
_writer .write (_columnIndex (), text , offset , len );
690
715
}
@@ -699,7 +724,12 @@ public final void writeString(SerializableString sstr) throws IOException
699
724
if (!_arraySeparator .isEmpty ()) {
700
725
_addToArray (sstr .getValue ());
701
726
} else {
702
- _writer .write (_columnIndex (), sstr .getValue ());
727
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
728
+ String text = sstr .getValue ();
729
+ if (_nextColumnDecorator != null ) {
730
+ text = _nextColumnDecorator .decorateValue (this , text );
731
+ }
732
+ _writer .write (_columnIndex (), text );
703
733
}
704
734
}
705
735
}
@@ -780,6 +810,7 @@ public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int l
780
810
writeNull ();
781
811
return ;
782
812
}
813
+
783
814
_verifyValueWrite ("write Binary value" );
784
815
if (!_skipValue ) {
785
816
// ok, better just Base64 encode as a String...
@@ -791,6 +822,10 @@ public void writeBinary(Base64Variant b64variant, byte[] data, int offset, int l
791
822
if (!_arraySeparator .isEmpty ()) {
792
823
_addToArray (encoded );
793
824
} else {
825
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
826
+ if (_nextColumnDecorator != null ) {
827
+ encoded = _nextColumnDecorator .decorateValue (this , encoded );
828
+ }
794
829
_writer .write (_columnIndex (), encoded );
795
830
}
796
831
}
@@ -810,7 +845,13 @@ public void writeBoolean(boolean state) throws IOException
810
845
if (!_arraySeparator .isEmpty ()) {
811
846
_addToArray (state ? "true" : "false" );
812
847
} else {
813
- _writer .write (_columnIndex (), state );
848
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations!
849
+ if (_nextColumnDecorator != null ) {
850
+ String text = _nextColumnDecorator .decorateValue (this , state ? "true" : "false" );
851
+ _writer .write (_columnIndex (), text );
852
+ } else {
853
+ _writer .write (_columnIndex (), state );
854
+ }
814
855
}
815
856
}
816
857
}
@@ -824,6 +865,15 @@ public void writeNull() throws IOException
824
865
if (!_arraySeparator .isEmpty ()) {
825
866
_addToArray (_schema .getNullValueOrEmpty ());
826
867
} else if (_tokenWriteContext .inObject ()) {
868
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
869
+ if (_nextColumnDecorator != null ) {
870
+ String nvl = _nextColumnDecorator .decorateNull (this );
871
+ if (nvl != null ) {
872
+ _writer .write (_columnIndex (), nvl );
873
+ return ;
874
+ }
875
+ }
876
+
827
877
_writer .writeNull (_columnIndex ());
828
878
} else if (_tokenWriteContext .inArray ()) {
829
879
// [dataformat-csv#106]: Need to make sure we don't swallow nulls in arrays either
@@ -833,6 +883,14 @@ public void writeNull() throws IOException
833
883
// based on either schema property, or CsvGenerator.Feature.
834
884
// Note: if nulls are to be written that way, would need to call `finishRow()` right after `writeNull()`
835
885
if (!_tokenWriteContext .getParent ().inRoot ()) {
886
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
887
+ if (_nextColumnDecorator != null ) {
888
+ String nvl = _nextColumnDecorator .decorateNull (this );
889
+ if (nvl != null ) {
890
+ _writer .write (_columnIndex (), nvl );
891
+ return ;
892
+ }
893
+ }
836
894
_writer .writeNull (_columnIndex ());
837
895
}
838
896
@@ -852,6 +910,10 @@ public void writeNumber(int v) throws IOException
852
910
if (!_skipValue ) {
853
911
if (!_arraySeparator .isEmpty ()) {
854
912
_addToArray (String .valueOf (v ));
913
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
914
+ } else if (_nextColumnDecorator != null ) {
915
+ _writer .write (_columnIndex (),
916
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
855
917
} else {
856
918
_writer .write (_columnIndex (), v );
857
919
}
@@ -870,6 +932,10 @@ public void writeNumber(long v) throws IOException
870
932
if (!_skipValue ) {
871
933
if (!_arraySeparator .isEmpty ()) {
872
934
_addToArray (String .valueOf (v ));
935
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
936
+ } else if (_nextColumnDecorator != null ) {
937
+ _writer .write (_columnIndex (),
938
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
873
939
} else {
874
940
_writer .write (_columnIndex (), v );
875
941
}
@@ -887,6 +953,10 @@ public void writeNumber(BigInteger v) throws IOException
887
953
if (!_skipValue ) {
888
954
if (!_arraySeparator .isEmpty ()) {
889
955
_addToArray (String .valueOf (v ));
956
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
957
+ } else if (_nextColumnDecorator != null ) {
958
+ _writer .write (_columnIndex (),
959
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
890
960
} else {
891
961
_writer .write (_columnIndex (), v );
892
962
@@ -901,6 +971,10 @@ public void writeNumber(double v) throws IOException
901
971
if (!_skipValue ) {
902
972
if (!_arraySeparator .isEmpty ()) {
903
973
_addToArray (String .valueOf (v ));
974
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
975
+ } else if (_nextColumnDecorator != null ) {
976
+ _writer .write (_columnIndex (),
977
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
904
978
} else {
905
979
_writer .write (_columnIndex (), v );
906
980
}
@@ -914,6 +988,10 @@ public void writeNumber(float v) throws IOException
914
988
if (!_skipValue ) {
915
989
if (!_arraySeparator .isEmpty ()) {
916
990
_addToArray (String .valueOf (v ));
991
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
992
+ } else if (_nextColumnDecorator != null ) {
993
+ _writer .write (_columnIndex (),
994
+ _nextColumnDecorator .decorateValue (this , String .valueOf (v )));
917
995
} else {
918
996
_writer .write (_columnIndex (), v );
919
997
}
@@ -932,6 +1010,11 @@ public void writeNumber(BigDecimal v) throws IOException
932
1010
boolean plain = isEnabled (JsonGenerator .Feature .WRITE_BIGDECIMAL_AS_PLAIN );
933
1011
if (!_arraySeparator .isEmpty ()) {
934
1012
_addToArray (plain ? v .toPlainString () : v .toString ());
1013
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
1014
+ } else if (_nextColumnDecorator != null ) {
1015
+ String numStr = plain ? v .toPlainString () : v .toString ();
1016
+ _writer .write (_columnIndex (),
1017
+ _nextColumnDecorator .decorateValue (this , numStr ));
935
1018
} else {
936
1019
_writer .write (_columnIndex (), v , plain );
937
1020
}
@@ -949,6 +1032,10 @@ public void writeNumber(String encodedValue) throws IOException
949
1032
if (!_skipValue ) {
950
1033
if (!_arraySeparator .isEmpty ()) {
951
1034
_addToArray (encodedValue );
1035
+ // 26-Aug-2024, tatu: [dataformats-text#495] Decorations?
1036
+ } else if (_nextColumnDecorator != null ) {
1037
+ _writer .write (_columnIndex (),
1038
+ _nextColumnDecorator .decorateValue (this , encodedValue ));
952
1039
} else {
953
1040
_writer .write (_columnIndex (), encodedValue );
954
1041
}
0 commit comments