Skip to content

goo32/saule

This branch is 4 commits ahead of, 176 commits behind joukevandermaas/saule:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

fa9ff79 · Jul 25, 2016
Jul 23, 2016
Jul 23, 2016
Jan 4, 2016
Nov 17, 2015
Jun 15, 2016
Nov 7, 2015
Nov 7, 2015
Jan 2, 2016
Jan 2, 2016
Jun 15, 2016
Jan 19, 2016

Repository files navigation

Saule

Build status

Saule is a Json Api (version 1.0) library for ASP.Net Web API 2. See the Wiki for documentation and some samples. Install Saule using NuGet:

Install-Package saule

To use Saule, you must define resources that contain the information about your domain:

public class PersonResource : ApiResource
{
    public PersonResource()
    {
        Attribute("FirstName");
        Attribute("LastName");
        Attribute("Age");

        BelongsTo<CompanyResource>("Job");
        HasMany<PersonResource>("Friends");
    }
}
public class CompanyResource : ApiResource
{
    public CompanyResource()
    {
        Attribute("Name");
        Attribute("NumberOfEmployees");
    }
}

You can then use these to serialize any class into Json Api (as long as your class has properties with the same names as in your model):

public class PersonController : ApiController
{
    [HttpGet]
    [ReturnsResource(typeof(PersonResource))]
    [Route("people/{id}")]
    public JohnSmith GetPerson(string id)
    {
        return new JohnSmith();
    }
}
GET http://example.com/people/123

{
  "data": {
    "type": "person",
    "id": "123",
    "attributes": {
      "first-name": "John",
      "last-name": "Smith",
      "age": 34
    },
    "relationships": {
      "job": {
        "links": {
          "self": "http://example.com/people/123/relationships/job/",
          "related": "http://example.com/people/123/job/"
        },
        "data": {
          "type": "company",
          "id": "456"
        }
      },
      "friends": {
        "links": {
          "self": "http://example.com/people/123/relationships/friends/",
          "related": "http://example.com/people/123/friends/"
        },
        "data": [
          {
            "type": "person",
            "id": "789"
          }
        ]
      }
    }
  },
  "included": [
    {
      "type": "company",
      "id": "456",
      "attributes": {
        "name": "Awesome, Inc.",
        "number-of-employees": 24
      }
    },
    {
      "type": "person",
      "id": "789",
      "attributes": {
        "first-name": "Sara",
        "last-name": "Jones",
        "age": 38
      }
    }
  ],
  "links": {
    "self": "http://example.com/people/123"
  }
}

Deserialization works just like in normal Web Api; you don't need to do anything special to make this work.

About

Json api library for ASP.Net Web API 2

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.8%
  • Other 0.2%