All Projects → JohnSundell → Wrap

JohnSundell / Wrap

Licence: mit
The easy to use Swift JSON encoder

Programming Languages

swift
15916 projects
reflection
70 projects

Projects that are alternatives of or similar to Wrap

Codability
Useful helpers for working with Codable types in Swift
Stars: ✭ 125 (-82.76%)
Mutual labels:  json, encoding
Jsonlab
JSONLab: a native JSON/UBJSON/MassagePack encoder/decoder for MATLAB/Octave
Stars: ✭ 202 (-72.14%)
Mutual labels:  json, encoding
Everlayout
Reusable, downloadable, up-datable iOS layouts
Stars: ✭ 103 (-85.79%)
Mutual labels:  json, xcode
Elixir Json
Native JSON library for Elixir
Stars: ✭ 216 (-70.21%)
Mutual labels:  json, encoding
Encoding
Go package containing implementations of efficient encoding, decoding, and validation APIs.
Stars: ✭ 705 (-2.76%)
Mutual labels:  json, encoding
Psl
PHP Standard Library - a modern, consistent, centralized, well-typed set of APIs for PHP programmers.
Stars: ✭ 329 (-54.62%)
Mutual labels:  json, encoding
Quicktype Xcode
Xcode extension to paste JSON as Swift, Objective-C, and more
Stars: ✭ 1,656 (+128.41%)
Mutual labels:  json, xcode
Localize
Localize is a framework writed in swift to localize your projects easier improves i18n, including storyboards and strings.
Stars: ✭ 253 (-65.1%)
Mutual labels:  json, xcode
Compile Hero
🔰Visual Studio Code Extension For Compiling Language
Stars: ✭ 169 (-76.69%)
Mutual labels:  automatic, json
Serpent
A protocol to serialize Swift structs and classes for encoding and decoding.
Stars: ✭ 281 (-61.24%)
Mutual labels:  json, encoding
Swiftai
SwiftAI, write Swift code smart. SwiftAI can generate Model class from JSON now. Codable and HandyJSON is supported. More features will be add.
Stars: ✭ 470 (-35.17%)
Mutual labels:  json, xcode
Flutter Architecture Blueprints
Flutter Architecture Blueprints is a project that introduces MVVM architecture and project structure approaches to developing Flutter apps.
Stars: ✭ 707 (-2.48%)
Mutual labels:  xcode
Xcmetrics
XCMetrics is the easiest way to collect Xcode build metrics and improve developer productivity.
Stars: ✭ 695 (-4.14%)
Mutual labels:  xcode
Sheetjs
📗 SheetJS Community Edition -- Spreadsheet Data Toolkit
Stars: ✭ 28,479 (+3828.14%)
Mutual labels:  json
Uppy
The next open source file uploader for web browsers 🐶
Stars: ✭ 24,829 (+3324.69%)
Mutual labels:  encoding
Nano Sql
Universal database layer for the client, server & mobile devices. It's like Lego for databases.
Stars: ✭ 717 (-1.1%)
Mutual labels:  json
Scodec
Scala combinator library for working with binary data
Stars: ✭ 709 (-2.21%)
Mutual labels:  encoding
Marshal
Marshaling the typeless wild west of [String: Any]
Stars: ✭ 694 (-4.28%)
Mutual labels:  json
Jose Jwt
Ultimate Javascript Object Signing and Encryption (JOSE) and JSON Web Token (JWT) Implementation for .NET and .NET Core
Stars: ✭ 692 (-4.55%)
Mutual labels:  json
Genie
GENie - Project generator tool
Stars: ✭ 693 (-4.41%)
Mutual labels:  xcode

Wrap

Unbox | Wrap

BuddyBuild CocoaPods Carthage Twitter: @johnsundell

Wrap is an easy to use Swift JSON encoder. Don't spend hours writing JSON encoding code - just wrap it instead!

Using Wrap is as easy as calling wrap() on any instance of a class or struct that you wish to encode. It automatically encodes all of your type’s properties, including nested objects, collections, enums and more!

It also provides a suite of simple but powerful customization APIs that enables you to use it on any model setup with ease.

Basic example

Say you have your usual-suspect User model:

struct User {
    let name: String
    let age: Int
}

let user = User(name: "John", age: 28)

Using wrap() you can now encode a User instance with one command:

let dictionary: [String : Any] = try wrap(user)

Which will produce the following Dictionary:

{
    "name": "John",
    "age": 28
}

Advanced example

The first was a pretty simple example, but Wrap can encode even the most complicated structures for you, with both optional, non-optional and custom type values, all without any extra code on your part. Let’s say we have the following model setup:

struct SpaceShip {
    let type: SpaceShipType
    let weight: Double
    let engine: Engine
    let passengers: [Astronaut]
    let launchLiveStreamURL: URL?
    let lastPilot: Astronaut?
}

enum SpaceShipType: Int, WrappableEnum {
    case apollo
    case sputnik
}

struct Engine {
    let manufacturer: String
    let fuelConsumption: Float
}

struct Astronaut {
    let name: String
}

Let’s create an instance of SpaceShip:

let ship = SpaceShip(
    type: .apollo,
    weight: 3999.72,
    engine: Engine(
        manufacturer: "The Space Company",
        fuelConsumption: 17.2321
    ),
    passengers: [
        Astronaut(name: "Mike"),
        Astronaut(name: "Amanda")
    ],
    launchLiveStreamURL: URL(string: "http://livestream.com"),
    lastPilot: nil
)

And now let’s encode it with one call to wrap():

let dictionary: WrappedDictionary = try wrap(ship)

Which will produce the following dictionary:

{
    "type": 0,
    "weight": 3999.72,
    "engine": {
        "manufacturer": "The Space Company",
        "fuelConsumption": 17.2321
    },
    "passengers": [
        {"name": "Mike"},
        {"name": "Amanda"}
    ],
    "launchLiveStreamURL": "http://livestream.com"
}

As you can see, Wrap automatically encoded the URL property to its absoluteString, and ignored any properties that were nil (reducing the size of the produced JSON).

Customization

While automation is awesome, customization is just as important. Thankfully, Wrap provides several override points that enables you to easily tweak its default behavior.

Customizing keys

Per default Wrap uses the property names of a type as its encoding keys, but sometimes this is not what you’re looking for. You can choose to override any or all of a type’s encoding keys by making it conform to WrapCustomizable and implementing keyForWrapping(propertyNamed:), like this:

struct Book: WrapCustomizable {
    let title: String
    let authorName: String

    func keyForWrapping(propertyNamed propertyName: String) -> String? {
        if propertyName == "authorName" {
            return "author_name"
        }

        return propertyName
    }
}

You can also use the keyForWrapping(propertyNamed:) API to skip a property entirely, by returning nil from this method for it.

Custom key types

You might have nested dictionaries that are not keyed on Strings, and for those Wrap provides the WrappableKey protocol. This enables you to easily convert any type into a string that can be used as a JSON key.

Encoding keys as snake_case

If you want the dictionary that Wrap produces to have snake_cased keys rather than the default (which is matching the names of the properties that were encoded), you can easily do so by conforming to WrapCustomizable and returning .convertToSnakeCase from the wrapKeyStyle property. Doing that will, for example, convert the property name myProperty into the key my_property.

Customized wrapping

For some nested types, you might want to handle the wrapping yourself. This may be especially true for any custom collections, or types that have a completely different representation when encoded. To do that, make a type conform to WrapCustomizable and implement wrap(context:dateFormatter:), like this:

struct Library: WrapCustomizable {
    private let booksByID: [String : Book]

    func wrap(context: Any?, dateFormatter: DateFormatter?) -> Any? {
        return Wrapper(context: context, dateFormatter: dateFormatter).wrap(self.booksByID)
    }
}

Enum support

Wrap also makes it super easy to encode any enum values that your types are using. If an enum is based on a raw type (such as String or Int), all you have to do is to declare conformance to WrappableEnum, and the rest is taken care of for you.

Non-raw type enum values are also automatically encoded. The default behavior encodes any values that don’t have associated values as their string representation, and those that do have associated values as a dictionary (with the string representation as the key), like this:

enum Profession {
    case developer(favoriteLanguageName: String)
    case lawyer
}

struct Person {
    let profession = Profession.developer(favoriteLanguageName: "Swift")
    let hobbyProfession = Profession.lawyer
}

Encodes into:

{
    "profession": {
        "developer": "Swift"
    },
    "hobbyProfession": "lawyer"
}

Contextual objects

To be able to easily encode any dependencies that you might want to use during the encoding process, Wrap provides the ability to supply a contextual object when initiating the wrapping process (by calling wrap(object, context: myContext).

A context can be of Any type and is accessible in all WrapCustomizable wrapping methods. Here is an example, where we send in a prefix to add to a Book’s title:

struct Book: WrapCustomizable {
    let title: String

    func wrap(context: Any?, dateFormatter: DateFormatter?) -> Any? {
        guard let prefix = context as? String else {
            return nil
        }

        return [
            "title" : prefix + self.title
        ]
    }
}

String serialization

let data = try wrap(object) as Data
let string = String(data: data, encoding: .utf8)

Compatibility

Wrap supports the following platforms:

  • 📱 iOS 8+
  • 🖥 macOS 10.9+
  • ⌚️ watchOS 2+
  • 📺 tvOS 9+
  • 🐧 Linux

Usage

Wrap can be easily used in either a Swift script, command line tool or in an app for iOS, macOS, watchOS, tvOS or Linux.

In an application

Either

  • Drag the file Wrap.swift into your application's Xcode project.

or

In a script

  • Install Marathon.
  • Add Wrap using $ marathon add https://github.com/JohnSundell/Wrap.git.
  • Run your script using $ marathon run <path-to-your-script>.

In a command line tool

  • Drag the file Wrap.swift into your command line tool's Xcode project.

Hope you enjoy wrapping your objects!

For more updates on Wrap, and my other open source projects, follow me on Twitter: @johnsundell

Also make sure to check out Unbox that let’s you easily decode JSON.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].