• Stars
    star
    105
  • Rank 327,227 (Top 7 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A super simple JSON helper for Swift

LumaJSON

An extremely simple and light-weight Swift wrapper for Objective-C JSON parser

Usage

To use this in your project, just copy the file in to your project. Normally I'm a fan of Cocoapods and all that, but it just seems unneccessary given the simplicity of what is really being done here.

Given a JSON structure such as:

{
    "user": {
        "name": "jquave",
        "id": 542,
        "url": "http://jamesonquave.com"
    },
    "friend_ids": [
        299,
        341,
        492
    ],
    "alert_message": "Please verify e-mail address to continue"
}

LumaJSON will recognize this as having a root NSDictionary node with three keys: user, friend_ids, and alert_message

These can be accessed using a subscript, and will return either an Optional LumaJSONObject or an object of the expected type if cast.

Example:

if let parsed = LumaJSON.parse(jsonStr) {
    
    // Simple printing to the console to check JSON structure
    println(parsed)

    // Simple Key/Value retreival
    if let alertMessage = parsed["alert_message"] as? String {
        println("Alert: \(alertMessage)")
    }
    
    // Nested JSON
    if let userName = parsed["user"]?["name"] as? String {
        println("Username is \(userName)")
    }
    
    // Nested object casting works using Swift's built-in mechanisms
    if let friendIDs = parsed["friend_ids"] as? [Int] {
        for friendID in friendIDs {
            println("Friend ID: \(friendID)")
        }
    }
}

Output to console:

LumaJSONObject: Optional({
    "alert_message" = "Please verify e-mail address to continue";
    "friend_ids" =     (
        299,
        341,
        492
    );
    user =     {
        id = 542;
        name = jquave;
        url = "http://jamesonquave.com";
    };
})
Alert: Please verify e-mail address to continue
Username is jquave
Friend ID: 299
Friend ID: 341
Friend ID: 492

Encoding Swift objects to JSON

Encoding objects as JSON requires creating an object that adheres to [String: AnyObject] at the root level.

var myData = [String: AnyObject]()
myData["friend_count"] = 4
myData["username"] = "jquave"

if let str = LumaJSON.jsonFromObject(myData) {
  println(str)
}

Outputs:

{"username":"jquave","friend_count":4}

Under MIT License http://opensource.org/licenses/MIT

More Repositories

1

Swift-Tutorial

Code for the Swift Tutorial on jamesonquave.com
Swift
238
star
2

EasyCast

Makes math operations in Swift easier by adding automatic casting between Int, Float, CGFloat, and Double. Use with care.
Swift
67
star
3

SwiftyCubes

A Swift demo of working with programmatic Views, using Dynamics, and handling touches manually.
Swift
64
star
4

CameraTutorial

Camera tutorial on JamesonQuave.com
Swift
59
star
5

TagReader

Code for Core NFC Tutorial in Swift 4 for NFC Tag reading
Swift
27
star
6

tasty

🍦 Simple and sweet Deno Web Framework for building simple APIs and Websites
TypeScript
22
star
7

Core-Data-In-Swift-Tutorial

This repository houses the code used in the Core Data tutorial on JamesonQuave.com
Swift
15
star
8

ARTrees

A simple ARKit demo app that lets you plant a 3D model of trees on real world surfaces by tapping. Written in Swift 4 and Xcode 9 beta.
Swift
14
star
9

SwiftNetworking

Some helpers for making API calls in Swift
Swift
14
star
10

SwiftPOSTTutorial

The code for the tutorial on making a POST Request in Swift from http://jamesonquave.com/blog/making-a-post-request-in-swift/
Swift
12
star
11

CoreAnimationTutorial

Supplementary to the Core Animation Tutorial on JamesonQuave.com
Swift
4
star
12

gosuckit

Brave new experiments in ruby gosu game dev
Ruby
2
star
13

TimeMyBrain

A toy app that measures and deceives the brain
Swift
2
star
14

JOptional

The code for the tutorial on JamesonQuave.com showing how you would implement Optionals in Swift using Swift
Swift
1
star
15

Swift-Tutorial-2

The second in the series of Swift tutorials on JamesonQuave.com
Swift
1
star
16

VimConfig

My vim files
Shell
1
star
17

HDResizerForCocosApps

Script to recursively resize hd images to standard definition (and handle renaming) using the -hd convention.
Ruby
1
star
18

leadgen

leadgen
Ruby
1
star
19

PhotoSwapr

Swift
1
star
20

BLOOM-Startup

Python
1
star
21

qlora-ft-falcon

Jupyter Notebook
1
star