@@ -97,287 +97,6 @@ mvn clean install -DskipTests=true -Dgpg.skip=true -Dmaven.javadoc.skip=true -B
97
97
VPackSlice slice = builder. slice(); // create slice
98
98
```
99
99
100
- ## serialize POJO
101
-
102
- ``` Java
103
- MyBean entity = new MyBean ();
104
- VPack vpack = new VPack .Builder (). build();
105
- VPackSlice slice = vpack. serialize(entity);
106
- ```
107
-
108
- ## deserialize VelocyPack
109
-
110
- ``` Java
111
- VPackSlice slice = ...
112
- VPack vpack = new VPack .Builder (). build();
113
- MyBean entity = vpack. deserialize(slice, MyBean . class);
114
- ```
115
-
116
- ## parse Json to VelocPack
117
-
118
- ``` Java
119
- String json = ...
120
- VPackParser parser = new VPackParser .Builder (). build();
121
- VPackSlice slice = parser. fromJson(json);
122
- ```
123
-
124
- ## parse VelocyPack to Json
125
-
126
- ``` Java
127
- VPackSlice slice = ...
128
- VPackParser parser = new VPackParser .Builder (). build();
129
- String json = parser. toJson(slice);
130
- ```
131
-
132
- # Registering modules
133
-
134
- Both ` VPack ` and ` VPackParser ` allow registering of modules which can offer custom serializers/deserializers for additional types.
135
-
136
- ## VPackModule
137
-
138
- ``` Java
139
- VPackModule module = ...
140
- VPack vpack = new VPack .Builder (). registerModule(module). build();
141
- ```
142
-
143
- ## VPackParserModule
144
-
145
- ``` Java
146
- VPackParserModule module = ...
147
- VPackParser parser = VPackParser . Builder(). registerModule(module). build();
148
- ```
149
-
150
- # Configure serialization / deserialization
151
-
152
- ## POJOs
153
-
154
- The class ` VPack ` can serialize/deserialize POJOs. They need at least a constructor without parameter. Also [ Builder deserialization] ( #builder-deserialization ) ,
155
- [ All-Arguments-Constructor deserialization] ( #all-arguments-constructor-deserialization ) and [ Static Factory Method deserialization] ( #static-factory-method-deserialization ) are supported.
156
-
157
-
158
- ``` Java
159
- public class MyObject {
160
-
161
- private String name;
162
- private Gender gender;
163
- private int age;
164
-
165
- public MyObject () {
166
- super ();
167
- }
168
-
169
- }
170
- ```
171
-
172
- ## serialized fieldnames
173
-
174
- To use a different serialized name for a field, use the annotation ` SerializedName ` .
175
-
176
- ``` Java
177
- public class MyObject {
178
-
179
- @SerializedName (" title" )
180
- private String name;
181
-
182
- private Gender gender;
183
- private int age;
184
-
185
- public MyObject () {
186
- super ();
187
- }
188
-
189
- }
190
- ```
191
-
192
- ## ignore fields
193
-
194
- To ignore fields at serialization/deserialization, use the annotation ` Expose `
195
-
196
- ``` Java
197
- public class MyObject {
198
-
199
- @Expose
200
- private String name;
201
- @Expose (serialize = true , deserialize = false )
202
- private Gender gender;
203
- private int age;
204
-
205
- public MyObject () {
206
- super ();
207
- }
208
-
209
- }
210
- ```
211
-
212
- ## custom de-/serializer
213
-
214
- ``` Java
215
- VPack vpack = new VPack .Builder ()
216
- .registerDeserializer(MyObject . class, new VPackDeserializer<MyObject > () {
217
- @Override
218
- public MyObject deserialize (
219
- final VPackSlice parent ,
220
- final VPackSlice vpack ,
221
- final VPackDeserializationContext context ) throws VPackException {
222
-
223
- final MyObject obj = new MyObject ();
224
- obj. setName(vpack. get(" name" ). getAsString());
225
- return obj;
226
- }
227
- }). registerSerializer(MyObject . class, new VPackSerializer<MyObject > () {
228
- @Override
229
- public void serialize (
230
- final VPackBuilder builder ,
231
- final String attribute ,
232
- final MyObject value ,
233
- final VPackSerializationContext context ) throws VPackException {
234
-
235
- builder. add(attribute, ValueType . OBJECT );
236
- builder. add(" name" , value. getName());
237
- builder. close();
238
- }
239
- }). build();
240
- ```
241
-
242
-
243
- # Builder deserialization
244
-
245
- Deserialization using builders is supported using the following annotations:
246
-
247
- ## @VPackPOJOBuilder
248
-
249
- It allows specifying the builder setters and build method. It has the following fields:
250
-
251
- - ` buildMethodName: String ` : the build method to call on the builder object after having set all the attributes
252
- - ` withSetterPrefix: String ` : the prefix of the builder setters
253
-
254
- This annotation can target:
255
- - the builder class having a public no-arg constructor, eg:
256
- ``` java
257
- @VPackPOJOBuilder (buildMethodName = " build" , withSetterPrefix = " set" )
258
- public class Builder {
259
- public Builder () {
260
- // ...
261
- }
262
-
263
- public MyClass build () {
264
- // ...
265
- }
266
-
267
- // ...
268
- }
269
- ```
270
- - a public static factory method which returns the builder, eg:
271
- ``` java
272
- public class MyClass {
273
- @VPackPOJOBuilder (buildMethodName = " build" , withSetterPrefix = " with" )
274
- public static Builder<MyClass > builder () {
275
- // ...
276
- }
277
- // ...
278
- }
279
- ```
280
-
281
- ## @VPackDeserialize
282
-
283
- It allows to specify the builder class that will be used during the deserialization. It has the following fields:
284
- - ` builder: Class<?> ` : builder class to use
285
- - ` builderConfig: VPackPOJOBuilder ` : it allows specifying the builder setters and build method, useful in case the
286
- builder code is auto generated and you cannot add ` @VPackPOJOBuilder ` to it.
287
-
288
- This annotation can target:
289
- - ` setter ` : allows specifying the builder for the setter argument
290
- - ` field ` : allows specifying the builder for the field
291
- - ` class ` : allows specifying the builder for the class
292
- - ` parameter ` : allows specifying the builder for a constructor (or factory method) parameter
293
-
294
- Example:
295
- ``` java
296
- @VPackDeserialize (builder = MyClassBuilder . class,
297
- builderConfig = @VPackPOJOBuilder (buildMethodName = " build" ,
298
- withSetterPrefix = " with" ))
299
- public class MyClass {
300
- // ...
301
- }
302
- ```
303
-
304
-
305
- # All-Arguments-Constructor deserialization
306
-
307
- Deserialization using All-Arguments-Constructor is supported annotating the constructor with ` @VPackCreator ` and
308
- annotating each of its parameters with ` @SerializedName("name") ` , eg:
309
-
310
- ``` java
311
- public class Person {
312
- private final String name;
313
- private final int age;
314
-
315
- @VPackCreator
316
- public Person (
317
- @SerializedName (" name" ) String name ,
318
- @SerializedName (" age" ) int age
319
- ) {
320
- this . name = name;
321
- this . age = age;
322
- }
323
- // ...
324
- }
325
- ```
326
-
327
-
328
- # Static Factory Method deserialization
329
-
330
- Deserialization using Static Factory Method is supported annotating the method with ` @VPackCreator ` and
331
- annotating each of its parameters with ` @SerializedName("name") ` , eg:
332
-
333
- ``` java
334
- public class Person {
335
- private final String name;
336
- private final int age;
337
-
338
- private Person (String name , int age ) {
339
- this . name = name;
340
- this . age = age;
341
- }
342
-
343
- @VPackCreator
344
- public static Person of (
345
- @SerializedName (" name" ) String name ,
346
- @SerializedName (" age" ) int age
347
- ) {
348
- return new Person (name, age);
349
- }
350
-
351
- // ...
352
- }
353
- ```
354
-
355
-
356
- # Kotlin data classes
357
-
358
- Deserialization of Kotlin data classes is supported annotating the constructor with ` @VPackCreator ` and annotating each of
359
- its parameters with ` @SerializedName("name") ` , eg:
360
-
361
- ``` kotlin
362
- data class Person @VPackCreator constructor(
363
- @SerializedName(" name" ) val name : String ,
364
- @SerializedName(" age" ) val age : Int
365
- )
366
- ```
367
-
368
-
369
- # Scala case classes
370
-
371
- Deserialization of Scala case classes is supported annotating the constructor with ` @VPackCreator ` and annotating each of
372
- its parameters with ` @SerializedName("name") ` , eg:
373
-
374
- ``` scala
375
- case class CasePerson @ VPackCreator ()(
376
- @ SerializedName (" name" ) val name : String ,
377
- @ SerializedName (" age" ) val age : Int
378
- )
379
- ```
380
-
381
100
382
101
# Learn more
383
102
0 commit comments