44import com .jsoniter .any .Any ;
55
66import java .io .IOException ;
7+ import java .lang .reflect .Field ;
8+ import java .util .ArrayList ;
79import java .util .HashMap ;
10+ import java .util .List ;
811import java .util .Map ;
912
1013class ReflectionObjectEncoder implements Encoder .ReflectionEncoder {
1114
1215 private final ClassDescriptor desc ;
16+ private final List <EncodeTo > fields = new ArrayList <EncodeTo >();
17+ private final List <EncodeTo > getters = new ArrayList <EncodeTo >();
1318
1419 public ReflectionObjectEncoder (ClassInfo classInfo ) {
1520 desc = ClassDescriptor .getEncodingClassDescriptor (classInfo , true );
16- for (Binding binding : desc .allEncoderBindings ()) {
21+ for (EncodeTo encodeTo : desc .encodeTos ()) {
22+ Binding binding = encodeTo .binding ;
1723 if (binding .encoder == null ) {
1824 // the field encoder might be registered directly
1925 binding .encoder = JsoniterSpi .getEncoder (binding .encoderCacheKey ());
2026 }
27+ if (binding .field != null ) {
28+ fields .add (encodeTo );
29+ } else {
30+ getters .add (encodeTo );
31+ }
2132 }
2233 }
2334
@@ -34,17 +45,13 @@ public void encode(Object obj, JsonStream stream) throws IOException {
3445 public Any wrap (Object obj ) {
3546 HashMap <String , Object > copied = new HashMap <String , Object >();
3647 try {
37- for (Binding field : desc .fields ) {
38- Object val = field .field .get (obj );
39- for (String toName : field .toNames ) {
40- copied .put (toName , val );
41- }
48+ for (EncodeTo encodeTo : fields ) {
49+ Object val = encodeTo .binding .field .get (obj );
50+ copied .put (encodeTo .toName , val );
4251 }
43- for (Binding getter : desc .getters ) {
44- Object val = getter .method .invoke (obj );
45- for (String toName : getter .toNames ) {
46- copied .put (toName , val );
47- }
52+ for (EncodeTo getter : getters ) {
53+ Object val = getter .binding .method .invoke (obj );
54+ copied .put (getter .toName , val );
4855 }
4956 } catch (Exception e ) {
5057 throw new JsonException (e );
@@ -59,48 +66,13 @@ private void enocde_(Object obj, JsonStream stream) throws Exception {
5966 }
6067 stream .writeObjectStart ();
6168 boolean notFirst = false ;
62- boolean omitZero = JsoniterSpi .getCurrentConfig ().omitZero ();
63- for (Binding field : desc .fields ) {
64- Object val = field .field .get (obj );
65- for (String toName : field .toNames ) {
66- if (!(field .shouldOmitNull && val == null )) {
67- if (omitZero && val instanceof Number && ((Number ) val ).doubleValue () == 0 ) {
68- continue ;
69- }
70- if (notFirst ) {
71- stream .writeMore ();
72- } else {
73- notFirst = true ;
74- }
75- stream .writeObjectField (toName );
76- if (field .encoder != null ) {
77- field .encoder .encode (val , stream );
78- } else {
79- stream .writeVal (val );
80- }
81- }
82- }
69+ for (EncodeTo encodeTo : fields ) {
70+ Object val = encodeTo .binding .field .get (obj );
71+ notFirst = writeEncodeTo (stream , notFirst , encodeTo , val );
8372 }
84- for (Binding getter : desc .getters ) {
85- Object val = getter .method .invoke (obj );
86- for (String toName : getter .toNames ) {
87- if (!(getter .shouldOmitNull && val == null )) {
88- if (omitZero && val instanceof Number && ((Number ) val ).doubleValue () == 0 ) {
89- continue ;
90- }
91- if (notFirst ) {
92- stream .writeMore ();
93- } else {
94- notFirst = true ;
95- }
96- stream .writeObjectField (toName );
97- if (getter .encoder != null ) {
98- getter .encoder .encode (val , stream );
99- } else {
100- stream .writeVal (val );
101- }
102- }
103- }
73+ for (EncodeTo encodeTo : getters ) {
74+ Object val = encodeTo .binding .method .invoke (obj );
75+ notFirst = writeEncodeTo (stream , notFirst , encodeTo , val );
10476 }
10577 for (UnwrapperDescriptor unwrapper : desc .unwrappers ) {
10678 if (unwrapper .isMap ) {
@@ -125,4 +97,24 @@ private void enocde_(Object obj, JsonStream stream) throws Exception {
12597 }
12698 stream .writeObjectEnd ();
12799 }
100+
101+ private boolean writeEncodeTo (JsonStream stream , boolean notFirst , EncodeTo encodeTo , Object val ) throws IOException {
102+ if (!(encodeTo .binding .shouldOmitNull && val == null )) {
103+ if (JsoniterSpi .getCurrentConfig ().omitZero () && val instanceof Number && ((Number ) val ).doubleValue () == 0 ) {
104+ return notFirst ;
105+ }
106+ if (notFirst ) {
107+ stream .writeMore ();
108+ } else {
109+ notFirst = true ;
110+ }
111+ stream .writeObjectField (encodeTo .toName );
112+ if (encodeTo .binding .encoder != null ) {
113+ encodeTo .binding .encoder .encode (val , stream );
114+ } else {
115+ stream .writeVal (val );
116+ }
117+ }
118+ return notFirst ;
119+ }
128120}
0 commit comments