Morpheus is a JSONAPI deserializer for android that uses java reflection. You can define your own java classes to deserialize.
Take a look at the documentation.
compile 'com.xamoom.android:morpheus:0.5.2'
Prepare your resources
- extend Resource
- Use @SerializedName() annotation when your field name differs from the json.
- Create relationship mapping with the @Relationship() annotation.
public class Article extends Resource {
@SerializedName("article-title")
private String title;
@Relationship("author")
private Author author;
@Relationship("comments")
private List<Comment> comments;
}
- Create a Morpheus instance
- Register your resources
- parse your JSON string
Morpheus morpheus = new Morpheus();
//register your resources
Deserializer.registerResourceClass("articles", Article.class);
Deserializer.registerResourceClass("people", Author.class);
Deserializer.registerResourceClass("comments", Comment.class);
JsonApiObject jsonApiObject =
morpheus.parse(loadJSONFromAsset(R.raw.articles));
Article article = (Article)jsonApiObject.getResources().get(0);
Log.v(TAG, "Article Id: " + article.getId())
Morpheus morpheus = new Morpheus();
Deserializer.registerResourceClass("products", Product.class);
JsonApiObject jsonApiObject = new JsonApiObject();
jsonApiObject.setResource(product);
String json = morpheus.createJson(jsonApiObject, false);
Delete an relationship:
article.addRelationshipToNull("author");
JsonApiObject jsonApiObject = new JsonApiObject();
jsonApiObject.setResource(article);
String articleJson = morpheus.createJson(jsonApiObject, false);
Morpheus can:
- deserialize data object or array
- deserialize relationships
- map includes to relationships
- deserialize links, meta, errors
- serialize resources with their relationships and includes
At the moment Morpheus maps
- Strings ->
String
- Floats ->
double
- Booleans ->
boolean
- JSONArrays ->
List<Object>
(with Gson) - JSONObject ->
HashMap<String, Object>
(with Gson)
You can write your own AttributeMapper by extending AttributeMapper.java
and initialize Morpheus with your mapper.
If you want to contribute: make your changes and do a pull request.