|
11 | 11 |
|
12 | 12 | public class GsonCompatibilityMode extends JsoniterAnnotationSupport { |
13 | 13 |
|
14 | | - private final static GsonCompatibilityMode INSTANCE = new GsonCompatibilityMode(); |
| 14 | + private Builder builder; |
| 15 | + |
| 16 | + private GsonCompatibilityMode(Builder builder) { |
| 17 | + this.builder = builder; |
| 18 | + } |
| 19 | + |
| 20 | + public static class Builder { |
| 21 | + private boolean excludeFieldsWithoutExposeAnnotation = false; |
| 22 | + |
| 23 | + public Builder excludeFieldsWithoutExposeAnnotation() { |
| 24 | + excludeFieldsWithoutExposeAnnotation = true; |
| 25 | + return this; |
| 26 | + } |
| 27 | + |
| 28 | + public GsonCompatibilityMode build() { |
| 29 | + return new GsonCompatibilityMode(this); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private final static GsonCompatibilityMode INSTANCE = new GsonCompatibilityMode(new Builder()); |
15 | 34 |
|
16 | 35 | public static void enable() { |
17 | 36 | JsoniterSpi.registerExtension(INSTANCE); |
@@ -116,24 +135,42 @@ protected JsonIgnore getJsonIgnore(Annotation[] annotations) { |
116 | 135 | } |
117 | 136 | final Expose gsonObj = getAnnotation( |
118 | 137 | annotations, Expose.class); |
119 | | - if (gsonObj == null) { |
120 | | - return null; |
| 138 | + if (gsonObj != null) { |
| 139 | + return new JsonIgnore() { |
| 140 | + @Override |
| 141 | + public boolean ignoreDecoding() { |
| 142 | + return !gsonObj.deserialize(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public boolean ignoreEncoding() { |
| 147 | + return !gsonObj.serialize(); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Class<? extends Annotation> annotationType() { |
| 152 | + return JsonIgnore.class; |
| 153 | + } |
| 154 | + }; |
121 | 155 | } |
122 | | - return new JsonIgnore() { |
123 | | - @Override |
124 | | - public boolean ignoreDecoding() { |
125 | | - return !gsonObj.deserialize(); |
126 | | - } |
127 | | - |
128 | | - @Override |
129 | | - public boolean ignoreEncoding() { |
130 | | - return !gsonObj.serialize(); |
131 | | - } |
132 | | - |
133 | | - @Override |
134 | | - public Class<? extends Annotation> annotationType() { |
135 | | - return JsonIgnore.class; |
136 | | - } |
137 | | - }; |
| 156 | + if (builder.excludeFieldsWithoutExposeAnnotation) { |
| 157 | + return new JsonIgnore() { |
| 158 | + @Override |
| 159 | + public boolean ignoreDecoding() { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public boolean ignoreEncoding() { |
| 165 | + return true; |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public Class<? extends Annotation> annotationType() { |
| 170 | + return JsonIgnore.class; |
| 171 | + } |
| 172 | + }; |
| 173 | + } |
| 174 | + return null; |
138 | 175 | } |
139 | 176 | } |
0 commit comments