xamoom / Morpheus

JSONAPI Implementation for Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Morpheus

Download Build Status

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.

Install

compile 'com.xamoom.android:morpheus:0.5.2'

Usage

Prepare your resources

  1. extend Resource
  2. Use @SerializedName() annotation when your field name differs from the json.
  3. 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;
}

Deserialize

  1. Create a Morpheus instance
  2. Register your resources
  3. 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())

Serialize

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);

Development status

Morpheus can:

  • deserialize data object or array
  • deserialize relationships
  • map includes to relationships
  • deserialize links, meta, errors
  • serialize resources with their relationships and includes

Data Attribute Mapping

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.

Contribution

If you want to contribute: make your changes and do a pull request.

About

JSONAPI Implementation for Android

License:MIT License


Languages

Language:Java 100.0%