55import java .lang .reflect .Field ;
66import java .lang .reflect .Method ;
77import java .lang .reflect .Type ;
8+ import java .util .function .ObjDoubleConsumer ;
89
910import static com .alibaba .fastjson2 .JSONWriter .Feature .WriteNonStringValueAsString ;
1011
1112final class FieldWriterDoubleVal <T >
1213 extends FieldWriter <T > {
13- @ SuppressWarnings ("unchecked" )
14+ private final ObjDoubleConsumer <JSONWriter > writerImpl ;
15+
1416 FieldWriterDoubleVal (
1517 String name ,
1618 int ordinal ,
@@ -24,6 +26,16 @@ final class FieldWriterDoubleVal<T>
2426 Object function
2527 ) {
2628 super (name , ordinal , features , format , null , label , fieldType , fieldClass , field , method , function );
29+
30+ if (decimalFormat != null ) {
31+ writerImpl = (w , v ) -> w .writeDouble (v , decimalFormat );
32+ } else {
33+ if ((features & WriteNonStringValueAsString .mask ) != 0 ) {
34+ writerImpl = JSONWriter ::writeString ;
35+ } else {
36+ writerImpl = JSONWriter ::writeDouble ;
37+ }
38+ }
2739 }
2840
2941 @ Override
@@ -43,30 +55,12 @@ public boolean write(JSONWriter jsonWriter, T object) {
4355 }
4456
4557 writeFieldName (jsonWriter );
46- if (decimalFormat != null ) {
47- jsonWriter .writeDouble (value , decimalFormat );
48- } else {
49- if ((features & WriteNonStringValueAsString .mask ) != 0 ) {
50- jsonWriter .writeString (value );
51- } else {
52- jsonWriter .writeDouble (value );
53- }
54- }
58+ writerImpl .accept (jsonWriter , value );
5559 return true ;
5660 }
5761
5862 @ Override
5963 public void writeValue (JSONWriter jsonWriter , T object ) {
60- double value = propertyAccessor .getDoubleValue (object );
61-
62- if (decimalFormat != null ) {
63- jsonWriter .writeDouble (value , decimalFormat );
64- } else {
65- if ((features & WriteNonStringValueAsString .mask ) != 0 ) {
66- jsonWriter .writeString (value );
67- } else {
68- jsonWriter .writeDouble (value );
69- }
70- }
64+ writerImpl .accept (jsonWriter , propertyAccessor .getDoubleValue (object ));
7165 }
7266}
0 commit comments