Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RealmとJSONライブラリ

zaki50
June 25, 2015

 RealmとJSONライブラリ

zaki50

June 25, 2015
Tweet

More Decks by zaki50

Other Decks in Technology

Transcript

  1. RealmͱJSONϥΠϒϥϦ 2015/6/25 Realm Meetup ࢁ㟒 ੣ (@zaki50)

  2. ࣗݾ঺հ • YAMAZAKI Makoto (@zaki50) • גࣜձࣾ uPhyca ॴଐ

  3. ࠓ೔ͷ಺༰ • Realm ϞσϧΛ JSON ʹγϦΞϥΠζ͢Δ࿩ • Android(Java)Ͱ͢ • ϝδϟʔͳJSONϥΠϒϥϦͱRealmͷ૬ੑ

    • ͦΕͧΕͷ࢖͍ํ
  4. JSONϥΠϒϥϦ • Moshi • Gson • Jackson • JsonPullParser

  5. ૬ੑʁ • RealmͷΦϒδΣΫτ͕POJOͰ͸ແ͍ ͜ͱʹىҼ͢Δ༷ʑͳ໰୊͕ى͜Δ

  6. ϞσϧΫϥε(User) public class User extends RealmObject {
 
 private String

    email;
 private String fullName;
 private Phone phone;
 
 public User() {}
 
 public User(String email, String fullName) {
 this.email = email;
 this.fullName = fullName;
 }
 // getterとsetterは省略
 }
  7. ϞσϧΫϥε(Phone) public class Phone extends RealmObject {
 
 private String

    os;
 private String type; 
 public Phone() {}
 
 public Phone(String os, String type) {
 this.os = os;
 this.type = type;
 } // getterとsetterは(ry
 }
  8. Moshi • https://github.com/square/moshi • Squareࣾ੡ • okioͱ࿈ܞͰ͖ΔͷͰɺOkHttpͱ࢖͏ ͱޮ཰͕ྑ͍ • ޙड़ͷGsonͱࣅͨAPI

  9. MoshiͰγϦΞϥΠζ User user = new User("zaki@example.com", "Makoto Yamazaki");
 user(new Phone("Android",

    "HT-03A"));
 Moshi moshi = new Moshi.Builder().build(); try { JsonAdapter<User> adapter = moshi.adapter(User.class);
 Log.d("json", adapter.toJson(user)); } catch (Exception e) {
 Log.d("json", "Moshi: error", e);
 }
  10. ࣮ߦ݁Ռ(Moshi) D/json (16011): java.lang.IllegalArgumentException: No JsonAdapter for java.lang.ref.WeakReference<io.realm.RealmChangeListener> annotated []

    D/json (16011): at com.squareup.moshi.Moshi.createAdapter(Moshi.java:93) D/json (16011): at com.squareup.moshi.Moshi.adapter(Moshi.java:55) D/json (16011): at com.squareup.moshi.Moshi.adapter(Moshi.java:46)
  11. ݪҼ public class User extends RealmObject {
 // 省略
 }

  12. RealmObject public abstract class RealmObject {
 protected Row row;
 protected

    Realm realm;
 // 省略 } public final class Realm implements Closeable {
 // 省略 private List<WeakReference<RealmChangeListener>> changeListeners = ...; // 省略 }
  13. ݁࿦(Moshi) • Moshi͸ର৅ΦϒδΣΫτͷશϑΟʔϧ υΛγϦΞϥΠζ͠Α͏ͱͯ͠͠·͏ɻ • আ֎ࢦఆ͕Ή͔͍ͣ͠ • Realmͱͷ૬ੑ ☓

  14. Gson • https://github.com/google/gson • Googleࣾ੡ • AndroidͷαϯϓϧͰ͸Α͘࢖༻͞Εͯ ͍Δ • ϝδϟʔΏ͑ͷ҆৺ײ

    • RealmެࣜαΠτͰ΋ݴٴ͞Ε͍ͯΔ
  15. Realm ެࣜαΠτ https://realm.io/docs/java/latest/#gson

  16. GsonͰγϦΞϥΠζ Gson gson = new GsonBuilder()
 .setExclusionStrategies(new ExclusionStrategy() {
 @Override


    public boolean shouldSkipField(FieldAttributes f) {
 return f.getDeclaringClass().equals(RealmObject.class);
 }
 
 @Override
 public boolean shouldSkipClass(Class<?> clazz) {
 return false;
 }
 }).create();
 
 User user = new User("zaki@example.com", "Makoto Yamazaki");
 user(new Phone("Android", "HT-03A"));
 Log.d("json", "Gson(stand alone): " + gson.toJson(user));
  17. ࣮ߦ݁Ռ(Gson) D/json (16011):Gson(stand alone): {"email":"zaki@example.com","fullName":"Makoto Yamazaki","phone":{"os":"Android","type":"HT-03A"}}

  18. None
  19. GsonͰγϦΞϥΠζ2 Gson gson = new GsonBuilder()
 .setExclusionStrategies(new ExclusionStrategy() { //

    省略
 }).create();
 
 User user = new User("zaki@example.com", "Makoto Yamazaki");
 user(new Phone("Android", “HT-03A")); 
 realm.beginTransaction();
 {
 user = realm.copyToRealm(standAloneUser);
 }
 realm.commitTransaction();
 Log.d("json", "Gson(managed): " + gson.toJson(user));
  20. ࣮ߦ݁Ռ2(Gson) D/json (16011):Gson(managed): {}

  21. None
  22. stand-alone vs managed • Stand-alone Object: σʔλϕʔεʹඥ෇ ͍͍ͯͳ͍ΦϒδΣΫτɻgetter ͸಺෦ ͷprivateϑΟʔϧυ͔Β஋Λฦ͢

    • Managed Object: σʔλϕʔεʹඥ෇͍ ͍ͯΔΦϒδΣΫτɻgetter͸ɺσʔλ ϕʔεʹ֨ೲ͞Ε͍ͯΔ஋Λฦ͢
  23. ݪҼ • Gson͸getterΛ࢖ͬͯ͘Εͳ͍ʂ • RealmެࣜαΠτʹ΋ɺγϦΞϥΠζ͢ΔͳΒ ΧελϜͷTypeAdapterΛϞσϧΫϥε͝ͱʹ هड़͠Ζͱ͔͍ͯ͋Δ https://gist.github.com/cmelchior/ ddac8efd018123a1e53a

  24. TypeAdapterͷྫ public class PersonSerializer implements JsonSerializer<Person> { @Override public JsonElement

    serialize(Person src, Type typeOfSrc, JsonSerializationContext context) { final JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("name", src.getName()); jsonObject.addProperty("age", src.getAge()); jsonObject.add(“favoriteDog", context.serialize(src.getFavoriteDog())); jsonObject.add("dogs", context.serialize(src.getDogs())); return jsonObject; } }
  25. ݁࿦(Gson) • Gson͸ඪ४Ͱ͸getter/setterΛ࢖Θͳ ͍ͷͰɺmanaged objectͰࠔΔ • શϞσϧΫϥεʹTypeAdapterॻ͘ͳ͍ ΒJsonObjectͰ࣮૷͢ΔͷͱมΘΒͳ͍ • Realmͱͷ૬ੑ˚

  26. Jackson • http://wiki.fasterxml.com/ JacksonHome • ΘΓͱߴ଎ͳ͜ͱͰ༗໊ • JavaEEք۾Ͱͷ࣮੷΋͋Γɺαʔόʔ αΠυͰ͸ϝδϟʔ

  27. JacksonͰγϦΞϥΠζ User user = new User("zaki@example.com", "Makoto Yamazaki");
 user(new Phone("Android",

    "HT-03A"));
 final ObjectMapper mapper = new ObjectMapper();
 
 Log.d("json", "Jackson(stand alone): " + mapper.writeValueAsString(user)); 
 realm.beginTransaction();
 {
 user = realm.copyToRealm(standAloneUser);
 }
 realm.commitTransaction();
 
 Log.d("json", "Jackson(managed): " + mapper.writeValueAsString(user));

  28. ࣮ߦ݁Ռ(Jsckson) D/json (16011): Jackson(stand alone): {"email":"zaki@example.com","fullName":"Makoto Yamazaki”,”phone": {"os":"Android","type":"HT-03A","valid":false}, "valid":false} D/json

    (16011): Jackson(managed): {"email":"zaki@example.com","fullName":"Makoto Yamazaki","phone": {"os":"Android","type":"HT-03A","valid":true}," valid":true}
  29. None
  30. Μʁ

  31. ࣮ߦ݁Ռ(Jackson) D/json (16011): Jackson(stand alone): {"email":"zaki@example.com","fullName":"Makoto Yamazaki”,"phone": {"os":"Android","type":"HT-03A","valid":false}, "valid":false} D/json

    (16011): Jackson(managed): {"email":"zaki@example.com","fullName":"Makoto Yamazaki","phone": {"os":"Android","type":"HT-03A","valid":true}," valid":true}
  32. ݪҼ • Jackson͸getterΛ࢖ͬͯ͘ΕΔ • ͔͠͠ɺ਌Ϋϥε΋ؚΊͯgetterͬΆ͍ϝιου ͕͋Δͱશ෦࢖͏ public abstract class RealmObject

    { // 省略 public boolean isValid() {
 return row != null && row.isAttached();
 }
 }
  33. ରॲํ๏ @JsonIgnoreProperties({"valid"}) public class User extends RealmObject {
 
 private

    String email;
 private String fullName;
 private Phone phone;
 
 // 省略
 } • “valid”͕ͳΜͷ͜ͱ͔Θ͔Γʹ͍͘ • কདྷతʹRealmObjectʹϝιου͕૿͕͑ͨ࣌৺഑
  34. ݁࿦(Jackson) • Jackson͸getterΛ࢖ͬͯ͘ΕΔͷͰmanaged objectͰ΋γϦΞϥΠζͰ͖Δ • ͔͠͠ɺRealmObject͕΋ͭgetter΋࢖ͬͯ͠· ͏ͨΊ༨ܭͳ΋ͷ͕ࠞೖ͢Δ • গ͠ͷखؒͰճආ͸Մೳ͕ͩόʔδϣϯΞοϓ ͕৺഑

    • Realmͱͷ૬ੑ ̋
  35. JsonPullParser(JPP) • https://github.com/vvakame/JsonPullParser • TOPGATE͕ࣾތΔAndroidΤΩεύʔτvvakameࢯ࡞ • ͚ͬ͜͏ߴ଎ • ݩʑ͸PullParser͕ͩͬͨɺΦϒδΣΫτϚοϐϯά ΋Ͱ͖Δ(ͱ͍͏͔ɺ΄΅ͦΕ͔͠࢖͍ͬͯͳ͍)

  36. JPPͰγϦΞϥΠζ User user = new User("zaki@example.com", "Makoto Yamazaki");
 user(new Phone("Android",

    "HT-03A"));
 StringWriter writer = new StringWriter();
 UserGen.encode(writer, user);
 writer.close();
 
 Log.d("json", "JPP(stand alone): " + writer.toString());
 
 realm.beginTransaction();
 user = realm.copyToRealm(standAloneUser);
 realm.commitTransaction();
 
 writer = new StringWriter();
 UserGen.encode(writer, user);
 writer.close();
 
 Log.d("json", "JPP(managed): " + writer.toString());
  37. ϞσϧΫϥε(User) @JsonModel public class User extends RealmObject { @JsonKey private

    String email; @JsonKey private String fullName; @JsonKey private Phone phone;
 
 // 省略
 }
  38. ઃఆ͕ͪΐͬͱ໘౗ buildscript {
 ...
 dependencies {
 classpath 'com.android.tools.build:gradle:1.2.3'
 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'


    }
 }
 apply plugin: 'com.android.application'
 apply plugin: 'com.neenbedankt.android-apt'
 ... dependencies { ...
 compile ‘net.vvakame:jsonpullparser-core:1.6.2' apt ‘net.vvakame:jsonpullparser-apt:1.6.2' ...
 }
  39. ࣮ߦ݁Ռ(JPP) D/json (16011): JPP(stand alone): {"email":"zaki@example.com","fullName":"Makoto Yamazaki”,"phone": {"os":"Android","type":"HT-03A"}} D/json (16011):

    JPP(managed): {"email":"zaki@example.com","fullName":"Makoto Yamazaki","phone": {"os":"Android","type":"HT-03A"}}
  40. None
  41. ݁࿦(JPP) • JPP͸getterΛ࢖ͬͯ͘ΕΔͷͰmanaged object Ͱ΋γϦΞϥΠζͰ͖Δ • ର৅ͱ͢ΔϑΟʔϧυʹΞϊςʔγϣϯΛ͚ͭΔ ͷͰɺ஌Βͳ͍͏ͪʹ༨ܭͳ΋ͷ͕ࠞೖ͠ͳ͍ • ϓϩδΣΫτઃఆ͕গ͚ͩ͠໘౗

    • Realmͱͷ૬ੑ ̋
  42. ݁࿦ Moshi<Gson<Jackson<JPP Realmͱͷ૬ੑͰ͸ Έͳ͞ΜJPPΛ࢖͍͖ͬͯ·͠ΐ͏ʂ

  43. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

  44. αϯϓϧίʔυ https://github.com/zaki50/RealmMeetup4