swiftyjson_是时候放弃SwiftyJSON了

swiftyjson

1. JSON解析是基础的一部分 (1. JSON Parsing Is Part of the Foundation)

Image for post
Photo by Mirko Blicke on Unsplash.
Mirko BlickeUnsplash上的 照片

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)

Image for post
Photo by David Guliciuc on Unsplash.
David GuliciucUnsplash上的 照片

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.

但是现在它仍然兼容吗? 让我们开始进行基准测试以比较这三种方法:原始JSONSerializationSwiftyJSONJSONDecoder

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.

TweetComment对象是反序列化并映射到的JSON,它们采用Codable协议来支持JSONDecoder 。 他们也有两个初始化器,一个用于DictionaryJSONSerialization ,另一个用于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:

这三种方法将运行10100100010,000100,000的范围内多次measure在Xcode的单元测试块进行比较的时候消耗:

3.结果与分析 (3. Outcome and Analysis)

Image for post
Photo by the author (made with VISME).
作者提供的照片(由VISME制作)。

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时,它比JSONDecoderJSONDecoder ,比JSONSerializationJSONSerialization

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)

Image for post
Photo by Brina Blum on Unsplash
Brina BlumUnsplash上的 照片

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)

Image for post
Photo by Olav Ahrens Røtne on Unsplash.
Olav AhrensRøtneUnsplash拍摄

All the repo and code mentioned in this article can be found on GitHub.

本文提到的所有回购和代码都可以在GitHub上找到

翻译自: https://medium.com/better-programming/time-to-abandon-swiftyjson-switch-jsondecoder-codable-407f9988daec

swiftyjson

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值