{"id":21376512,"url":"https://github.com/fluidgroup/jayson","last_synced_at":"2025-04-06T19:13:03.808Z","repository":{"id":11086010,"uuid":"68098802","full_name":"FluidGroup/JAYSON","owner":"FluidGroup","description":"🧱 A JSON decoding/encoding library that handles optimistically or strictly.","archived":false,"fork":false,"pushed_at":"2024-09-20T13:39:39.000Z","size":813,"stargazers_count":255,"open_issues_count":7,"forks_count":18,"subscribers_count":12,"default_branch":"main","last_synced_at":"2024-11-24T09:34:59.282Z","etag":null,"topics":["decoder","ios","json","linux","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/FluidGroup.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["muukii"],"patreon":"muukii","ko_fi":"muukii"}},"created_at":"2016-09-13T10:31:10.000Z","updated_at":"2024-10-23T02:25:50.000Z","dependencies_parsed_at":"2024-12-06T08:31:38.883Z","dependency_job_id":"3437e1dd-3e7f-476c-8ecb-c09a133f31fb","html_url":"https://github.com/FluidGroup/JAYSON","commit_stats":{"total_commits":153,"total_committers":6,"mean_commits":25.5,"dds":0.3529411764705882,"last_synced_commit":"59e9a669b0d18a6536f3592a22a6d08538a718ec"},"previous_names":["muukii/jayson"],"tags_count":47,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2FJAYSON","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2FJAYSON/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2FJAYSON/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/FluidGroup%2FJAYSON/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/FluidGroup","download_url":"https://codeload.github.com/FluidGroup/JAYSON/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247535519,"owners_count":20954576,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decoder","ios","json","linux","swift"],"created_at":"2024-11-22T09:16:01.356Z","updated_at":"2025-04-06T19:13:03.785Z","avatar_url":"https://github.com/FluidGroup.png","language":"Swift","readme":"\n# Do you need to handle the root cause of failure in decoding JSON?\n\nWe often process the value as a default value if it could not be decoded from JSON. (Recovering with a default value)  \nHowever, doing that might cause a serious problem and hide the actual root cause in the app.  \nRecovering with a default value is not a bad choice, it's important to know the JSON represents unexpected shape or value before recovering.  \n\n```swift\nlet json: JSON\n\ndo {\n  self.id = try json.next(\"id\").getString()\n} catch {\n  print(error)\n  // We can know why decoding failed from error.\n  // Not found \"id\" or \"id\" found but it was not `string` or something else.\n  // that's why here recover the value to fill `self.id`\n  self.id = \"unknown\"\n}\n```\n\n---\n\nJAYSON provides 2 ways of accessing to JSON object.\n\n1. Easy access (with dynamic-member-lookup)\n\n```swift\nlet urlString: String? = json[3]?.shot?.images?.hidpi_image?.string\n```\n\n2. Strict access (with dynamic-member-lookup)\n\nWe can know where error was caused. (with JSONError)\n\n```swift\nlet id: String = try json\n    .next(0)\n    .next(\"id\")\n    .getString()\n```\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src=\"banner@2x.png\" width=375\u003e\n\u003c/p\u003e\n\n# JAYSON\n\n[![Build Status](https://app.bitrise.io/app/0cc465d9351375ab/status.svg?token=c_MLug7GlJJn0F44V4o5hw\u0026branch=master)](https://app.bitrise.io/app/0cc465d9351375ab)\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmuukii%2FJAYSON.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmuukii%2FJAYSON?ref=badge_shield)\n![](https://img.shields.io/badge/Swift-4.2-blue.svg?style=flat)\n[![Version](https://img.shields.io/cocoapods/v/json.svg?style=flat)](http://cocoapods.org/pods/json)\n[![License](https://img.shields.io/cocoapods/l/json.svg?style=flat)](http://cocoapods.org/pods/json)\n[![Platform](https://img.shields.io/cocoapods/p/json.svg?style=flat)](http://cocoapods.org/pods/json)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n\nStrict and Scalable JSON library.\nAnd also supports `dynamicMemberLookup`\n\n## Requirements\n\nSwift **5+**  iOS📱, watchOS⌚️, tvOS📺, macOS🖥, **Linux**✨\n\n# Usage\n\n# Read JSON\n\n## Easy Access\n\n```swift\nlet urlString: String? = json[3]?[\"shot\"]?[\"images\"]?[\"hidpi_image\"]?.string\n```\n\n## Strict Access (try-catch)\n\nif the value does not exist, throw `JSONError`\u003cbr\u003e\nFailed location can be known from [JSONError](#jsonerror)\n\nGet Value (String, Bool, Number)\n\n```swift\nlet id: String = try json\n    .next(0)\n    .next(\"id\")\n    .getString()\n```\n\n**Using dynamicMemberLookup**\n\n```swift\nlet id: String = try json\n    .next(0)\n    .next(\\.id)\n    .getString()\n```\n\n### Get Value with Decoder (Custom Object)\n\nUsing the Decoder can be transformed in a custom object.\nAnd, throwable\n\n```swift\n\nlet imageURL: URL = try json\n    .next(0)\n    .next(\"image\")\n    .next(\"hidpi_image\")\n    .get {\n        URL.init(string: try $0.getString())!\n    }\n```\n\n### General Getter\n\nStrict getters\n\n```swift\nextension JSON {\n    public func getDictionary() throws -\u003e [String : JSON]\n    public func getArray() throws -\u003e [JSON]\n    public func getNumber() throws -\u003e NSNumber\n    public func getInt() throws -\u003e Int\n    public func getInt8() throws -\u003e Int8\n    public func getInt16() throws -\u003e Int16\n    public func getInt32() throws -\u003e Int32\n    public func getInt64() throws -\u003e Int64\n    public func getUInt() throws -\u003e UInt\n    public func getUInt8() throws -\u003e UInt8\n    public func getUInt16() throws -\u003e UInt16\n    public func getUInt32() throws -\u003e UInt32\n    public func getUInt64() throws -\u003e UInt64\n    public func getString() throws -\u003e String\n    public func getBool() throws -\u003e Bool\n    public func getFloat() throws -\u003e Float\n    public func getDouble() throws -\u003e Double\n}\n\n///\nextension JSON {\n    public func get\u003cT\u003e(_ s: (JSON) throws -\u003e T) rethrows -\u003e T\n}\n```\n\nOptional Read-only properties😁\n```swift\nextension JSON {\n    public var dictionary: [String : Any]? { get }\n    public var array: [Any]? { get }\n    public var string: String? { get }\n    public var number: NSNumber? { get }\n    public var double: Double? { get }\n    public var float: Float? { get }\n    public var int: Int? { get }\n    public var uInt: UInt? { get }\n    public var int8: Int8? { get }\n    public var uInt8: UInt8? { get }\n    public var int16: Int16? { get }\n    public var uInt16: UInt16? { get }\n    public var int32: Int32? { get }\n    public var uInt32: UInt32? { get }\n    public var int64: Int64? { get }\n    public var uInt64: UInt64? { get }\n    public var bool: Bool? { get }\n}\n```\n\n# Initialize JSON\n\n```swift\nlet jsonData: Data = ...\nlet json = try JSON(data: jsonData)\n```\n\n```swift\nlet jsonData: Data\nlet json: Any = try JSONSerialization.jsonObject(with: data, options: [])\nlet json = try JSON(any: json)\n```\n\n```swift\nlet userInfo: [AnyHashable: Any]\nlet json = try JSON(any: json)\n```\n\n```swift\nlet objects: [Any]\nlet json = try JSON(any: json)\n```\n\nIn the case of the following try it is not required.\n\n```swift\nlet object: [String : JSON]\nlet json = JSON(object)\n```\n\n```swift\nlet object: [JSON]\nlet json = JSON(object)\n```\n\n```swift\nlet object: [JSONWritableType]\nlet json = JSON(object)\n```\n\n```swift\nlet object: [String : JSONWritableType]\nlet json = JSON(object)\n```\n---\n\n# Get current path (Debugging information.)\n\n```swift\n\nlet path = try json\n    .next(0)\n    .next(\"image\")\n    .next(\"hidpi_image\")\n    .currentPath()\n\n// path =\u003e \"[0][\"image\"][\"hidpi_image\"]\"\n```\n\n# JSONError\n\nIf you have access that does not exist key, throw　`JSONError`.\n\n```swift\npublic enum JSONError: Error {\n  case notFoundKey(key: String, json: JSON)\n  case notFoundIndex(index: Int, json: JSON)\n  case failedToGetString(source: Any, json: JSON)\n  case failedToGetBool(source: Any, json: JSON)\n  case failedToGetNumber(source: Any, json: JSON)\n  case failedToGetArray(source: Any, json: JSON)\n  case failedToGetDictionary(source: Any, json: JSON)\n  case decodeError(source: Any, json: JSON, decodeError: Error)\n  case invalidJSONObject\n}\n```\n\nexample:\n\n```swift\ndo {\n  let urlString: String = try json\n    .next(\"shots\")\n    .next(0)\n    .next(\"user\")\n    .next(\"profile_image\")\n    .next(\"foo\") // ‼️ throw\n    .getString()\n} catch {\n   print(error)\n}\n```\n\nOutput jsonError\n\n```\nnotFoundKey(\"foo\",\njson\nPath: Root-\u003e[\"shots\"][0][\"user\"][\"profile_image\"]\nSourceType: dictionary\n\nSource:\n{\n    large = \"https://...\";\n    medium = \"https://...\";\n    small = \"https://...\";\n})\n```\n\n# Go Back JSON hierarchy\n\n```swift\n\ntry json\n    .next(0)\n    .next(\"image\")\n    .back() // \u003c---\n    .next(\"image\")\n    .next(\"hidpi_image\")\n\n```\n\n# Import Example (dribbble API)\n\n```swift\nlet json = try! JSON(data)\n\nstruct Shot {\n    let id: Int\n    let title: String\n    let width: Int\n    let height: Int\n    let hidpiImageURLString: String?\n    let normalImageURLString: String\n    let teaserImageURLString: String\n}\n\ndo {\n    let shots: [Shot] = try json.getArray().map { json -\u003e Shot in\n\n        let imagesjson = try json.next(\"images\")\n\n        return Shot(\n            id: try json.next(\"id\").getInt(),\n            title: try json.next(\"title\").getString(),\n            width: try json.next(\"width\").getInt(),\n            height: try json.next(\"height\").getInt(),\n            hidpiImageURLString: try? imagesjson.next(\"hidpi\").getString(),\n            normalImageURLString: try imagesjson.next(\"normal\").getString(),\n            teaserImageURLString: try imagesjson.next(\"teaser\").getString()\n        )\n    }\n    print(shots)\n} catch {\n    print(error)\n}\n```\n\n## Write JSON\n\n```swift\nvar json = JSON()\njson[\"id\"] = 18737649\njson[\"active\"] = true\njson[\"name\"] = \"muukii\"\n\nvar images = [String:JSON]()\nimages[\"large\"] = \"http://...foo\"\nimages[\"medium\"] = \"http://...bar\"\nimages[\"small\"] = \"http://...fuzz\"\n\njson[\"images\"] = JSON(images)\n\nlet data = try json.data(options: .prettyPrinted)\n```\n\n-\u003e data\n```\n{\n  \"name\" : \"muukii\",\n  \"active\" : true,\n  \"id\" : 18737649,\n  \"images\" : {\n    \"large\" : \"http:\\/\\/...foo\",\n    \"small\" : \"http:\\/\\/...fuzz\",\n    \"medium\" : \"http:\\/\\/...bar\"\n  }\n}\n```\n\n### json Convertible Examples\n\n```swift\nvar json = JSON()\n\njson[\"String\"] = \"String\"\njson[\"NSString\"] = JSON(\"NSString\" as NSString)\njson[\"NSNumber\"] = NSNumber(value: 0)\njson[\"Int\"] = 64\njson[\"Int8\"] = JSON(8 as Int8)\njson[\"Int16\"] = JSON(16 as Int16)\njson[\"Int32\"] = JSON(32 as Int32)\njson[\"Int64\"] = JSON(64 as Int64)\n\njson[\"UInt\"] = JSON(64 as UInt)\njson[\"UInt8\"] = JSON(8 as UInt8)\njson[\"UInt16\"] = JSON(16 as UInt16)\njson[\"UInt32\"] = JSON(32 as UInt32)\njson[\"UInt64\"] = JSON(64 as UInt64)\n\njson[\"Bool_true\"] = true\njson[\"Bool_false\"] = false\n\njson[\"Float\"] = JSON(1.0 / 3.0 as Float)\njson[\"Double\"] = JSON(1.0 / 3.0 as Double)\njson[\"CGFloat\"] = JSON(1.0 / 3.0 as CGFloat)\n```\n\n# Installation\n\njson is available through [CocoaPods](http://cocoapods.org). To install\nit, simply add the following line to your Podfile:\n\n```ruby\npod \"JAYSON\"\n```\n\n# Author\n\nmuukii, muukii.app@gmail.com\n\n# License\n\njson is available under the MIT license. See the LICENSE file for more info.\n\n\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmuukii%2FJAYSON.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmuukii%2FJAYSON?ref=badge_large)\n","funding_links":["https://github.com/sponsors/muukii","https://patreon.com/muukii","https://ko-fi.com/muukii"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluidgroup%2Fjayson","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffluidgroup%2Fjayson","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffluidgroup%2Fjayson/lists"}