Skip to content

Commit

Permalink
feature(SabyJSON): add raw
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOF committed Aug 25, 2023
1 parent 8fd8ce8 commit 201ba2d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Source/JSON/Modify/JSONUnwrap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ extension JSON {
guard case .array(let value) = self else { return nil }
return value
}

public var raw: Any? {
if case .string(let value) = self { return value }
if case .number(let value) = self { return value }
if case .boolean(let value) = self { return value }
if case .object(let value) = self {
return value.mapValues { $0.raw }
}
if case .array(let value) = self {
return value.map { $0.raw }
}
return nil
}
}

extension JSON {
Expand Down
19 changes: 19 additions & 0 deletions Test/JSON/Modify/JSONUnwrapTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ final class JSONUnwrapTest: XCTestCase {
XCTAssertEqual(json.rawArray, [])
}

func test__raw() {
let json = JSON.from([
"a": "a",
"b": 1,
"c": [
"a": "a",
"b": 1,
"c": true
],
"d": [
"a",
1,
true
],
"e": nil
])
XCTAssertEqual(try JSON.from(unsafe: json.raw), json)
}

func test__is_string() {
let json = JSON.from("string")
XCTAssertTrue(json.isString)
Expand Down

0 comments on commit 201ba2d

Please sign in to comment.