• This repository has been archived on 19/Sep/2018
  • Stars
    star
    1,090
  • Rank 42,497 (Top 0.9 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 6 years ago

Reviews

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

Repository Details

A reusable framework for parsing JSON in Swift.

Why Freddy?

Parsing JSON elegantly and safely can be hard, but Freddy is here to help. Freddy is a reusable framework for parsing JSON in Swift. It has three principal benefits.

First, Freddy provides a type safe solution to parsing JSON in Swift. This means that the compiler helps you work with sending and receiving JSON in a way that helps to prevent runtime crashes.

Second, Freddy provides an idiomatic solution to JSON parsing that takes advantage of Swift's generics, enumerations, and functional features. This is all provided without the pain of having to memorize our documentation to understand our magical custom operators. Freddy does not have any of those. If you feel comfortable writing Swift (using extensions, protocols, initializers, etc.), then you will not only understand how Freddy is organized, but you will also feel comfortable using Freddy.

Third, Freddy provides great error information for mistakes that commonly occur while parsing JSON. If you subscript the JSON object with a key that is not present, you get an informative error. If your desired index is out of bounds, you get an informative error. If you try to convert a JSON value to the wrong type, you get a good error here too.

So, Freddy vs. JSON, who wins? We think it is Freddy.

Usage

This section describes Freddy's basic usage. You can find more examples on parsing data, dealing with errors, serializing JSON instances into NSData, and more in the Wiki. You can read the documentation to see the full API.

Deserialization: Parsing Raw Data

Basic Usage

Consider some example JSON data:

{
    "success": true,
    "people": [
        {
            "name": "Matt Mathias",
            "age": 32,
            "spouse": true
        },
        {
            "name": "Sergeant Pepper",
            "age": 25,
            "spouse": false
        }
    ],
    "jobs": [
        "teacher",
        "judge"
    ],
    "states": {
        "Georgia": [
            30301,
            30302,
            30303
        ],
        "Wisconsin": [
            53000,
            53001
        ]
    }
}

Here is a quick example on how to parse this data using Freddy:

let data = getSomeData()
do {
    let json = try JSON(data: data)
    let success = try json.getBool(at: "success")
    // do something with `success`
} catch {
    // do something with the error
}

After we load in the data, we create an instance of JSON, the workhorse of this framework. This allows us to access the values from the JSON data. We try because the data may be malformed and the parsing could generate an error. Next, we access the "success" key by calling the getBool(at:) method on JSON. We try here as well because accessing the json for the key "success" could fail - e.g., if we had passed an unknown key. This method takes two parameters, both of which are used to define a path into the JSON instance to find a Boolean value of interest. If a Bool is found at the path described by "success", then getBool(at:) returns a Bool. If the path does not lead to a Bool, then an appropriate error is thrown.

Use Paths to Access Nested Data with Subscripting

With Freddy, it is possible to use a path to access elements deeper in the json structure. For example:

let data = getSomeData()
do {
    let json = try JSON(data: data)
    let georgiaZipCodes = try json.getArray(at: "states","Georgia")
    let firstPersonName = try json.getString(at: "people",0,"name")
} catch {
    // do something with the error
}

In the code json.getArray(at: "states","Georgia"), the keys "states" and "Georgia" describe a path to the Georgia zip codes within json. Freddy's parlance calls this process "subscripting" the JSON. What is typed between the parentheses of, for example, getArray(at:) is a comma-separated list of keys and indices that describe the path to a value of interest.

There can be any number of subscripts, and each subscript can be either a String indicating a named element in the JSON, or an Int that represents an element in an array. If there is something invalid in the path such as an index that doesn't exist in the JSON, an error will be thrown.

More on Subscripting

JSONDecodable: Deserializing Models Directly

Now, let's look an example that parses the data into a model class:

let data = getSomeData()
do {
    let json = try JSON(data: data)
    let people = try json.getArray(at: "people").map(Person.init)
    // do something with `people`
} catch {
    // do something with the error
}

Here, we are instead loading the values from the key "people" as an array using the method getArray(at:). This method works a lot like the getBool(at:) method you saw above. It uses the path provided to the method to find an array. If the path is good, the method will return an Array of JSON. If the path is bad, then an appropriate error is thrown.

We can then call map on that JSON array. Since the Person type conforms to JSONDecodable, we can pass in the Person type's initializer. This call applies an initializer that takes an instance of JSON to each element in the array, producing an array of Person instances.

Here is what JSONDecodable looks like:

public protocol JSONDecodable {
    init(json: JSON) throws
}

It is fairly simple protocol. All it requires is that conforming types implement an initializer that takes an instance of JSON as its sole parameter.

To tie it all together, here is what the Person type looks like:

public struct Person {
    public let name: String
    public let age: Int
    public let spouse: Bool
}

extension Person: JSONDecodable {
    public init(json value: JSON) throws {
        name = try value.getString(at: "name")
        age = try value.getInt(at: "age")
        spouse = try value.getBool(at: "spouse")
    }
}

Person just has a few properties. It conforms to JSONDecodable via an extension. In the extension, we implement a throwsing initializer that takes an instance of JSON as its sole parameter. In the implementation, we try three functions: 1) getString(at:), 2) getInt(at:), and 3) getBool(at:). Each of these works as you have seen before. The methods take in a path, which is used to find a value of a specific type within the JSON instance passed to the initializer. Since these paths could be bad, or the requested type may not match what is actually inside of the JSON, these methods may potentially throw an error.

Serialization

Freddy's serialization support centers around the JSON.serialize() method.

Basic Usage

The JSON enumeration supports conversion to Data directly:

let someJSON: JSON = …
do {
    let data: Data = try someJSON.serialize()
} catch {
    // Handle error
}

JSONEncodable: Serializing Other Objects

Most of your objects aren't Freddy.JSON objects, though. You can serialize them to Data by first converting them to a Freddy.JSON via JSONEncodable.toJSON(), the sole method of the JSONEncodable protocol, and then use serialize() to convert the Freddy.JSON to Data:

let myObject: JSONEncodable = …

// Object -> JSON -> Data:
let objectAsJSON: JSON = myObject.toJSON()
let data: Data = try objectAsJSON.serialize()

// More concisely:
let dataOneLiner = try object.toJSON().serialize()

Freddy provides definitions for common Swift datatypes already. To make your own datatypes serializable, conform them to JSONEncodable and implement that protocol's toJSON() method:

extension Person: JSONEncodable {
    public func toJSON() -> JSON {
        return .dictionary([
            "name": .string(name),
            "age": .int(age),
            "spouse": .bool(spouse)])
    }
}

Getting Started

Freddy requires iOS 8.0, Mac OS X 10.9, watchOS 2.0, or tvOS 9.0. Linux is not yet supported.

You have a few different options to install Freddy.

Carthage

Add us to your Cartfile:

github "bignerdranch/Freddy" ~> 3.0

After running carthage bootstrap, add Freddy.framework to the "Linked Frameworks and Libraries" panel of your application target. Read more.

CocoaPods

Add us to your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'Freddy'

Then run pod install.

Submodules

  1. git submodule add https://github.com/bignerdranch/Freddy.git Vendor/Freddy
  2. Drag Freddy.xcodeproj into your Xcode project.
  3. Add Freddy.framework to the "Linked Frameworks and Libraries" panel of your application target.

Carthage can be used to check out dependencies and maintain Git submodule state as well.

Swift Package Manager

Add us to your Package.swift:

import PackageDescription

let package = Package(
    name: "My Nerdy App",
    dependencies: [
        .Package(url: "https://github.com/bignerdranch/Freddy.git", majorVersion: 3),
    ]
)

iOS 7

If you would like to use Freddy with iOS 7, then you will need to use a previous release of Freddy.

Setting Breakpoint Errors

It can be helpful to set breakpoints for errors when you start working with a new set of JSON. This allows you to explore the structure of the JSON when you break. In particular, you will likely want to set a breakpoint for Freddy's JSON.Error so that you can inspect what went wrong.

Here is how you can set this sort of breakpoint:

  1. Go to the Breakpoint navigator

  2. Click the "+" button in the bottom left corner

Breakpoint navigator

  1. Select "Add Swift Error Breakpoint"

Add Error Breakpoint

Now you have a breakpoint that will only trigger when a Swift error is generated. But your program will break whenever any Swift error is thrown. What if you only want to break for Freddy's JSON.Error error?

You can edit the breakpoint to add a filter:

  1. Right-click your new error breakpoint

  2. Select Edit Breakpoint...

Edit Breakpoint

  1. A window will appear with a text box for "Type"

  2. Enter JSON.Error

Error Type

And that is pretty much it! You now have an error breakpoint that will only trigger when errors of type JSON.Error are thrown. Take a look at the framework's tests for further examples of usage. The Wiki also have a lot of very useful information.

More Repositories

1

expandable-recycler-view

[DEPRECATED]
Java
1,215
star
2

CoreDataStack

The Big Nerd Ranch Core Data Stack
Swift
562
star
3

Deferred

Work with values that haven't been determined yet.
Swift
421
star
4

developing-alexa-skills-solutions

JavaScript
232
star
5

why_elixir

Selling points for Elixir / Phoenix
155
star
6

Typesetter

Android tool to help display changes to text positioning.
Java
150
star
7

iOS7Demos

Some short demos of new stuff in iOS 7
Objective-C
145
star
8

BNRDynamicTypeManager

Objective-C
127
star
9

android-bluetooth-testbed

Provides a Server and Client example for understanding Bluetooth Low Energy on Android
Java
97
star
10

music-frequency-d3

A music frequency visualizer using D3.js
JavaScript
93
star
11

rust-ios-app-part-1

Let's Build an iOS App in Rust, part 1
Swift
81
star
12

iOS8Demos

iOS 8 release day demonstrations
Objective-C
77
star
13

AndroidCourseResources

Resources for our Android Bootcamp.
Java
75
star
14

stockwatcher

stockwatcher, a modern android development stack showcase
Java
62
star
15

alexa-airportinfo

Node.js src for the article "Developing Alexa Skills Locally with Node.js"
JavaScript
61
star
16

bnns-cocoa-example

An example of a neural network trained by tensorflow and executed using BNNS
Objective-C
59
star
17

type-erasure-playgrounds

Code to accompany Swift Symposium Talk - https://www.youtube.com/watch?v=d_FJHBl5ohU
Swift
56
star
18

Result

Small Swift library for Result type
Swift
47
star
19

android-listview-custom-view

Example of using a custom view subclass with ListView and a custom Adapter
Java
45
star
20

linette

Custom lint checks for Android development.
Java
42
star
21

rust-ios-app-part-3

Let's Build an iOS App in Rust, part 3
Swift
31
star
22

iOS3eSolutions

Solutions to iOS Programming: The Big Nerd Ranch Guide, 3rd Edition.
Objective-C
31
star
23

doze-logger

Simple app thrown together to help demonstrate the effects of doze mode on your background schedulers.
Java
30
star
24

ScriptableTextEditor

A simple Cocoa text editor that demonstrates creating a plugin system in Python.
Objective-C
29
star
25

DeferredTCPSocket

DeferredTCPSocket is a Swift framework for asynchronous TCP networking.
Swift
28
star
26

macOS-bootstrap

A bootstrap script for configuring machine to develop iOS, tvOS, and macOS apps
Shell
25
star
27

BNR-blog-cameraxMlkit

Sample project associated with blog post on using Firebase MLKit with CameraX.
Kotlin
21
star
28

blog-ios-xcui-todo

Demonstrates using Xcode 7's new UI testing support
Swift
20
star
29

BNRun-SiriKit-Sample

Sample code for the SiriKit blog post
Swift
19
star
30

Roger

Roger is jolly. By Franklin.
Objective-C
18
star
31

controllers-for-tvOS

Sample app for tvOS that reads game controllers
Swift
18
star
32

jenkins-android-signing

[DEPRECATED] Jenkins Plugin for signing Android APKs
Java
18
star
33

alexa-account-linking-service

Code example for the Oauth account linking example for an alexa skills
JavaScript
17
star
34

tensorflow-cocoa-example

Using tensorflow inside a desktop Cocoa application
Python
17
star
35

ViewRotationTest

A demonstration of various techniques for view rotation in Cocoa Touch.
Objective-C
17
star
36

BlappyFird

Demo clone of a popular game
Swift
16
star
37

rust-ios-app-part-2

Let's Build an iOS App in Rust, part 2
Swift
12
star
38

rust-ios-app-part-4

Let's Build an iOS App in Rust, part 4
Rust
12
star
39

react-testing-workshop

Starter Code for BNR React Testing Workshop
JavaScript
11
star
40

BNRXIBCell

A UITableViewCell subclass, intended to be subclassed, to forward action messages from cell subviews to a controller.
Objective-C
11
star
41

opencv-cocoa-example

An example of using OpenCV in a Cocoa application
Objective-C++
10
star
42

BNR-blog-motionlayout

Sample project associated with blog post on using MotionLayout and MotionEditor.
Kotlin
9
star
43

semaphorejs

A functional-style Semaphore for managing concurrent access to a resource. For JavaScript.
JavaScript
9
star
44

RanchWeather

A demo app written in Swift 3 to demonstrate different code patterns.
Swift
9
star
45

android-securebank

Code example companion for the DevNexus talk: "Keeping Android Secrets Secure with Fingerprint Authentication and the Keystore"
Java
8
star
46

ember-engine-example

Ember-Engine Example: Large Company Site from Blogpost
JavaScript
8
star
47

dexie-fulltextsearch

Full text search for IndexedDB databases, powered by Dexie.
JavaScript
8
star
48

id3-meta

A metadata parser for ID3v2 formatted MP3s, in JavaScript.
JavaScript
8
star
49

SyncAdapterDemo

Experiment with android sync adapters. Getting them to work with a remote rails server to sync book error data.
Java
8
star
50

iOS-Responsive-UI-Sample

3 examples examining modern techniques for making iOS app UI accessible and responsive.
Swift
7
star
51

OpenParentApplicationDemo

Demo of openParentApplication method of WatchKit
Swift
7
star
52

doktor-demo

A simple Ktor demo app. Write Kotlin on the server.
Kotlin
7
star
53

BNRLoupe

A magnifying loupe for images that displays one-for-one pixel information for the point under the finger.
Objective-C
7
star
54

permission-manager

Easily and seamlessly ask for Android runtime permissions.
Java
7
star
55

alexa-cakebaker

JavaScript
7
star
56

cocoa-conf-stack-views

@cbkeur's CocoaConf talk on UIStackViews
Swift
6
star
57

character-data-api

Kotlin Ktor backend for the Samodelkin Android Client
Kotlin
6
star
58

meta_elixir

Elixir
5
star
59

cleaning-up-messy-chaining-code-in-swift

Swift
5
star
60

ShindigSheriff

Event Manager Manager, Mr. Manager
Ruby
5
star
61

auto-layout-mastery-conference-talk-2016

Step's Auto Layout Mastery talk about Priorities
Swift
4
star
62

upfront

Go
4
star
63

ottergram-solutions

Sample of how we could organize solutions for the 4 apps in the frontend book.
4
star
64

FocusWheel

A tvOS app for a two-part Big Nerd Ranch Blog Post on collection view layouts and the focus engine
Swift
4
star
65

Github-Issues-Reader

A super cool app for reading github issues that definitely doesn't already exist
Swift
3
star
66

cocoa-layout-instrument-demo

Sample files to accompany a blog post on the Cocoa Layout Instrument.
Swift
3
star
67

BNR-react-native-style-queries

Declarative responsive design for React Native.
JavaScript
3
star
68

bootstrap

Ruby
3
star
69

SocialDistancingSimulator

A SpriteKit-based social distancing simulator
Swift
3
star
70

auto-layout-priorities-2016

Demo project that accompanies Auto Layout Mastery Conference Talk on priorities from Step Christopher
Swift
3
star
71

alfred_reflecticle_extension

Ruby
2
star
72

KotlinConf2023-TestRefactor

Kotlin 2023 Testing Workshop Template
Kotlin
2
star
73

tacostand

A sample project for practice using Rails applications
Ruby
2
star
74

authorizing-jsonapi-resources

A tutorial for protecting private resources in JSONAPI::Resources.
Ruby
2
star
75

BNR-blog-React-to-Vue-react-simple

React project for comparison to Vue for blog entry
JavaScript
2
star
76

vapor-testing-example

Demonstrates testing techniques in the Vapor framework
Swift
2
star
77

bnr-webhooks

Big Nerd Ranch's webhook layer for inter-service communication.
Ruby
2
star
78

cardo

DSL for creating recurring Pivotal Tracker stories
Ruby
2
star
79

ember-engine-external-admin

Ember-Engine Example: External Admin
JavaScript
2
star
80

html5-course-ajax-dummy

Just a dummy AJAX endpoint for the HTML5 course.
Ruby
2
star
81

CrashTest

UIKit Dynamics Playground
Objective-C
2
star
82

KotlinConf2023-ContextReceivers

The supporting code for Bryan Sills's talk titled "Introduction To Context Receivers" at KotlinConf 2023
Kotlin
2
star
83

constraints_experiments

Some experiments for a blog post
Elixir
2
star
84

BNR-graphql-java-samples

Holds some meaningful examples of building GraphQL API's in Java.
Java
2
star
85

nybblr-javascript-book-walkthrough

My run through of the frontend dev book.
JavaScript
2
star
86

AtomicSwift

Objective-C
1
star
87

BNR-blog-React-to-Vue-vue-simple

Vue project for comparison to React for blog entry
Vue
1
star
88

gatsby-twenties-tribute

A tribute page to the twenties built with Gatsby.js
CSS
1
star
89

ml-boston-pricer

A repo for hosting the demo project for BNR's Core ML blog post.
Swift
1
star
90

service-object-testing-example

Java
1
star
91

devnulldb

:trollface: MySQL server
JavaScript
1
star
92

clash_of_the_coders

Site for Clash of the Coders scoreboard
Ruby
1
star
93

omniauth-stable

Omniauth Strategy for Stable
Ruby
1
star
94

KotlinConf2023-ComposeForDesktop

The supporting code for Bryan Sills's codelab titled "Compose For Desktop" at KotlinConf 2023
Kotlin
1
star
95

RubyCourseResources

Files and helpful info associated with the Ruby portion of our Beginning Ruby on Rails Bootcamp.
1
star
96

carkeeper

Sample code accompanying @lyricsboy's talk "Applying the Single Responsibility Principle in iOS"
Objective-C
1
star
97

ios-ci-example

Example Distribution for the Travis CI Integration of a sample app
Objective-C
1
star
98

TableCollectionView

Handful of classes that make it easier to use a Collection View as a replacement for a Table View
1
star
99

iOS-Win8-Seminar-Public

Files for use in the exercises of the "Windows Store Development for iOS Developers" seminar.
1
star
100

iOSCourseResources

1
star