• Stars
    star
    164
  • Rank 230,032 (Top 5 %)
  • Language
    Swift
  • License
    MIT License
  • Created almost 4 years ago
  • Updated about 3 years ago

Reviews

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

Repository Details

Flexible JSON traversal for rapid prototyping.

RBBJSON

Swift Version: 5.2 Swift Package Manager Swift Package Manager Twitter: @DLX

RBBJSON enables flexible JSON traversal at runtime and JSONPath-like querying for rapid prototyping.

Use JSONDecoder to create an RBBJSON struct, then traverse it using dynamic member lookup:

let json = try JSONDecoder().decode(RBBJSON.self, from: data)

json.firstName         // RBBJSON.string("John")
json.lastName          // RBBJSON.string("Appleseed")
json.age               // RBBJSON.number(26)
json.invalidKey        // RBBJSON.null
json.phoneNumbers[0]   // RBBJSON.string("+14086065775")

If you want to access a value that coincides with a Swift-defined property, use a String subscript instead:

json.office.map     // Error: Maps to Sequence.map
json.office["map"]  // RBBJSON.string("https://maps.apple.com/?q=IL1")

To unbox a JSON value, use one of the failable initializers:

String(json.firstName) // "John"
String(json.lastName)  // "Appleseed"
String(json.age)       // nil

Int(json.age)          // 26
Double(json.age)       // 26.0

You can also make use of a JSONPath-inspired Query syntax to find nested data inside a JSON structure.

For example, given:

{ 
  "store": {
    "book": [ 
      { 
        "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { 
        "category": "fiction",
        "author": "Evelyn Waugh",
        "title": "Sword of Honour",
        "price": 12.99
      },
      { 
        "category": "fiction",
        "author": "Herman Melville",
        "title": "Moby Dick",
        "isbn": "0-553-21311-3",
        "price": 8.99
      },
      { 
        "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}
JSONPath RBBJSON Result
$.store.book[*].author json.store.book[any: .child].author The authors of all books in the store.
$..author json[any: .descendantOrSelf].author All authors.
$.store.* json.store[any: .child] All things in the store, a list of books an a red bycicle.
$.store..price json.store[any: .descendantOrSelf].price All prices in the store.
$..book[2] json[any: .descendantOrSelf].book[2] The second book.
$..book[-2] json[any: .descendantOrSelf].book[-2] The second-to-last book.
$..book[0,1], $..book[:2] json[any: .descendantOrSelf].book[0, 1]), json[any: .descendantOrSelf].book[0...1]), json[any: .descendantOrSelf].book[0..<2]) The first two books.
$..book[?(@.isbn)] json[any: .descendantOrSelf].book[has: \.isbn] All books with an ISBN number.
$..book[?(@.price<10)] json.store.book[matches: { $0.price <= 10 }] All books cheaper than 10.
$.store["book", "bicycle"]..["price", "author"] json.store["book", "bicycle"][any: .descendantOrSelf]["price", "author"] The author (where available) and price of every book or bicycle.

Once you query a JSON value using one of the higher order selectors, the resulting type of the expression will be a lazy RBBJSONQuery:

json.store.book[0]["title"]     // RBBJSON.string("Sayings of the Century")
json.store.book[0, 1]["title"]  // some RBBJSONQuery

Because RBBJSONQuery conforms to Sequence, you can initialize an Array with it to obtain the results or use e.g. compactMap:

String(json.store.book[0].title)                    // "Sayings of the Century"
json.store.book[0, 1].title.compactMap(String.init) // ["Sayings of the Century", "Sword of Honour"]

String(json.store.book[0]["invalid Property"])                    // nil
json.store.book[0, 1]["invalid Property"].compactMap(String.init) // []

More Repositories

1

Cartography

A declarative Auto Layout DSL for Swift 📱📐
Swift
7,338
star
2

hamburger-button

A hamburger button transition
Swift
2,308
star
3

RBBAnimation

Block-based animations made easy, comes with easing functions and a CASpringAnimation replacement.
Objective-C
2,061
star
4

Underscore.m

A DSL for Data Manipulation
Objective-C
1,465
star
5

FLXView

A UIView that uses Flexbox for layouting. ✨
Objective-C
481
star
6

Swim

A DSL for writing HTML in Swift
Swift
310
star
7

Asterism

Asterism is yet another functional toolbelt for Objective-C. It tries to be typesafe and simple.
Objective-C
226
star
8

Fantastical-Alfred-Workflow

A simple Alfred 2 workflow for Fantastical.
158
star
9

NES.swift

An NES emulator written in Swift
Swift
149
star
10

dotfiles

Dotfiles to make computing personal.
Shell
87
star
11

Stubbilino

Simple stubbing for Objective-C
Objective-C
86
star
12

swamp

icon stamping in Swift
Swift
85
star
13

Peel-Off-Animation-Example-Code

Example code for https://robb.is/working-on/a-peel-off-animation
Swift
62
star
14

Monocle

Pretty much only a Lens
Swift
45
star
15

ShaderBugs

Some isssues I ran into with SwiftUI.Shader
Swift
36
star
16

robb.swift

My personal website ported to Swift
Swift
34
star
17

Xcode-Configurations

Useful tweaks to Xcode
Objective-C
30
star
18

jekyll-embedly-client

No longer maintained
Ruby
23
star
19

URLRequest-AWS

An extension on URLRequest to sign it for AWS.
Swift
20
star
20

Marbleo.us

A marbleous project
CoffeeScript
18
star
21

Digitale-Zivilgesellschaft

Recommendations from multiple civil society organisations that fight for independent digital infrastructure and open access to knowledge.
HTML
11
star
22

laughing-man

The Laughing Man logo implemented in pure HTML/CSS
6
star
23

monome.js

A library for writing monome apps with node.js
C++
5
star
24

slang

The pxl effect in the browser
CoffeeScript
4
star
25

spinamp

Winamp inside Spotify – it really whips the moose's ass
CoffeeScript
2
star
26

FeedImporter

Import RSS feeds into SoundCloud
Ruby
1
star
27

thedickensbar.com

Put a Dickensbar on top of every page. Lost all its meaning now that the dickbar is gone…
CSS
1
star
28

6strings

Guitar synthesis in the browser!
CoffeeScript
1
star
29

Future

A simple, cold Future.
Swift
1
star