jsonapi.org Realm Android 的标准配置



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

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

{
"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转换器的文档,您就会了解它是如何工作的。棘手的部分是领域需要其所有对象扩展领域对象或领域模块我尝试在 jasminb/jsonapi 转换器中覆盖链接和链接不起作用,所以我所做的在产品模型中可以看到。有2个类

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

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

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 的代码。

几乎所有看过这段代码的人(包括我、@Dalinaum和@EpicPandaForce(都建议你立即将 JSON 和 Realm 表示分开:将 JSON 读入 JSON 对象,并在 JSON 对象上添加一个toRealm方法,或者在 Realm 对象上添加一个fromJson方法(或构造函数(。

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

我们都同意你正在做的事情是聪明的,并且会奏效。 我们都建议你以不同的方式做!

最新更新