All Projects → utahiosmac → Marshal

utahiosmac / Marshal

Licence: mit
Marshaling the typeless wild west of [String: Any]

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Marshal

Lift
Lift is a Swift library for generating and extracting values into and out of JSON-like data structures.
Stars: ✭ 33 (-95.24%)
Mutual labels:  json, mac
Whc datamodelfactory
Mac上iOS开发辅助工具,快速把json/xml数据转换生成对应模型类属性,省去麻烦手动创建,提高开发效率。Mac iOS development aid, quickly put the json/XML data transformation generates the corresponding model class attribute, save trouble created manually, improve the development efficiency.
Stars: ✭ 1,118 (+61.1%)
Mutual labels:  json, mac
Jsonconverter
JSONConverter is a desktop application for Mac OS X which enables you to covert JSON objects preview pretty rich JOSN and model classes
Stars: ✭ 402 (-42.07%)
Mutual labels:  json, mac
Mobility
Pluggable Ruby translation framework
Stars: ✭ 644 (-7.2%)
Mutual labels:  json
Netaddr
A network address manipulation library for Python
Stars: ✭ 648 (-6.63%)
Mutual labels:  mac
Ssm Bookappointment
优雅整合SSM框架:SpringMVC + Spring + MyBatis(用户登陆式图书预约系统)
Stars: ✭ 666 (-4.03%)
Mutual labels:  json
Googletranslate
🌐 Google 翻译 Mac 客户端
Stars: ✭ 688 (-0.86%)
Mutual labels:  mac
Sirix
SirixDB is a temporal, evolutionary database system, which uses an accumulate only approach. It keeps the full history of each resource. Every commit stores a space-efficient snapshot through structural sharing. It is log-structured and never overwrites data. SirixDB uses a novel page-level versioning approach called sliding snapshot.
Stars: ✭ 638 (-8.07%)
Mutual labels:  json
Pmacct
pmacct is a small set of multi-purpose passive network monitoring tools [NetFlow IPFIX sFlow libpcap BGP BMP RPKI IGP Streaming Telemetry].
Stars: ✭ 677 (-2.45%)
Mutual labels:  json
Coriander
Build NVIDIA® CUDA™ code for OpenCL™ 1.2 devices
Stars: ✭ 665 (-4.18%)
Mutual labels:  mac
Thor
Switch the right application ASAP.
Stars: ✭ 660 (-4.9%)
Mutual labels:  mac
Nord Iterm2
An arctic, north-bluish clean and elegant iTerm2 color scheme.
Stars: ✭ 651 (-6.2%)
Mutual labels:  mac
Cutelyst
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
Stars: ✭ 671 (-3.31%)
Mutual labels:  json
Go Json
Fast JSON encoder/decoder compatible with encoding/json for Go
Stars: ✭ 647 (-6.77%)
Mutual labels:  json
Struct
Xcode projects on steroids
Stars: ✭ 684 (-1.44%)
Mutual labels:  mac
Jsx
an erlang application for consuming, producing and manipulating json. inspired by yajl
Stars: ✭ 641 (-7.64%)
Mutual labels:  json
Structured Text Tools
A list of command line tools for manipulating structured text data
Stars: ✭ 6,180 (+790.49%)
Mutual labels:  json
Countries
Countries, Languages & Continents data (capital and currency, native name, calling codes).
Stars: ✭ 656 (-5.48%)
Mutual labels:  json
Alcinoe
Alcinoe Component Library For Delphi. Full opengl video player, WebRTC delphi wrapper, native ios/android TEdit, Improuved firemonkey controls, Firebase cloud messaging, Android/ios facebook sdk login, Json/Bson Parser, ImageMagick wrapper, MongoDb client And much more
Stars: ✭ 657 (-5.33%)
Mutual labels:  json
Ulfius
Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
Stars: ✭ 666 (-4.03%)
Mutual labels:  json

License Carthage Platforms Build Status codebeat badge

Marshal

In Swift, we all deal with JSON, plists, and various forms of [String: Any]. Marshal believes you don't need a Ph.D. in monads or magic mirrors to deal with these in an expressive and type safe way. Marshal will help you write declarative, performant, error handled code using the power of Protocol Oriented Programming™.

Usage

Extracting values from [String: Any] using Marshal is as easy as

let name: String = try json.value(for: "name")
let url: URL = try json.value(for: "user.website") // extract from nested objects!

Converting to Models

Unmarshaling is the process of taking an intermediary data format (the marshaled object) and tranforming it into a local representation. Think of marshaling as serialization and unmarshaling as deserialization, or coding and decoding, respectively.

Often we want to take a marshaled object (like [String: Any]) and unmarshal it into one of our local models—for example we may want to take some JSON and initialize one of our local models with it:

struct User: Unmarshaling {
    var id: Int
    var name: String
    var email: String

    init(object: MarshaledObject) throws {
        id = try object.value(for: "id")
        name = try object.value(for: "name")
        email = try object.value(for: "email")
    }
}

Now, just by virtue of supplying a simple initializer you can pull your models directly out of [String: Any]!

let users: [User] = try json.value(for: "users")

That was easy! Thanks, Protocol Oriented Programming™!

Error Handling

Are you the shoot-from-the-hip type that doesn't care about errors? Use try? to give yourself an optional value. Otherwise, join us law-abiding folks and wrap your code in a do-catch to get all the juicy details when things go wrong.

Add Your Own Values

Out of the box, Marshal supports extracting native Swift types like String, Int, etc., as well as URL, anything conforming to Unmarshaling, and arrays of all the aforementioned types. It does not support extraction of more complex types such as Date due to the wide variety of date formats, etc.

However, Marshal doesn't just leave you up the creek without a paddle! Adding your own Marshal value type is as easy as extending your type with ValueType.

extension Date : ValueType {
    public static func value(from object: Any) throws -> Date {
        guard let dateString = object as? String else {
            throw MarshalError.typeMismatch(expected: String.self, actual: type(of: object))
        }
        // assuming you have a Date.fromISO8601String implemented...
        guard let date = Date.fromISO8601String(dateString) else {
            throw MarshalError.typeMismatch(expected: "ISO8601 date string", actual: dateString)
        }
        return date
    }
}

By simply implementing value(from:), Marshal allows you to immediately do this:

let birthDate: Date = json.value(for: "user.dob")

Protocol Oriented Programming™ strikes again!

Back to the Marshaled Object

We've looked at going from our [String: Any] into our local models, but what about the other way around?

extension User: Marshaling {
    func marshaled() -> [String: Any] {
        return {
            "id": id,
            "name" : name,
            "email": email
        }
    }
}

Now, you might be thinking "but couldn't I use reflection to do this for me automagically?" You could. And if you're into that, there are some other great frameworks for you to use. But Marshal believes mirrors can lead down the road to a world of hurt. Marshal lives in a world where What You See Is What You Get™, and you can easily adapt to APIs that snake case, camel case, or whatever case your backend developers are into. Marshal code is explicit and declarative. But don't just take Marshal's word for it—read the good word towards the end here on the official Swift blog.

Performance

Of course, Marshal wouldn't be the Marshal if it didn't have some of the fastest guns in the West. You should always take benchmarks with a grain of salt, but chew on these benchmarks for a bit anyway.

Contributors

Marshal began as a blog series on JSON parsing by Jason Larsen, but quickly evolved into a community project. A special thanks to the many people who have contributed at one point or another to varying degrees with ideas and code. A few of them, in alphabetical order:

  • Bart Whiteley
  • Brian Mullen
  • Derrick Hathaway
  • Dave DeLong
  • Jason Larsen
  • Mark Schultz
  • Nate Bird
  • Tim Shadel
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].