Giter Club home page Giter Club logo

sourcery-autojsonserializable's Introduction

Sourcery AutoJSONSerialization

This is my attempt to implement some Sourcery templates which automatically generates JSON bindings for your structs and classes.

Build Status codecov

My objective here is to provide a fully tested code generation template that will let you skip the pain of writing this code:

extension Contact: JSONDeserializable {
    enum Fields: String {
        case identifier = "id"
        case firstName = "first_name"
        case lastName = "last_name"
        case age = "age"
    }

    init?(JSONObject: [String : Any]) {
        guard let id = JSONObject[Fields.identifier.rawValue] as? String,
            let firstName = JSONObject[Fields.firstName.rawValue] as? String,
            let lastName = JSONObject[Fields.lastName.rawValue] as? String,
            let age = JSONObject[Fields.age.rawValue] as? Int
            else {
                return nil
        }
        self.id = id
        self.firstName = firstName
        self.lastName = lastName
        self.age = age
    }
}

Usage

The JSON serialization/deserialization is based on two protocols.

protocol JSONDeserializable {
    init?(JSONObject: [String: Any])
}

protocol JSONSerializable {
    func toJSONObject() -> [String: Any]
}

With this tool, you do not need to implement these protocols, the Sourcery templates will do the boiler plate code for you.
To do this, all you need to do is conform to one (or both) of these protocols:

protocol AutoJSONDeserializable {}

protocol AutoJSONSerializable {}

And then run Sourcery using the templates found in the Templates/ folder of this repository.

Example

Let say you have a model struct like this one:

struct Contact {
    let id: String
    let firstName: String
    let lastName: String
    let age: Int
}

Making it conform to AutoJSONDeserializable would result make it support this JSON:

{
  "id": "SomeID",
  "firstName": "John",
  "lastName": "Doe'"
}

Features

  • primitive JSON types (String, Int, Double)
  • ISO8601 formatted dates
  • optionals
  • nested structures
  • JSONKey annotation

Annotations

When mapping your JSON to your model structure, you may sometimes want to use a different attribute name as the one in the JSON file.
Let's say you have this JSON:

{
  "id": "SomeID",
  "first_name": "John",
  "last_name": "Doe'"
}

As you can see, the naming convention does not follow camel case. To fix this you can use Sourcery annotations.
On a property, you can define the JSONKey attribute with the JSON name you want to bind the property to:

struct Contact {
    let id: String
    // sourcery: JSONKey = "first_name"
    let firstName: String
    // sourcery: JSONKey = "last_name"
    let lastName: String
}

Installation

The current installation process is pretty basic and does not support any dependency managers yet.
To install just copy the following source files in your project:

  • Sources/AutoJSONSerialization/AutoJSONSerializable.swift
  • Sources/AutoJSONSerialization/AutoJSONDeserializable.swift
  • Sources/AutoJSONSerialization/JSONDateFormatter.swift

And then copy the Templates files and configure Sourcery.

sourcery-autojsonserializable's People

Contributors

liquidsoul avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.