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. ϞσϧΫϥε(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は省略
 }
  2. ϞσϧΫϥε(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
 }
  3. MoshiͰγϦΞϥΠζ User user = new User("[email protected]", "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);
 }
  4. ࣮ߦ݁Ռ(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)
  5. RealmObject public abstract class RealmObject {
 protected Row row;
 protected

    Realm realm;
 // 省略 } public final class Realm implements Closeable {
 // 省略 private List<WeakReference<RealmChangeListener>> changeListeners = ...; // 省略 }
  6. 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("[email protected]", "Makoto Yamazaki");
 user(new Phone("Android", "HT-03A"));
 Log.d("json", "Gson(stand alone): " + gson.toJson(user));
  7. GsonͰγϦΞϥΠζ2 Gson gson = new GsonBuilder()
 .setExclusionStrategies(new ExclusionStrategy() { //

    省略
 }).create();
 
 User user = new User("[email protected]", "Makoto Yamazaki");
 user(new Phone("Android", “HT-03A")); 
 realm.beginTransaction();
 {
 user = realm.copyToRealm(standAloneUser);
 }
 realm.commitTransaction();
 Log.d("json", "Gson(managed): " + gson.toJson(user));
  8. stand-alone vs managed • Stand-alone Object: σʔλϕʔεʹඥ෇ ͍͍ͯͳ͍ΦϒδΣΫτɻgetter ͸಺෦ ͷprivateϑΟʔϧυ͔Β஋Λฦ͢

    • Managed Object: σʔλϕʔεʹඥ෇͍ ͍ͯΔΦϒδΣΫτɻgetter͸ɺσʔλ ϕʔεʹ֨ೲ͞Ε͍ͯΔ஋Λฦ͢
  9. 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; } }
  10. JacksonͰγϦΞϥΠζ User user = new User("[email protected]", "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));

  11. ରॲํ๏ @JsonIgnoreProperties({"valid"}) public class User extends RealmObject {
 
 private

    String email;
 private String fullName;
 private Phone phone;
 
 // 省略
 } • “valid”͕ͳΜͷ͜ͱ͔Θ͔Γʹ͍͘ • কདྷతʹRealmObjectʹϝιου͕૿͕͑ͨ࣌৺഑
  12. JPPͰγϦΞϥΠζ User user = new User("[email protected]", "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());
  13. ϞσϧΫϥε(User) @JsonModel public class User extends RealmObject { @JsonKey private

    String email; @JsonKey private String fullName; @JsonKey private Phone phone;
 
 // 省略
 }
  14. ઃఆ͕ͪΐͬͱ໘౗ 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' ...
 }