Skip to content

Commit 8045bb9

Browse files
committed
refactor: decode method
1 parent 530c50c commit 8045bb9

File tree

3 files changed

+11
-29
lines changed

3 files changed

+11
-29
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import PackageDescription
77
let packageDependencies: [Package.Dependency] = [
88
.package(
99
url: "https://github.com/ibireme/yyjson.git",
10-
from: "0.11.1"
10+
from: "0.12.0"
1111
),
1212
]
1313
#else

Sources/ReerJSON/ReerJSONDecoder.swift

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ open class ReerJSONDecoder {
170170
///
171171
/// - parameter type: The type of the value to decode.
172172
/// - parameter data: The data to decode from.
173-
/// - parameter path: The decoding container path.
173+
/// - parameter path: The decoding container path, `["user", "info"]`
174174
/// - returns: A value of the requested type.
175175
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid JSON.
176176
/// - throws: An error if any value throws an error during decoding.
@@ -204,34 +204,16 @@ open class ReerJSONDecoder {
204204
///
205205
/// - parameter type: The type of the value to decode.
206206
/// - parameter data: The data to decode from.
207-
/// - parameter keyPath: The decoding container path, "user.info".
207+
/// - parameter path: The decoding container path, `"user.info"`
208208
/// - returns: A value of the requested type.
209209
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid JSON.
210210
/// - throws: An error if any value throws an error during decoding.
211-
open func decode<T: Decodable>(_ type: T.Type, from data: Data, keyPath: String) throws -> T {
212-
let doc = data.withUnsafeBytes {
213-
yyjson_read(
214-
$0.bindMemory(to: CChar.self).baseAddress,
215-
data.count,
216-
YYJSON_READ_NUMBER_AS_RAW
217-
)
218-
}
219-
guard let doc else {
220-
return try decodeWithFoundationDecoder(type, from: data)
221-
}
222-
223-
defer {
224-
yyjson_doc_free(doc)
225-
}
226-
227-
var pointer = yyjson_doc_get_root(doc)
228-
for key in keyPath.components(separatedBy: CharacterSet(charactersIn: ".")) {
229-
pointer = key.withCString { yyjson_obj_get(pointer, $0) }
230-
}
231-
232-
let json = JSON(pointer: pointer)
233-
let impl = JSONDecoderImpl(json: json, userInfo: userInfo, codingPathNode: .root, options: options)
234-
return try impl.unbox(json, as: type, for: .root, _CodingKey?.none)
211+
open func decode<T: Decodable>(_ type: T.Type, from data: Data, path: String) throws -> T {
212+
return try decode(
213+
type,
214+
from: data,
215+
path: path.components(separatedBy: CharacterSet(charactersIn: "."))
216+
)
235217
}
236218

237219
func decodeWithFoundationDecoder<T : Decodable>(_ type: T.Type, from data: Data) throws -> T {

Tests/ReerJSONTests/ReerJSONDecoderTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ class ReerJSONTests: XCTestCase {
933933
""".data(using: .utf8)!
934934
let model = try ReerJSONDecoder().decode(Test.self, from: data, path: ["a", "b"])
935935
XCTAssert(model.c == "ddd")
936-
let model3 = try ReerJSONDecoder().decode(Test.self, from: data, keyPath: "a.b")
936+
let model3 = try ReerJSONDecoder().decode(Test.self, from: data, path: "a.b")
937937
XCTAssert(model3.c == "ddd")
938938

939939
struct Test2: Decodable {
@@ -944,7 +944,7 @@ class ReerJSONTests: XCTestCase {
944944
""".data(using: .utf8)!
945945
let model2 = try ReerJSONDecoder().decode(Test2.self, from: data2, path: ["a", "b"])
946946
XCTAssert(model2.c == [1, 2, 3])
947-
let model4 = try ReerJSONDecoder().decode(Test2.self, from: data2, keyPath: "a.b")
947+
let model4 = try ReerJSONDecoder().decode(Test2.self, from: data2, path: "a.b")
948948
XCTAssert(model4.c == [1, 2, 3])
949949
}
950950
}

0 commit comments

Comments
 (0)