Swiftpack.co - Package - aonawale/JSONAPISerializer

JSONAPISerializer for Server Side Swift

Serialize Swift objects into JSONAPI compliant structures.

Basic usage

import JSON
import JSONAPISerializer

struct User: JSONRepresentable {
    var id: Node?
    let firstName: String
    let lastName: String

    func makeJSON() throws -> JSON {
        var json = JSON([:])
        try json.set("id", id)
        try json.set("first-name", firstName)
        try json.set("last-name", lastName)
        return json
    }
}

let config = JSONAPIConfig(type: "users")
let serializer = JSONAPISerializer(config: config)

let users: [User]...
try serializer.serialize(users)

Produces

{
  "data": [{
    "type": "users",
    "id": "1",
    "attributes": {
      "first-name": "foo",
      "last-name": "bar"
    }
  }, {
    "type": "users",
    "id": "2",
    "attributes": {
      "first-name": "John",
      "last-name": "Doe"
    }
  }],
  "jsonapi": {
    "version": "1.0"
  },
  "links": {
    "self": "/users"
  },
  "meta": {}
}

Relationships

let petsConfig = JSONAPIConfig(type: "pets")
let profileConfig = JSONAPIConfig(type: "profiles", relationships: ["pets": petsConfig])
let userConfig = JSONAPIConfig(type: "users", relationships: ["profile": profileConfig])
let serializer = JSONAPISerializer(config: userConfig)

let users: [User]...
try serializer.serialize(users)

Produces

{
  "data": [
    {
      "id": 2,
      "type": "users",
      "attributes": {
        "first-name": "foo",
        "last-name": "bar"
      },
      "relationships": {
        "profile": {
          "data": {
            "id": 2,
            "type": "profiles"
          }
        }
      }
    }
  ],
  "included": [
    {
      "attributes": {
        "age": 18,
        "user-id": 2,
      },
      "id": 2,
      "relationships": {
        "pets": {
          "data": [
            {
              "id": 5,
              "type": "pets"
            },
            {
              "id": 8,
              "type": "pets"
            }
          ]
        }
      },
      "type": "profiles"
    },
    {
      "attributes": {
        "pet-name": "dog",
        "profile-id": 2
      },
      "id": 5,
      "relationships": {},
      "type": "pets"
    },
    {
      "attributes": {
        "pet-name": "cat",
        "profile-id": 2
      },
      "id": 8,
      "relationships": {},
      "type": "pets"
    }
  ],
  "jsonapi": {
    "version": "1.0"
  },
  "links": {
    "self": "/users"
  },
  "meta": {}
}

Roadmap

  • Dasherized, underscored and camel cased keys support.

License

JSONAPISerializer is available under the MIT license. See the LICENSE file for more information.

Github

link
Stars: 17
Help us keep the lights on

Used By

Total: 0

Releases

v1.2.2 - Sep 26, 2017

Fix whitelisted relationships not taking precedence over blacklisted relationships. Fix blacklisted relationships and attributes from being serialized.

v1.2.1 - Sep 23, 2017

Fix bug where blacklisted relationships are being serialized

v1.2.0 - Jul 31, 2017

Add support for customizing relationship keys.

v1.1 - Jun 17, 2017

Add support for relationships as string Include JSONAPI version in serialized JSON

v1.0 - May 22, 2017