{"id":20945183,"url":"https://github.com/tbxark/jsonelement","last_synced_at":"2025-05-14T01:31:36.011Z","repository":{"id":56922994,"uuid":"73168480","full_name":"TBXark/JSONElement","owner":"TBXark","description":"JSONElement makes it easier and safer to use JSON","archived":false,"fork":false,"pushed_at":"2022-05-18T07:29:18.000Z","size":46,"stargazers_count":6,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-04-26T12:01:19.100Z","etag":null,"topics":["json-mapper"],"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/TBXark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-08T09:09:13.000Z","updated_at":"2023-10-13T06:35:39.000Z","dependencies_parsed_at":"2022-08-21T05:20:15.999Z","dependency_job_id":null,"html_url":"https://github.com/TBXark/JSONElement","commit_stats":null,"previous_names":["tbxark/jsonmapper"],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TBXark%2FJSONElement","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TBXark%2FJSONElement/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TBXark%2FJSONElement/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TBXark%2FJSONElement/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TBXark","download_url":"https://codeload.github.com/TBXark/JSONElement/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225269352,"owners_count":17447513,"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":["json-mapper"],"created_at":"2024-11-18T23:47:04.991Z","updated_at":"2024-11-18T23:47:05.645Z","avatar_url":"https://github.com/TBXark.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# JSONElement\n\n`JSONElement` is a simple, fast and secure way to access Json.\n\nBecause `Any` can't be used in `Codable`, it can be replaced with `JSONElement` for properties of type `Any`.\n\n\n\n## Installation\n\n### Swift Package Manager\n\nAdd the dependency in your `Package.swift` file:\n\n```swift\nlet package = Package(\n    name: \"myproject\",\n    dependencies: [\n        .package(url: \"https://github.com/TBXark/JSONElement.git\", .upToNextMajor(from: \"1.4.0\"))\n        ],\n    targets: [\n        .target(\n            name: \"myproject\",\n            dependencies: [\"JSONElement\"]),\n        ]\n)\n```\n\n### Carthage\n\nAdd the dependency in your `Cartfile` file:\n\n```bash\ngithub \"TBXark/JSONElement\" ~\u003e 1.4.0.\n```\n\n### CocoaPods\n\nAdd the dependency in your `Podfile` file:\n\n```ruby\npod 'JSONElement'\n```\n\n# Example\n\n```swift\n\nfinal class JSONElementTests: XCTestCase {\n    struct Human: Codable {\n        let age: Int\n        let name: String\n        let height: Double\n        let extra: JSONElement // Any?\n    }\n    \n    let dict = [\"data\": [\"man\": [\"age\": 10, \"name\": \"Peter\", \"height\": 180.0, \"extra\": [123, \"123\", [123], [\"123\": 123], true]]]]\n\n    \n    func testJSONElement() throws {\n\n        let json = try JSONElement(rawJSON: dict)\n        // 使用dynamicMemberLookup直接获取\n        XCTAssertEqual(json.data.man.age.intValue, 10)\n\n        // 使用Key获取\n        XCTAssertEqual(json[\"data\"][\"man\"][\"height\"].decimalValue, 180.0)\n\n        // 使用Keypath获取\n        XCTAssertEqual(json[keyPath: \"data.man.name\"].stringValue, \"Peter\")\n        \n        // 将不确定类型对象解析为JSONElement\n        XCTAssertEqual(json.data.man.extra.arrayValue?.first?.intValue , 123)\n        XCTAssertEqual(try? json.data.man.extra.arrayValue?.last?.as(Bool.self), true)\n\n    }\n\n\n    func testJSONMapper() throws {\n\n        let json = JSONMapper(dict)\n\n        // 使用 dynamicMemberLookup 获取\n        XCTAssertEqual(json.data.man.height.as(Double.self), 180.0)\n\n        // 使用Key获取\n        XCTAssertEqual(json[\"data\"][\"man\"][\"age\"].intValue, 10)\n        XCTAssertEqual(json[\"data\"][\"man\"][\"height\"].as(Double.self), 180.0)\n\n        // 使用Keypath获取\n        XCTAssertEqual(json[keyPath: \"data.man.name\"].as(String.self), \"Peter\")\n\n        // 将不确定类型对象解析为JSONElement\n        XCTAssertEqual(try? json[keyPath: \"data.man.extra\"].as(JSONElement.self)?.arrayValue?.last?.as(Bool.self), true)\n\n    }\n\n    static var allTests = [\n        (\"testJSONElement\", testJSONElement)\n        (\"testJSONMapper\", testJSONMapper),\n    ]\n}\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftbxark%2Fjsonelement","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftbxark%2Fjsonelement","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftbxark%2Fjsonelement/lists"}