|
32 | 32 | import com.cloud.utils.NumbersUtil; |
33 | 33 | import com.cloud.utils.Pair; |
34 | 34 | import com.cloud.utils.exception.CloudRuntimeException; |
| 35 | +import com.google.gson.ExclusionStrategy; |
| 36 | +import com.google.gson.FieldAttributes; |
35 | 37 | import com.google.gson.Gson; |
36 | 38 | import com.google.gson.GsonBuilder; |
37 | 39 | import com.google.gson.JsonArray; |
@@ -88,20 +90,33 @@ public static Version get(final byte ver) throws UnsupportedVersionException { |
88 | 90 | protected static final short FLAG_CONTROL = 0x40; |
89 | 91 |
|
90 | 92 | protected static final GsonBuilder s_gBuilder; |
| 93 | + protected static final GsonBuilder s_exposeAnnotationBuilder; |
| 94 | + |
91 | 95 | static { |
92 | 96 | s_gBuilder = new GsonBuilder(); |
93 | | - s_gBuilder.registerTypeAdapter(Command[].class, new ArrayTypeAdaptor<Command>()); |
94 | | - s_gBuilder.registerTypeAdapter(Answer[].class, new ArrayTypeAdaptor<Answer>()); |
95 | | -// final Type listType = new TypeToken<List<VolumeVO>>() {}.getType(); |
96 | | -// s_gBuilder.registerTypeAdapter(listType, new VolListTypeAdaptor()); |
97 | | - s_gBuilder.registerTypeAdapter(new TypeToken<List<PortConfig>>() {}.getType(), new PortConfigListTypeAdaptor()); |
98 | | - s_gBuilder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() {}.getType(), new NwGroupsCommandTypeAdaptor()); |
99 | | - s_logger.info("Builder inited."); |
| 97 | + setDefaultGsonConfig(s_gBuilder); |
| 98 | + s_logger.info("Default Builder inited."); |
| 99 | + |
| 100 | + s_exposeAnnotationBuilder = new GsonBuilder(); |
| 101 | + setDefaultGsonConfig(s_exposeAnnotationBuilder); |
| 102 | + s_exposeAnnotationBuilder.excludeFieldsWithoutExposeAnnotation(); |
| 103 | + s_logger.info("Expose annotation Builder inited."); |
| 104 | + } |
| 105 | + |
| 106 | + public static void setDefaultGsonConfig(GsonBuilder builder){ |
| 107 | + builder.registerTypeAdapter(Command[].class, new ArrayTypeAdaptor<Command>()); |
| 108 | + builder.registerTypeAdapter(Answer[].class, new ArrayTypeAdaptor<Answer>()); |
| 109 | + builder.registerTypeAdapter(new TypeToken<List<PortConfig>>() {}.getType(), new PortConfigListTypeAdaptor()); |
| 110 | + builder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() {}.getType(), new NwGroupsCommandTypeAdaptor()); |
100 | 111 | } |
101 | 112 |
|
102 | 113 | public static GsonBuilder initBuilder() { |
103 | 114 | return s_gBuilder; |
104 | 115 | } |
| 116 | + |
| 117 | + public static GsonBuilder initBuilderWithExposeAnnotation() { |
| 118 | + return s_exposeAnnotationBuilder; |
| 119 | + } |
105 | 120 |
|
106 | 121 | protected Version _ver; |
107 | 122 | protected long _seq; |
@@ -240,7 +255,7 @@ public Command[] getCommands() { |
240 | 255 | public String toString() { |
241 | 256 | String content = _content; |
242 | 257 | if (content == null) { |
243 | | - final Gson gson = s_gBuilder.create(); |
| 258 | + final Gson gson = s_gBuilder.create(); |
244 | 259 | try { |
245 | 260 | content = gson.toJson(_cmds); |
246 | 261 | } catch(Throwable e) { |
@@ -330,7 +345,7 @@ public void log(long agentId, String msg) { |
330 | 345 | s_logger.trace(buf.toString()); |
331 | 346 | } |
332 | 347 | } |
333 | | - |
| 348 | + |
334 | 349 | /** |
335 | 350 | * Factory method for Request and Response. It expects the bytes to be |
336 | 351 | * correctly formed so it's possible that it throws underflow exceptions |
|
0 commit comments