modelToJsonでInverseModelListRefを扱う

2012-08-18T00:00:00+00:00 Java

package server_director.model;

import java.io.Serializable;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.InverseModelListRef;
import org.slim3.datastore.Model;

import server_director.meta.ServerMeta;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;

@Model
public class Profile implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey = true)
    private Key key;

    @Attribute(persistent = true)
    private User user;

    @Attribute(persistent = false)
    private InverseModelListRef<Server, Profile> servers = new InverseModelListRef<Server, Profile>(
        Server.class,
        ServerMeta.get().profileRef.getName(),
        this
    );

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public InverseModelListRef<Server, Profile> getServers() {
        return servers;
    }
}

みたいなのがあって、これをmodelToJsonを使ってJSON化するとInverseModelListRefなのはエンコードしたJSONに含まれない(ModelRefの場合はKeyが出るらしい)

でこれをやる方法がちゃんとあるみたいで以下のようにJsonアノテーションを設定する

package server_director.model;

import java.io.Serializable;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.InverseModelListRef;
import org.slim3.datastore.Model;
import org.slim3.datastore.json.Expanded;
import org.slim3.datastore.json.Json;

import server_director.meta.ServerMeta;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.users.User;

@Model
public class Profile implements Serializable {

    private static final long serialVersionUID = 1L;

    @Attribute(primaryKey=true)
    private Key key;

    @Attribute(persistent=true)
    @Json(ignore=true)
    private User user;

    @Attribute(persistent=false)
    @Json(ignore=false, coder=Expanded.class)
    private InverseModelListRef<Server, Profile> servers = new InverseModelListRef<Server, Profile>(
        Server.class,
        ServerMeta.get().profileRef.getName(),
        this
    );

    public Key getKey() {
        return key;
    }

    public void setKey(Key key) {
        this.key = key;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public InverseModelListRef<Server, Profile> getServers() {
        return servers;
    }
}

Userなのはignore=trueでJSONに含ませない、んでInverseModelListRefなのはignore=falseでcoderをExpanded(デフォルトはorg.slim3.datastore.json.Default?)にする

な感じでmodelToJsonに流せばよろしい模様。但し、modelToJson(Object model, int maxDepth)を使用してmaxDepthを1以上にする事らしい

参考1: http://groups.google.com/group/slim3-developer/browse_thread/thread/25d8c86791381877

参考2: https://plus.google.com/101049791196889453404/posts/DM6CP3grMo5

Slim3でrespond_toもどき Chrome Extension開発を勉強してみる (16) - chrome.extension.connect -