Skip to content

Commit ca7dbca

Browse files
authored
Add toJSON() and toMap() support to AccountOwnedModel superclass (#150)
This PR adds a new helper feature that allows all classes that extend AccountOwnedModel to serialize to either a JSON string or a Map representation of the object's fields.
1 parent 631d55b commit ca7dbca

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
11
package com.nylas;
22

3+
import java.util.Map;
4+
35
public abstract class AccountOwnedModel extends RestfulModel {
46

57
private String account_id;
68

79
public String getAccountId() {
810
return account_id;
911
}
12+
13+
/**
14+
* Converts the model to a JSON string
15+
* @return JSON string representation of the object
16+
*/
17+
public String toJSON() {
18+
return JsonHelper.objectToJson(AccountOwnedModel.class, this);
19+
}
20+
21+
/**
22+
* Converts the model to a Map
23+
* @return Map representation of the object
24+
*/
25+
@SuppressWarnings("unchecked")
26+
public Map<String, ? extends AccountOwnedModel> toMap() {
27+
return (Map<String, ? extends AccountOwnedModel>) JsonHelper.adapter(AccountOwnedModel.class).toJsonValue(this);
28+
}
1029

1130
}

src/main/java/com/nylas/JobStatus.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ public String getJobObjectId() {
107107
return super.getId();
108108
}
109109

110+
/**
111+
* {@inheritDoc}
112+
*/
113+
@Override
114+
public String toJSON() {
115+
return JsonHelper.objectToJson(JobStatus.class, this);
116+
}
117+
118+
/**
119+
* {@inheritDoc}
120+
*/
121+
@SuppressWarnings("unchecked")
122+
@Override
123+
public Map<String, JobStatus> toMap() {
124+
return (Map<String, JobStatus>) JsonHelper.adapter(JobStatus.class).toJsonValue(this);
125+
}
126+
110127
@Override
111128
public String toString() {
112129
return "JobStatus [" +

src/main/java/com/nylas/JsonHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public static <T> T fromJsonUnchecked(JsonAdapter<T> adapter, String json) {
6969
private static final JsonAdapter<List<Object>> listAdapter
7070
= moshi.<List<Object>>adapter(List.class).indent(" ");
7171

72+
public static String objectToJson(Class<?> cls, Object obj) {
73+
return adapter(cls).toJson(obj);
74+
}
75+
7276
public static String mapToJson(Map<String, Object> map) {
7377
return mapAdapter.toJson(map);
7478
}

src/main/java/com/nylas/delta/Delta.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public String toString() {
4141
// Adapter for identifying the class of object in the delta to serialize to
4242
public static final JsonAdapter.Factory ACCOUNT_OWNED_MODEL_JSON_FACTORY
4343
= PolymorphicJsonAdapterFactory.of(AccountOwnedModel.class, "object")
44+
.withSubtype(Account.class, "account")
45+
.withSubtype(Calendar.class, "calendar")
46+
.withSubtype(RoomResource.class, "room_resource")
4447
.withSubtype(Contact.class, "contact")
4548
.withSubtype(File.class, "file")
4649
.withSubtype(Message.class, "message")

0 commit comments

Comments
 (0)