All Projects → isair → Jsonhelper

isair / Jsonhelper

Licence: other
✌ Convert anything into anything in one operation; JSON data into class instances, hex strings into UIColor/NSColor, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Jsonhelper

Cuckoo
Boilerplate-free mocking framework for Swift!
Stars: ✭ 1,344 (+69.7%)
Mutual labels:  protocol, cocoapods
Guillotinemenu
Our Guillotine Menu Transitioning Animation implemented in Swift reminds a bit of a notorious killing machine.
Stars: ✭ 2,908 (+267.17%)
Mutual labels:  protocol, cocoapods
Randomkit
Random data generation in Swift
Stars: ✭ 1,458 (+84.09%)
Mutual labels:  protocol, cocoapods
Foldingtabbar.ios
Folding Tab Bar and Tab Bar Controller
Stars: ✭ 3,677 (+364.27%)
Mutual labels:  protocol, cocoapods
Abexpandableview
Expandable, collapsible, filterable and single/multi selectable table view.
Stars: ✭ 138 (-82.58%)
Mutual labels:  protocol, cocoapods
Awesomeintroguideview
🏆An awesome view for Introduce
Stars: ✭ 415 (-47.6%)
Mutual labels:  dictionaries, cocoapods
Validatedpropertykit
Easily validate your Properties with Property Wrappers 👮
Stars: ✭ 701 (-11.49%)
Mutual labels:  cocoapods
Lsquic
LiteSpeed QUIC and HTTP/3 Library
Stars: ✭ 727 (-8.21%)
Mutual labels:  protocol
Open Source Ios Apps
📱 Collaborative List of Open-Source iOS Apps
Stars: ✭ 28,826 (+3539.65%)
Mutual labels:  cocoapods
Complimentarygradientview
Create complementary gradients generated from dominant and prominent colors in supplied image. Inspired by Grade.js
Stars: ✭ 691 (-12.75%)
Mutual labels:  cocoapods
Axwebviewcontroller
AXWebViewController is a webViewController to browse web content inside applications. It’s a lightweight controller on iOS platform based on WKWebView (UIWebView would be the base Kit under iOS 8.0). It added navigation tool bar to refresh, go back, go forward and so on. It support the navigation style on WeChat. It is a simple-using and convenient web view controller using inside applications.
Stars: ✭ 770 (-2.78%)
Mutual labels:  cocoapods
Font Awesome Swift
Font Awesome swift library for iOS.
Stars: ✭ 743 (-6.19%)
Mutual labels:  cocoapods
Examples
A collection of TLA+ specifications of varying complexities
Stars: ✭ 720 (-9.09%)
Mutual labels:  protocol
Notepad
[iOS] A fully themeable markdown editor with live syntax highlighting.
Stars: ✭ 705 (-10.98%)
Mutual labels:  cocoapods
Defaults
Swifty and modern UserDefaults
Stars: ✭ 734 (-7.32%)
Mutual labels:  cocoapods
Node Minecraft Protocol
Parse and serialize minecraft packets, plus authentication and encryption.
Stars: ✭ 697 (-11.99%)
Mutual labels:  protocol
Lbry Sdk
The LBRY SDK for building decentralized, censorship resistant, monetized, digital content apps.
Stars: ✭ 7,169 (+805.18%)
Mutual labels:  protocol
Uiimageview Letters
UIImageView category for using initials as a placeholder image, written in Objective-C. For a Swift implementation, see https://github.com/bachonk/InitialsImageView
Stars: ✭ 694 (-12.37%)
Mutual labels:  cocoapods
Gradients
🌔 A curated collection of splendid 180+ gradients made in swift
Stars: ✭ 719 (-9.22%)
Mutual labels:  cocoapods
Blockhook
Hook Objective-C blocks. A powerful AOP tool.
Stars: ✭ 742 (-6.31%)
Mutual labels:  cocoapods

JSONHelper CocoaPods CocoaPods

Build Status CocoaPods Gitter

Convert anything into anything in one operation; hex strings into UIColor/NSColor, JSON strings into class instances, y/n strings to booleans, arrays and dictionaries of these; anything you can make sense of!

Latest version requires iOS 8+ and Xcode 7.3+

GitAds

Table of Contents

  1. Installation
  2. The <-- Operator
  3. Convertible Protocol
  4. Deserializable Protocol (with JSON deserialization example)
  5. Serializable Protocol

Installation

CocoaPods

Add the following line in your Podfile.

pod "JSONHelper"

Carthage

Add the following line to your Cartfile.

github "isair/JSONHelper"

Then do carthage update. After that, add the framework to your project.

The <-- Operator

The <-- operator takes the value on its right hand side and tries to convert it into the type of the value on its left hand side. If the conversion fails, an error is logged on debug builds. If it's successful, the value of the left hand side variable is overwritten. It's chainable as well.

If the right hand side value is nil or the conversion fails, and the left hand side variable is an optional, then nil is assigned to it. When the left hand side is non-optional, the current value of the left hand side variable is left untouched.

Using this specification let's assume you have a dictionary response that you retrieved from some API with hex color strings in it, under the key colors, that you want to convert into an array of UIColor instances. Also, to fully use everything we know, let's also assume that we want to have a default value for our color array in case the value for the key we're looking for does not exist (is nil).

var colors = [UIColor.blackColor(), UIColor.whiteColor()]
// Assume we have response { "colors": ["#aaa", "#b06200aa"] }
colors <-- response[colorsKey]

Convertible Protocol

If your type is a simple value-like type, adopting the Convertible protocol is the way to make your type work with the <-- operator.

Example:

struct Vector2D: Convertible {
  var x: Double = 0
  var y: Double = 0

  init(x: Double, y: Double) {
    self.x = x
    self.y = y
  }

  static func convertFromValue<T>(value: T?) throws -> Self? {
    guard let value = value else { return nil }

    if let doubleTupleValue = value as? (Double, Double) {
      return self.init(x: doubleTupleValue.0, y: doubleTupleValue.1)
    }

    throw ConversionError.UnsupportedType
  }
}
var myVector: Vector2D?
myVector <-- (1.0, 2.7)

Deserializable Protocol

While you can basically adopt the Convertible protocol for any type, if your type is always converted from a dictionary or a JSON string then things can get a lot easier with the Deserializable protocol.

Example:

class User: Deserializable {
  static let idKey = "id"
  static let emailKey = "email"
  static let nameKey = "name"
  static let avatarURLKey = "avatar_url"

  private(set) var id: String?
  private(set) var email: String?
  private(set) var name = "Guest"
  private(set) var avatarURL = NSURL(string: "https://mysite.com/assets/default-avatar.png")

  required init(dictionary: [String : AnyObject]) {
    id <-- dictionary[User.idKey]
    email <-- dictionary[User.emailKey]
    name <-- dictionary[User.nameKey]
    avatarURL <-- dictionary[User.avatarURLKey]
  }
}
var myUser: User?
user <-- apiResponse["user"]

Serializable Protocol

// Serialization is coming soon. I'll probably not add a new protocol and just rename and update the Deserializable protocol and turn it into a mixin.

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].