【问题标题】:jsonapi.org standard with Realm Androidjsonapi.org 标准与 Realm Android
【发布时间】:2017-12-27 00:13:23
【问题描述】:

我想解析遵循jsonapi.org标准的json

我正在使用 jsonapi-converter 进行转换器,并且也想实现 Realm。我无法得到任何想法或线索如何实现两者。如果有人这样做,您的帮助将不胜感激。提前致谢。

【问题讨论】:

  • 使用 jsonapi-converter 的类可以扩展 RealmObject 并使用 Realm 的注解吗?如果可能的话,它们可以混合在一起。否则,我认为您应该将 jsonapi-converter 对象转换为 JsonObject、JSON 字符串或 Realm 对象。
  • @Dalinaum 我正在努力。使用 jsonapi-converter 我可以解析和扩展 RealmObject 但关于领域注释我正在测试它。
  • 最好将 API 响应和数据库对象分开,在这种情况下,如何做到这两点真的不是问题。
  • @EpicPandaForce 你的意思是响应对象和领域对象应该是分开的???例如,如果我正在获取用户数据,则响应和数据库对象可以与用户相同。我猜。
  • 嘿@ArjunGurung,我们在哪里?如果你有解决方案,你会分享吗?

标签: android realm json-api


【解决方案1】:

几乎所有查看过此代码的人(包括我,@Dalinaum 和 @EpicPandaForce)都立即建议您将 JSON 和 Realm 表示分开:将 JSON 读入 JSON 对象并在JSON 对象,或 Realm 对象上的 fromJson 方法(或构造函数)。

尝试在同一个对象中表示两个外部接口会将两个表示联系在一起,并且可能不是很好的关注点分离。

我们都同意您所做的事情很聪明并且会奏效。我们都建议您以不同的方式进行操作!

【讨论】:

  • 这样做肯定会分离领域和 json 之间的联系,但这样做会导致创建许多模型。那么拥有更多的模型类然后将它们捆绑在一起会更好吗?我会尝试一次。感谢您的评论。
【解决方案2】:
{
   "data":[
      {
         "type":"product",
         "id":"1",
         "attributes":{
            "title":"Wai Wai Noodles",
            "description":"",
            "liked":true,
            "is-active":true
         },
         "links":{
            "self":"https://wongel.info/product/1"
         },
         "relationships":{
            "productinfo":{
               "links":{
                  "self":"https://wongel.info/product/1/relationships/productinfo",
                  "related":"https://wongel.info/product/1/productinfo"
               },
               "data":{
                  "type":"productinfo",
                  "id":"1"
               }
            }
         }
      }
   ],
   "included":[
      {
         "type":"productinfo",
         "id":"1",
         "attributes":{
            "product-category":null,
            "description":null
         },
         "links":{
            "self":"https://wongel.info/productinfo/1"
         }
      }
   ]
}

这是我的 json,我必须按照 jsonApi.Org 标准进行解析。我正在使用 jasminb/jsonapi-converter 进行领域解析。所以这是我的产品类和产品信息类

@Type("product")
@JsonIgnoreProperties(ignoreUnknown = true)
public class Product extends RealmObject {
    @Id
    @PrimaryKey
    @JsonProperty("id")
    private String id;
    @JsonProperty("title")
    private String title;
    @JsonProperty("description")
    private String description;
    @JsonProperty("is_active")
    private boolean isActive;
    @JsonProperty("liked")
    private boolean liked;

    @Relationship("productinfo")
    private ProductInfo productInfo;

    @Ignore
    @Links
    @JsonProperty("links")
    private com.github.jasminb.jsonapi.Links links;
    @JsonIgnore
    private RealmList<JsonMap> linkList;

    @Ignore
    @RelationshipLinks("productinfo")
    private com.github.jasminb.jsonapi.Links productLinks;
    @JsonIgnore
    private RealmList<JsonMap> productLinkList;

    //setter getter

    public void migrateToList() {
        linkList = GlobalUtils.getMap(links);
        productLinkList = GlobalUtils.getMap(productLinks);
    }

    public void migrateToLink() {
        links = GlobalUtils.getMap(linkList);
        productLinks = GlobalUtils.getMap(productLinkList);
    }
}

产品信息

@Type("productinfo")
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductInfo extends RealmObject {
    @Id
    @PrimaryKey
    @JsonProperty("id")
    private String id;
    @JsonProperty("product-category")
    private String category;
    @JsonProperty("description")
    private String description;

//setter getter
}

用于保存链接数据的JsonMap

public class JsonMap extends RealmObject {
    private String key;
    private String value;

    public JsonMap() {

    }

    public JsonMap(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public String getKey() {
        return key;
    }

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

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

因此,一旦阅读了 jasminb/jsonapi-converter 的文档,您就会了解它是如何工作的。棘手的部分是领域需要其所有对象扩展领域对象或领域模块我尝试在 jasminb/jsonapi-converter 中覆盖链接和链接没有工作,所以我所做的可以在产品模型中看到。有2个班

public void migrateToList() {
        linkList = GlobalUtils.getMap(links);
        productLinkList = GlobalUtils.getMap(productLinks);
    }

    public void migrateToLink() {
        links = GlobalUtils.getMap(linkList);
        productLinks = GlobalUtils.getMap(productLinkList);
    }

一个帮助将数据从链接迁移到领域支持的realmList,另一个帮助从列表迁移到调用api等所需的链接。

ResourceConverter converter = new ResourceConverter(ProductR.class, ProductInfo.class);
        converter.enableSerializationOption(SerializationFeature.INCLUDE_RELATIONSHIP_ATTRIBUTES);

        JSONAPIConverterFactory converterFactory = new JSONAPIConverterFactory(converter);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL)
                .addConverterFactory(converterFactory)
                .build();
        ApiService apiService = retrofit.create(ApiService.class);

        Call<JSONAPIDocument<List<Product>>> call = apiService.getProducts();

        call.enqueue(new Callback<JSONAPIDocument<List<Product>>>() {
            @Override
            public void onResponse(Call<JSONAPIDocument<List<Product>>> call, Response<JSONAPIDocument<List<Product>>> response) {
                if (response.isSuccessful()) {
                    // success
                } else {
                    //error body
                }

            }

            @Override
            public void onFailure(Call<JSONAPIDocument<List<Product>>> call, Throwable t) {
                //failed
            }
        });

上面调用服务器api的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    • 1970-01-01
    相关资源
    最近更新 更多