41
41
* The Skygear Record Serializer.
42
42
*/
43
43
public class RecordSerializer {
44
+
45
+ private static final String RecordSerializationDeprecatedIDKey = "_id" ;
46
+ private static final String RecordSerializationResponseTypeKey = "_type" ;
47
+ private static final String RecordSerializationRecordTypeKey = "_recordType" ;
48
+ private static final String RecordSerializationRecordIDKey = "_recordID" ;
49
+ private static final String RecordSerializationCreationDatetimeKey = "_created_at" ;
50
+ private static final String RecordSerializationUpdateDatetimeKey = "_updated_at" ;
51
+ private static final String RecordSerializationOwnerIDKey = "_ownerID" ;
52
+ private static final String RecordSerializationCreatorIDKey = "_created_by" ;
53
+ private static final String RecordSerializationUpdaterIDKey = "_updated_by" ;
54
+ private static final String RecordSerializationAccessKey = "_access" ;
55
+ private static final String RecordSerializationTransientKey = "_transient" ;
56
+
44
57
private static final String TAG = "Skygear SDK" ;
45
58
private static List <String > ReservedKeys = Arrays .asList (
46
- "_id" ,
47
- "_type" ,
48
- "_recordType" ,
49
- "_recordID" ,
50
- "_created_at" ,
51
- "_updated_at" ,
52
- "_ownerID" ,
53
- "_created_by" ,
54
- "_updated_by" ,
55
- "_access" ,
56
- "_transient"
59
+ RecordSerializationDeprecatedIDKey ,
60
+ RecordSerializationResponseTypeKey ,
61
+ RecordSerializationRecordTypeKey ,
62
+ RecordSerializationRecordIDKey ,
63
+ RecordSerializationCreationDatetimeKey ,
64
+ RecordSerializationUpdateDatetimeKey ,
65
+ RecordSerializationOwnerIDKey ,
66
+ RecordSerializationCreatorIDKey ,
67
+ RecordSerializationUpdaterIDKey ,
68
+ RecordSerializationAccessKey ,
69
+ RecordSerializationTransientKey
57
70
);
58
71
59
72
private static Set <? extends Class > CompatibleValueClasses = new HashSet <>(Arrays .asList (
@@ -163,31 +176,40 @@ public static JSONObject serialize(Record record) {
163
176
try {
164
177
JSONObject jsonObject = RecordSerializer .serialize (record .data );
165
178
166
- jsonObject .put ("_id" , String .format (
179
+ jsonObject .put (RecordSerializationDeprecatedIDKey , String .format (
167
180
"%s/%s" ,
168
181
record .getType (),
169
182
record .getId ()
170
183
));
171
- jsonObject .put ("_recordType" , record .getType ());
172
- jsonObject .put ("_recordID" , record .getId ());
184
+ jsonObject .put (RecordSerializationRecordTypeKey , record .getType ());
185
+ jsonObject .put (RecordSerializationRecordIDKey , record .getId ());
173
186
if (record .createdAt != null ) {
174
- jsonObject .put ("_created_at" , DateSerializer .stringFromDate (record .createdAt ));
187
+ jsonObject .put (
188
+ RecordSerializationCreationDatetimeKey ,
189
+ DateSerializer .stringFromDate (record .createdAt )
190
+ );
175
191
}
176
192
if (record .updatedAt != null ) {
177
- jsonObject .put ("_updated_at" , DateSerializer .stringFromDate (record .updatedAt ));
193
+ jsonObject .put (
194
+ RecordSerializationUpdateDatetimeKey ,
195
+ DateSerializer .stringFromDate (record .updatedAt )
196
+ );
178
197
}
179
198
if (record .creatorId != null ) {
180
- jsonObject .put ("_created_by" , record .creatorId );
199
+ jsonObject .put (RecordSerializationCreatorIDKey , record .creatorId );
181
200
}
182
201
if (record .updaterId != null ) {
183
- jsonObject .put ("_updated_by" , record .updaterId );
202
+ jsonObject .put (RecordSerializationUpdaterIDKey , record .updaterId );
184
203
}
185
204
if (record .ownerId != null ) {
186
- jsonObject .put ("_ownerID" , record .ownerId );
205
+ jsonObject .put (RecordSerializationOwnerIDKey , record .ownerId );
187
206
}
188
207
189
208
if (record .getAccess () != null ) {
190
- jsonObject .put ("_access" , AccessControlSerializer .serialize (record .getAccess ()));
209
+ jsonObject .put (
210
+ RecordSerializationAccessKey ,
211
+ AccessControlSerializer .serialize (record .getAccess ())
212
+ );
191
213
}
192
214
193
215
// handle _transient
@@ -203,7 +225,7 @@ public static JSONObject serialize(Record record) {
203
225
}
204
226
}
205
227
206
- jsonObject .put ("_transient" , transientObject );
228
+ jsonObject .put (RecordSerializationTransientKey , transientObject );
207
229
}
208
230
209
231
return jsonObject ;
@@ -222,57 +244,41 @@ public static JSONObject serialize(Record record) {
222
244
* @throws JSONException the JSON exception
223
245
*/
224
246
public static Record deserialize (JSONObject jsonObject ) throws JSONException {
225
- String recordType ;
226
- String recordID ;
227
- try {
228
- recordType = jsonObject .getString ("_recordType" );
229
- recordID = jsonObject .getString ("_recordID" );
230
- } catch (JSONException e ) {
231
- String typedId = jsonObject .optString ("_id" );
232
- String [] split = typedId .split ("/" , 2 );
233
-
234
- if (split .length < 2 || split [0 ].length () == 0 || split [1 ].length () == 0 ) {
235
- throw new InvalidParameterException ("Fail to parse record id" );
236
- }
237
-
238
- recordType = split [0 ];
239
- recordID = split [1 ];
240
- }
241
-
242
- Record record = new Record (recordType , recordID );
247
+ RecordIdentifier identifier = RecordSerializer .deserializeRecordIdentifer (jsonObject );
248
+ Record record = new Record (identifier .type , identifier .id );
243
249
244
250
// handle _create_at
245
- if (jsonObject .has ("_created_at" )) {
246
- String createdAtString = jsonObject .getString ("_created_at" );
251
+ if (jsonObject .has (RecordSerializationCreationDatetimeKey )) {
252
+ String createdAtString = jsonObject .getString (RecordSerializationCreationDatetimeKey );
247
253
record .createdAt = DateSerializer .dateFromString (createdAtString );
248
254
}
249
255
250
256
// handle _updated_at
251
- if (jsonObject .has ("_updated_at" )) {
252
- String updatedAtString = jsonObject .getString ("_updated_at" );
257
+ if (jsonObject .has (RecordSerializationUpdateDatetimeKey )) {
258
+ String updatedAtString = jsonObject .getString (RecordSerializationUpdateDatetimeKey );
253
259
record .updatedAt = DateSerializer .dateFromString (updatedAtString );
254
260
}
255
261
256
262
// handle _created_by, _updated_by, _ownerID
257
- if (jsonObject .has ("_created_by" )) {
258
- record .creatorId = jsonObject .getString ("_created_by" );
263
+ if (jsonObject .has (RecordSerializationCreatorIDKey )) {
264
+ record .creatorId = jsonObject .getString (RecordSerializationCreatorIDKey );
259
265
}
260
- if (jsonObject .has ("_updated_by" )) {
261
- record .updaterId = jsonObject .getString ("_updated_by" );
266
+ if (jsonObject .has (RecordSerializationUpdaterIDKey )) {
267
+ record .updaterId = jsonObject .getString (RecordSerializationUpdaterIDKey );
262
268
}
263
- if (jsonObject .has ("_ownerID" )) {
264
- record .ownerId = jsonObject .getString ("_ownerID" );
269
+ if (jsonObject .has (RecordSerializationOwnerIDKey )) {
270
+ record .ownerId = jsonObject .getString (RecordSerializationOwnerIDKey );
265
271
}
266
272
267
273
// handle _access
268
274
JSONArray accessJsonArray = null ;
269
- if (!jsonObject .isNull ("_access" )) {
270
- accessJsonArray = jsonObject .getJSONArray ("_access" );
275
+ if (!jsonObject .isNull (RecordSerializationAccessKey )) {
276
+ accessJsonArray = jsonObject .getJSONArray (RecordSerializationAccessKey );
271
277
}
272
278
273
279
// handle _transient
274
- if (!jsonObject .isNull ("_transient" )) {
275
- JSONObject transientObject = jsonObject .getJSONObject ("_transient" );
280
+ if (!jsonObject .isNull (RecordSerializationTransientKey )) {
281
+ JSONObject transientObject = jsonObject .getJSONObject (RecordSerializationTransientKey );
276
282
Iterator <String > transientKeys = transientObject .keys ();
277
283
278
284
while (transientKeys .hasNext ()) {
@@ -319,4 +325,47 @@ public static Record deserialize(JSONObject jsonObject) throws JSONException {
319
325
320
326
return record ;
321
327
}
328
+
329
+ /**
330
+ * RecordIdentifier is a data class representing the identifier of a record, which includes
331
+ * the type and the ID of the record.
332
+ *
333
+ * RecordIdentifier is only used within this package.
334
+ */
335
+ static class RecordIdentifier {
336
+ final String type ;
337
+ final String id ;
338
+
339
+ RecordIdentifier (String type , String id ) {
340
+ this .type = type ;
341
+ this .id = id ;
342
+ }
343
+ }
344
+
345
+ static RecordIdentifier deserializeRecordIdentifer (JSONObject jsonObject )
346
+ throws JSONException
347
+ {
348
+ String recordType ;
349
+ String recordID ;
350
+ try {
351
+ recordType = jsonObject .getString (RecordSerializationRecordTypeKey );
352
+ recordID = jsonObject .getString (RecordSerializationRecordIDKey );
353
+ } catch (JSONException e ) {
354
+ String typedId = jsonObject .getString (RecordSerializationDeprecatedIDKey );
355
+ String [] split = typedId .split ("/" , 2 );
356
+
357
+ if (split .length != 2 || split [0 ].length () == 0 || split [1 ].length () == 0 ) {
358
+ throw new JSONException (String .format (
359
+ "%s and / or %s is malformed" ,
360
+ RecordSerializationRecordTypeKey ,
361
+ RecordSerializationRecordIDKey
362
+ ));
363
+ }
364
+
365
+ recordType = split [0 ];
366
+ recordID = split [1 ];
367
+ }
368
+
369
+ return new RecordIdentifier (recordType , recordID );
370
+ }
322
371
}
0 commit comments