Saagar Jha

> On Jan 29, 2018, at 13:25, Quincey Morris 
> <quinceymor...@rivergatesoftware.com> wrote:
> 
> On Jan 29, 2018, at 12:42 , Eric E. Dolecki <edole...@gmail.com> wrote:
>> 
>> So this would do it?
> 
> I would strongly recommend using JSONDecoder instead of JSONSerialization. 
> The errors JSONDecoder throws are AFAIK more detailed than JSONSerialization, 
> and will tell you the exact location in the string of any error you find.

Uhh…JSONDecoder swallows all of JSONSerialization’s errors and wraps it into 
its own 
<https://github.com/apple/swift/blob/e5fdc0955ce662bd929c7e1706d4a1f1d0f5d397/stdlib/public/SDK/Foundation/JSONEncoder.swift#L1097>.
 I’m not sure this counts as “more detailed”.

> 
> Also, FWIW, I beg you not to use this pattern (if you actually do, outside of 
> this sample fragment):
> 
>>       let jsonData = jsonString.data(using: String.Encoding.utf8)
>>      …
>>           _ = try JSONSerialization.jsonObject(with: jsonData!)
> 
> 
> but do this instead:
> 
>>       let jsonData = jsonString.data(using: String.Encoding.utf8)!
>>      …
>>           _ = try JSONSerialization.jsonObject(with: jsonData)
> 
> 
> That is, don’t let optionals “escape” from the place where they first appear. 
> If, in real code, you’re going to make the string decoding failure a handled 
> error too, then this would of course become:
> 
>>       if let jsonData = jsonString.data(using: String.Encoding.utf8) {
>>      …
>>      …
>>           _ = try JSONSerialization.jsonObject(with: jsonData)
> 
> 
> where the optional still doesn’t escape from its point of appearance.
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/saagar%40saagarjha.com
> 
> This email sent to saa...@saagarjha.com

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to