javascript
swiftyjson_是时候放弃SwiftyJSON了
swiftyjson
1. JSON解析是基礎的一部分 (1. JSON Parsing Is Part of the Foundation)
Photo by Mirko Blicke on Unsplash. Mirko Blicke在Unsplash上的照片 。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)
Photo by David Guliciuc on Unsplash. David Guliciuc在Unsplash上的照片 。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)
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時,它比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)} : nilIt 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)
Photo by Brina Blum on Unsplash Brina Blum在Unsplash上的照片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)
Photo by Olav Ahrens R?tne on Unsplash.由Olav AhrensR?tne在Unsplash上拍攝 。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
總結
以上是生活随笔為你收集整理的swiftyjson_是时候放弃SwiftyJSON了的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Linux 二进制分析
- 下一篇: 机器学习数据集
