All Projects → zoul → Generic Json Swift

zoul / Generic Json Swift

Licence: mit
A simple Swift library for working with generic JSON structures

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Generic Json Swift

Cleanjson
Swift JSON decoder for Codable
Stars: ✭ 178 (+87.37%)
Mutual labels:  json, codable
Codability
Useful helpers for working with Codable types in Swift
Stars: ✭ 125 (+31.58%)
Mutual labels:  json, codable
Ladybug
A powerful model framework for Swift 4
Stars: ✭ 147 (+54.74%)
Mutual labels:  json, codable
Keyedcodable
Easy nested key mappings for swift Codable
Stars: ✭ 248 (+161.05%)
Mutual labels:  json, codable
Undictify
Python library providing type-checked function calls at runtime
Stars: ✭ 97 (+2.11%)
Mutual labels:  type-safety, json
Jsontocodable
A generating tool from Raw JSON to Codable (Swift4) text written in Swift4.
Stars: ✭ 33 (-65.26%)
Mutual labels:  json, codable
Swiftai
SwiftAI, write Swift code smart. SwiftAI can generate Model class from JSON now. Codable and HandyJSON is supported. More features will be add.
Stars: ✭ 470 (+394.74%)
Mutual labels:  json, codable
Adaptivecardui
Snippets of UI, authored in JSON and rendered with SwiftUI
Stars: ✭ 73 (-23.16%)
Mutual labels:  json, codable
Go
A high-performance 100% compatible drop-in replacement of "encoding/json"
Stars: ✭ 10,248 (+10687.37%)
Mutual labels:  json
Metayaml
A powerful schema validator!
Stars: ✭ 92 (-3.16%)
Mutual labels:  json
Tabtoy
高性能表格数据导出器
Stars: ✭ 1,302 (+1270.53%)
Mutual labels:  json
Filecontextcore
FileContextCore is a "Database"-Provider for Entity Framework Core and adds the ability to store information in files instead of being limited to databases.
Stars: ✭ 91 (-4.21%)
Mutual labels:  json
Night Config
Powerful java configuration library for toml, yaml, hocon, json and in-memory configurations
Stars: ✭ 93 (-2.11%)
Mutual labels:  json
Jsonmapper
Map nested JSON structures onto PHP classes
Stars: ✭ 1,306 (+1274.74%)
Mutual labels:  json
Swagger Merger
🔗 Merge multiple swagger files into a swagger file, support JSON/YAML.
Stars: ✭ 94 (-1.05%)
Mutual labels:  json
Catj
Displays JSON files in a flat format.
Stars: ✭ 1,301 (+1269.47%)
Mutual labels:  json
Tanka
Flexible, reusable and concise configuration for Kubernetes
Stars: ✭ 1,299 (+1267.37%)
Mutual labels:  json
Awesome Resume For Chinese
📄 适合中文的简历模板收集(LaTeX,HTML/JS and so on)由 @hoochanlon 维护
Stars: ✭ 1,324 (+1293.68%)
Mutual labels:  json
Restson Rust
Easy-to-use REST client for Rust programming language
Stars: ✭ 93 (-2.11%)
Mutual labels:  json
Python Training For Network Engineers
Python hands-on training for network engineers. How to automate Junos with Python
Stars: ✭ 92 (-3.16%)
Mutual labels:  json

Generic JSON

Build Status

Generic JSON makes it easy to deal with freeform JSON strings without creating a separate, well-typed structure.

Codable and freeform JSON

Swift 4 introduced a new JSON encoding and decoding machinery represented by the Codable protocol. The feature is very nice and very type-safe, meaning it’s no longer possible to just willy-nilly decode a JSON string pulling random untyped data from it. Which is good™ most of the time – but what should you do when you do want to just willy-nilly encode or decode a JSON string without introducing a separate, well-typed structure for it? For example:

// error: heterogeneous collection literal could only be inferred to '[String : Any]';
// add explicit type annotation if this is intentional
let json = [
    "foo": "foo",
    "bar": 1,
]

// Okay then:
let json: [String:Any] = [
    "foo": "foo",
    "bar": 1,
]

// But: fatal error: Dictionary<String, Any> does not conform to Encodable because Any does not conform to Encodable.
let encoded = try JSONEncoder().encode(json)

So this doesn’t work very well. Also, the json value can’t be checked for equality with another, although arbitrary JSON values should support equality. Enter JSON.

Usage

Create a JSON structure

let json: JSON = [
    "foo": "foo",
    "bar": 1,
]

// "{"bar":1,"foo":"foo"}"
let str = try String(data: try JSONEncoder().encode(json), encoding: .utf8)!
let hopefullyTrue = (json == json) // true!

Convert Encodable objects into a generic JSON structure

struct Player: Codable {
    let name: String
    let swings: Bool
}

let val = try JSON(encodable: Player(name: "Miles", swings: true))
val == [
    "name": "Miles",
    "swings": true,
] // true

Query Values

Consider the following JSON structure:

let json: JSON = [
    "num": 1,
    "str": "baz",
    "bool": true,
    "obj": [
        "foo": "jar",
        "bar": 1,
    ]
]

Querying values can be done using optional property accessors, subscripting or dynamic member subscripting:

// Property accessors
if let str = json.objectValue?["str"]?.stringValue {  }
if let foo = json.objectValue?["obj"]?.objectValue?["foo"]?.stringValue {  }

// Subscripting
if let str = json["str"]?.stringValue {  }
if let foo = json["obj"]?["foo"]?.stringValue {  }

// Dynamic member subscripting
if let str = json.str?.stringValue {  }
if let foo = json.obj?.foo?.stringValue {  }

You may even drill through nested structures using a dot-separated key path:

let val = json[keyPath: "obj.foo"] // "jar"
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].