在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:faogustavo/JSONApi开源软件地址:https://github.com/faogustavo/JSONApi开源编程语言:Java 100.0%开源软件介绍:================================================================================================================================================================= A simple way to implement JSONApi specifications to convert Models to Json and Json to Models. INSTALLAdd this dependecy from jCenter: compile 'com.gustavofao:JSONApi:${latestVersion}' If the installation fails, add this line to your top level gradle: maven { url "http://dl.bintray.com/faogustavo/maven" } USAGEThe first step to use the library is to initiate the deserializer with your classes. To show how it works, we will use the default JSON that is on jsonapi.org homepage and on raw folder. FIRST STEP - Create your modelsAll models to be converted need to:
import com.gustavofao.jsonapi.Annotatios.Type;
import com.gustavofao.jsonapi.Models.Resource;
@Type("comments")
public class Comment extends Resource {
private String body;
private Person author;
/*
Important: If you leave this out, de-serialisation might fail "... has no zero argument constructor"
If you use proguard minify, add the rule mentioned in the end of this page.
*/
public Comment() {}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Person getPerson() {
return author;
}
public void setPerson(Person author) {
this.author = author;
}
} SECOND STEP - Instantiate the JSONApiConverterThe JSONApiConverter have to be instantiated with all your models. JSONApiConverter api = new JSONApiConverter(Article.class, Comment.class, Person.class); THIRD STEP - Serialize or deserializeSERIALIZE INTO JSONTo serialize one object, it have to be an instance or inherit from Resource and have to be passed as parameter to toJson. The return will be a String with the JSON. Article article = new Article();
//
// SET VALUES HERE
//
String jsonValue = api.toJson(article);
DESERIALIZE FROM JSONTo deserialize the JSON, you have to pass it as parameter for fromJson method. The return will be an JSONApiObject. JSONApiObject<Article> obj = api.fromJson(json);
if (obj.getData().size() > 0) {
//Success
if (obj.getData().size() == 1) {
//Single Object
Article article = obj.getData(0);
} else {
//List of Objects
List<Article> resources = obj.getData();
}
} else {
//Error or empty data
}
TIPSONE-TO-MANY RELATIONTo handle with one-to-many relation you have to use JSONList with the type of the Object. Example below. CHANGE SERIALIZATION NAMETo change the name of the object on the JSON you can use the Annotation SerialName on your field. Example below. IGNORE FIELDSTo ignore fields of the model you have to use the Annotation Excluded on your field. Example below. import com.gustavofao.jsonapi.Annotatios.Excluded;
import com.gustavofao.jsonapi.Annotatios.SerialName;
import com.gustavofao.jsonapi.Annotatios.Type;
import com.gustavofao.jsonapi.Models.JSONList;
import com.gustavofao.jsonapi.Models.Resource;
@Type("articles")
public class Article extends Resource {
private String title;
private JSONList<Comment> comments;
@Excluded
private String blah;
@SerialName("author")
private Person person;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public JSONList<Comment> getComments() {
return comments;
}
public void setComments(JSONList<Comment> comments) {
this.comments = comments;
}
} MULTIPLE TYPE TO SAME OBJECTWhen you have different types for the same object you can use the annotation @Types(String[] value). @Types({"test", "test02"}) METADATAMeta data can be retrieved from the JSONApiObject using getMeta() JSONApiObject<Article> obj = api.fromJson(json);
JSONObject meta = obj.getMeta(); ERRORSThe documentation from errors can be found in this link. To handle with it you have to check your JSONApiObject if it hasErrors(). JSONApiObject obj = api.fromJson(json);
if (obj.hasErrors()) {
List<ErrorModel> errorList = obj.getErrors();
//Do Something with the errors
} else {
//Handle with success conversion
} The attributes from ErrorModel are: private String status;
private String title;
private String detail;
private ErrorSource source; And from ErrorSource: private String pointer;
private String parameter; RetrofitThe library has integration with Retrofit. To use you have to pass the JSONConverterFactory as converterFactory and Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(JSONConverterFactory.create(Article.class, Comment.class, Person.class))
.baseUrl(url)
.build(); All requests have to be with parameter from server JSONApiObject. Call<JSONApiObject> obj = service.testRequest();
obj.enqueue(new Callback<JSONApiObject>() {
@Override
public void onResponse(Call<JSONApiObject> call, Response<JSONApiObject> response) {
if (response.body() != null) {
if (response.body().hasErrors()) {
List<ErrorModel> errorList = response.body().getErrors();
//Do something with the errors
} else {
if (response.body().getData().size() > 0) {
Toast.makeText(MainActivity.this, "Object With data", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No Items", Toast.LENGTH_SHORT).show();
}
}
} else {
try {
JSONApiObject object = App.getConverter().fromJson(response.errorBody().string());
handleErrors(object.getErrors());
} catch (IOException e) {
Toast.makeText(MainActivity.this, "Empty Body", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<JSONApiObject> call, Throwable t) {
Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT).show();
}
}); SERIALIZATION MAPSAt this moment we can do the mapping listed above (java -> Json):
PROGUARDIf you have
Next steps
License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论