swiftyjson
1. JSON解析是基础的一部分 (1. JSON Parsing Is Part of the Foundation)
There are handfuls of well-known projects dealing with JSON parsing employing various approaches and philosophies. SwiftyJSON
is probably the earliest and most popular one among them. It’s less verbose and error-prone and leverages Swift’s powerful type system to handle all of the details.
有许多著名的项目都在使用各种方法和理念来处理JSON解析。 SwiftyJSON
可能是其中最早,最受欢迎的一种。 它不那么冗长且容易出错,并且利用Swift强大的类型系统来处理所有细节。
JSONSerialization
is the core of most JSON parsing projects. It comes from Swift’s Foundation framework and converts JSON into different Swift data types. People used raw JSONSerialization
to parse JSON objects before projects like SwiftyJSON
came up. But it can be painful, as the value type and JSON structure may vary. You need to manually handle errors and cast the type Any
to Swift foundation types. It’s more error-prone.
JSONSerialization
是大多数JSON解析项目的核心。 它来自Swift的Foundation框架,并将JSON转换为不同的Swift数据类型。 在出现SwiftyJSON
项目之前,人们使用原始的JSONSerialization
解析JSON对象。 但这可能会很痛苦,因为值类型和JSON结构可能会有所不同。 您需要手动处理错误并将类型Any
为Swift基础类型。 这更容易出错。
JSONDecoder
was announced with Swift 4 with more advanced and promising features. It decodes JSON objects to Swift objects and adopts the Decodable
protocol.
Swift 4宣布了JSONDecoder
,它具有更多高级功能和有前途的功能。 它将JSON对象解码为Swift对象,并采用Decodable
协议。
2.绩效基准 (2. Performance Benchmarking)
Looking back to the past few years, SwiftyJSON
has been playing an important role in our project by saving us from the raw JSONSerialization
pains.
回顾过去的几年, SwiftyJSON
通过使我们摆脱了原始的JSONSerialization
痛苦而一直在我们的项目中扮演重要角色。
But is it still compatible nowadays? Let’s start our benchmarking to compare these three approaches: the raw JSONSerialization
, SwiftyJSON
,and JSONDecoder
.
但是现在它仍然兼容吗? 让我们开始进行基准测试以比较这三种方法:原始JSONSerialization
, SwiftyJSON
和JSONDecoder
。
The following simple Tweet
JSON contains an array of Comments
and a nested array of Replies
:
以下简单的Tweet
JSON包含一个Comments
数组和一个嵌套的Replies
数组:
Tweet
and Comment
objects are the JSON deserialized and mapped to, and they adopt the Codable
protocol to support JSONDecoder
. They also have two initializers, one for Dictionary
from JSONSerialization
and another for the JSON
object from SwiftyJSON
.
Tweet
和Comment
对象是反序列化并映射到的JSON,它们采用Codable
协议来支持JSONDecoder
。 他们也有两个初始化器,一个用于Dictionary
从JSONSerialization
,另一个用于JSON
的对象SwiftyJSON
。
The three approaches will be running 10
, 100
, 1000
, 10,000
, and 100,000
times within the measure
block in Xcode’s unit test to compare the time consumption:
这三种方法将运行10
, 100
, 1000
, 10,000
和100,000
的范围内多次measure
在Xcode的单元测试块进行比较的时候消耗:
3.结果与分析 (3. Outcome and Analysis)
![Image for post](https://miro.medium.com/max/9999/1*3qMQW0PGSMbBge2mhCeC3Q.png)
SwiftyJSON
has the steepest curve of time consumption when the loop accumulates. The data shows it’s three times slower than JSONDecoder
and almost six times slower than JSONSerialization
when the loop goes to 100,000
.
循环累积时, SwiftyJSON
的时间消耗曲线SwiftyJSON
陡峭。 数据显示,当循环达到100,000
时,它比JSONDecoder
慢JSONDecoder
,比JSONSerialization
慢JSONSerialization
。
What makes it so slow?
是什么让它这么慢?
SwiftyJSON
uses JSONSerialization
to deserialize the JSON object:
SwiftyJSON
使用JSONSerialization
反序列化JSON对象:
The bottleneck is obviously in the object mapping and retrieving process. Part of the reason was found when I looked into this line of code:
瓶颈显然在对象映射和检索过程中。 当我查看以下代码行时,找到了部分原因:
return type == .array ? rawArray.map { JSON($0)} : nil
It loops and maps every single object to the JSON
object when retrieving values. When we handle nested arrays, the time complexity and space complexity are growing exponentially.
检索值时,它将循环并将每个对象映射到JSON
对象。 当我们处理嵌套数组时,时间复杂度和空间复杂度呈指数增长。
4。结论 (4. Conclusion)
The raw JSONSerialization
approach has outstanding performance but isn’t ideal when dealing with real-world JSON. No one likes chained optional, error-prone, and verbose code:
原始JSONSerialization
方法具有出色的性能,但在处理实际JSON时并不理想。 没有人喜欢链接的可选,容易出错和冗长的代码:
JSONDecoder
is the go-to direction in my opinion due to its beautiful balance of performance and usability:
我认为JSONDecoder
是性能和可用性之间的完美平衡,因此是我的JSONDecoder
方向:
SwiftyJSON
has the worst performance and is relatively verbose compared with JSONDecoder
. Code like this is better than with raw JSONSerialization
but not as clean as with JSONDecoder
:
SwiftyJSON
性能最差,并且与JSONDecoder
相比比较冗长。 这样的代码是比生吃效果更好JSONSerialization
但不作为清洁与JSONDecoder
:
In this article, we benchmarked the three approaches. Both the performance and code usability prove it’s time to abandon SwiftyJSON
and immigrate to JSONDecoder
!
在本文中,我们对这三种方法进行了基准测试。 性能和代码可用性都证明了该放弃SwiftyJSON
并迁移到JSONDecoder
!
5.资源 (5. Resources)
All the repo and code mentioned in this article can be found on GitHub.
本文提到的所有回购和代码都可以在GitHub上找到 。
swiftyjson