Skip to content

Commit a78ba3d

Browse files
author
Ben Lei
committed
Refactor different serializers
1 parent 55c3db1 commit a78ba3d

File tree

6 files changed

+178
-100
lines changed

6 files changed

+178
-100
lines changed

skygear/src/androidTest/java/io/skygear/skygear/RecordSerializerUnitTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ public void testRecordDeserializationDeprecatedFlow() throws Exception {
440440
assertTrue(record.isPublicWritable());
441441
}
442442

443-
@Test(expected = InvalidParameterException.class)
443+
@Test(expected = JSONException.class)
444444
public void testRecordDeserializationNotAllowNoId() throws Exception {
445445
JSONObject jsonObject = new JSONObject();
446446
jsonObject.put("_recordType", "Note");

skygear/src/androidTest/java/io/skygear/skygear/ReferenceSerializerUnitTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ public void testReferenceFormatChecking() throws Exception {
6969
assertFalse(ReferenceSerializer.isReferenceFormat(new Object()));
7070
assertFalse(ReferenceSerializer.isReferenceFormat(new JSONObject("{}")));
7171
assertFalse(ReferenceSerializer.isReferenceFormat(
72-
new JSONObject("{\"$type\": \"record\"}")
72+
new JSONObject("{\"$type\": \"not-ref\"}")
7373
));
7474
assertFalse(ReferenceSerializer.isReferenceFormat(
75-
new JSONObject("{\"$type\": \"record\", \"$recordType\": \"Note\"}")
75+
new JSONObject("{\"$type\": \"ref\", \"$recordType\": \"Note\"}")
7676
));
7777
assertFalse(ReferenceSerializer.isReferenceFormat(
78-
new JSONObject("{\"$type\": \"record\", \"$recordID\": \"some-id\"}")
78+
new JSONObject("{\"$type\": \"ref\", \"$recordID\": \"some-id\"}")
7979
));
8080
assertFalse(ReferenceSerializer.isReferenceFormat(
81-
new JSONObject("{\"$type\": \"record\", \"$id\": \"some-id\"}")
81+
new JSONObject("{\"$type\": \"ref\", \"$id\": \"some-id\"}")
8282
));
8383
assertTrue(ReferenceSerializer.isReferenceFormat(
8484
new JSONObject("{" +

skygear/src/androidTest/java/io/skygear/skygear/ValueSerializerUnitTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ public void testSerializeReference() throws Exception {
148148

149149
assertEquals("ref", jsonValue.getString("$type"));
150150
assertEquals("Comment", jsonValue.getString("$recordType"));
151-
assertEquals("" +
152-
"7a7873dc-e14b-4b8f-9c51-948da68e924e",
151+
assertEquals("7a7873dc-e14b-4b8f-9c51-948da68e924e",
153152
jsonValue.getString("$recordID")
154153
);
155154
assertEquals(

skygear/src/main/java/io/skygear/skygear/ErrorSerializer.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
*/
2828
public class ErrorSerializer {
2929

30+
private static final String ErrorSerializationCodeKey = "code";
31+
private static final String ErrorSerializationNameKey = "name";
32+
private static final String ErrorSerializationMessageKey = "message";
33+
private static final String ErrorSerializationExtraInfoKey = "info";
34+
3035
/**
3136
* Serializes an error object
3237
*
@@ -36,13 +41,14 @@ public class ErrorSerializer {
3641
public static JSONObject serialize(Error errorObject) {
3742
try {
3843
JSONObject jsonObject = new JSONObject();
39-
jsonObject.put("message", errorObject.getDetailMessage());
40-
jsonObject.put("code", errorObject.getCodeValue());
41-
jsonObject.put("name", errorObject.getName());
44+
45+
jsonObject.put(ErrorSerializationCodeKey, errorObject.getCodeValue());
46+
jsonObject.put(ErrorSerializationNameKey, errorObject.getName());
47+
jsonObject.put(ErrorSerializationMessageKey, errorObject.getDetailMessage());
4248

4349
JSONObject infoObject = errorObject.getInfo();
4450
if (infoObject != null) {
45-
jsonObject.put("info", infoObject);
51+
jsonObject.put(ErrorSerializationExtraInfoKey, infoObject);
4652
}
4753

4854
return jsonObject;
@@ -59,10 +65,11 @@ public static JSONObject serialize(Error errorObject) {
5965
* @throws JSONException the json exception
6066
*/
6167
public static Error deserialize(JSONObject jsonObject) throws JSONException {
62-
String errorString = jsonObject.optString("message");
63-
int errorCodeValue = jsonObject.getInt("code");
64-
String errorName = jsonObject.optString("name");
65-
JSONObject errorInfo = jsonObject.optJSONObject("info");
68+
69+
int errorCodeValue = jsonObject.getInt(ErrorSerializationCodeKey);
70+
String errorName = jsonObject.optString(ErrorSerializationNameKey);
71+
String errorString = jsonObject.optString(ErrorSerializationMessageKey);
72+
JSONObject errorInfo = jsonObject.optJSONObject(ErrorSerializationExtraInfoKey);
6673

6774
return new Error(errorCodeValue, errorName, errorString, errorInfo);
6875
}

skygear/src/main/java/io/skygear/skygear/RecordSerializer.java

+102-53
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,32 @@
4141
* The Skygear Record Serializer.
4242
*/
4343
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+
4457
private static final String TAG = "Skygear SDK";
4558
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
5770
);
5871

5972
private static Set<? extends Class> CompatibleValueClasses = new HashSet<>(Arrays.asList(
@@ -163,31 +176,40 @@ public static JSONObject serialize(Record record) {
163176
try {
164177
JSONObject jsonObject = RecordSerializer.serialize(record.data);
165178

166-
jsonObject.put("_id", String.format(
179+
jsonObject.put(RecordSerializationDeprecatedIDKey, String.format(
167180
"%s/%s",
168181
record.getType(),
169182
record.getId()
170183
));
171-
jsonObject.put("_recordType", record.getType());
172-
jsonObject.put("_recordID", record.getId());
184+
jsonObject.put(RecordSerializationRecordTypeKey, record.getType());
185+
jsonObject.put(RecordSerializationRecordIDKey, record.getId());
173186
if (record.createdAt != null) {
174-
jsonObject.put("_created_at", DateSerializer.stringFromDate(record.createdAt));
187+
jsonObject.put(
188+
RecordSerializationCreationDatetimeKey,
189+
DateSerializer.stringFromDate(record.createdAt)
190+
);
175191
}
176192
if (record.updatedAt != null) {
177-
jsonObject.put("_updated_at", DateSerializer.stringFromDate(record.updatedAt));
193+
jsonObject.put(
194+
RecordSerializationUpdateDatetimeKey,
195+
DateSerializer.stringFromDate(record.updatedAt)
196+
);
178197
}
179198
if (record.creatorId != null) {
180-
jsonObject.put("_created_by", record.creatorId);
199+
jsonObject.put(RecordSerializationCreatorIDKey, record.creatorId);
181200
}
182201
if (record.updaterId != null) {
183-
jsonObject.put("_updated_by", record.updaterId);
202+
jsonObject.put(RecordSerializationUpdaterIDKey, record.updaterId);
184203
}
185204
if (record.ownerId != null) {
186-
jsonObject.put("_ownerID", record.ownerId);
205+
jsonObject.put(RecordSerializationOwnerIDKey, record.ownerId);
187206
}
188207

189208
if (record.getAccess() != null) {
190-
jsonObject.put("_access", AccessControlSerializer.serialize(record.getAccess()));
209+
jsonObject.put(
210+
RecordSerializationAccessKey,
211+
AccessControlSerializer.serialize(record.getAccess())
212+
);
191213
}
192214

193215
// handle _transient
@@ -203,7 +225,7 @@ public static JSONObject serialize(Record record) {
203225
}
204226
}
205227

206-
jsonObject.put("_transient", transientObject);
228+
jsonObject.put(RecordSerializationTransientKey, transientObject);
207229
}
208230

209231
return jsonObject;
@@ -222,57 +244,41 @@ public static JSONObject serialize(Record record) {
222244
* @throws JSONException the JSON exception
223245
*/
224246
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);
243249

244250
// 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);
247253
record.createdAt = DateSerializer.dateFromString(createdAtString);
248254
}
249255

250256
// 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);
253259
record.updatedAt = DateSerializer.dateFromString(updatedAtString);
254260
}
255261

256262
// 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);
259265
}
260-
if (jsonObject.has("_updated_by")) {
261-
record.updaterId = jsonObject.getString("_updated_by");
266+
if (jsonObject.has(RecordSerializationUpdaterIDKey)) {
267+
record.updaterId = jsonObject.getString(RecordSerializationUpdaterIDKey);
262268
}
263-
if (jsonObject.has("_ownerID")) {
264-
record.ownerId = jsonObject.getString("_ownerID");
269+
if (jsonObject.has(RecordSerializationOwnerIDKey)) {
270+
record.ownerId = jsonObject.getString(RecordSerializationOwnerIDKey);
265271
}
266272

267273
// handle _access
268274
JSONArray accessJsonArray = null;
269-
if (!jsonObject.isNull("_access")) {
270-
accessJsonArray = jsonObject.getJSONArray("_access");
275+
if (!jsonObject.isNull(RecordSerializationAccessKey)) {
276+
accessJsonArray = jsonObject.getJSONArray(RecordSerializationAccessKey);
271277
}
272278

273279
// handle _transient
274-
if (!jsonObject.isNull("_transient")) {
275-
JSONObject transientObject = jsonObject.getJSONObject("_transient");
280+
if (!jsonObject.isNull(RecordSerializationTransientKey)) {
281+
JSONObject transientObject = jsonObject.getJSONObject(RecordSerializationTransientKey);
276282
Iterator<String> transientKeys = transientObject.keys();
277283

278284
while(transientKeys.hasNext()) {
@@ -319,4 +325,47 @@ public static Record deserialize(JSONObject jsonObject) throws JSONException {
319325

320326
return record;
321327
}
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+
}
322371
}

0 commit comments

Comments
 (0)