Last active
February 21, 2017 02:21
-
-
Save seventhmoon/d8f0847a2d42fae5358800ba7b7b7693 to your computer and use it in GitHub Desktop.
Helper function for casting / parsing Record when using Skygear
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import java.util.Map; | |
import io.skygear.skygear.Record; | |
public class RecordHelper { | |
public static <T> T toObject(Record record, Class<T> classOfT) { | |
Gson gson = new Gson(); | |
String json = toJson(record); | |
return gson.fromJson(json, classOfT); | |
} | |
public static String toJson(Record record) { | |
Gson gson = new Gson(); | |
Map<String, Object> root = record.getData(); | |
Map<String, Object> firstLevel = record.getTransient(); | |
for (String key : firstLevel.keySet()) { | |
Record r = (Record) firstLevel.get(key); | |
root.put(key, r.getData()); | |
} | |
return gson.toJson(root); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment