Re: Checking if a string is valid JSON

2018-01-29 Thread Quincey Morris
On Jan 29, 2018, at 13:30 , Saagar Jha  wrote:
> 
> Uhh…JSONDecoder swallows all of JSONSerialization’s errors and wraps it into 
> its own 
> .
>  I’m not sure this counts as “more detailed”.

Well, it presents all of JSONSerialization’s errors as DecodingError, but these 
are not all the DecodingErrors it can present. Also, AFAICT from a brief look 
at that code, DecodingError has a “codingPath” value that apparently locates 
the error within the source string. That seems unique to JSONDecoder.

OTOH, it may be that JSONSerialization errors are in fact as informative as 
JSONDecoder errors, in which case I mostly retract my comment. (I still think 
JSONDecoder is a better choice.)

___

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


Re: Checking if a string is valid JSON

2018-01-29 Thread Saagar Jha

Saagar Jha

> On Jan 29, 2018, at 13:25, Quincey Morris 
>  wrote:
> 
> On Jan 29, 2018, at 12:42 , Eric E. Dolecki  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 
.
 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


Re: Checking if a string is valid JSON

2018-01-29 Thread Quincey Morris
On Jan 29, 2018, at 12:42 , Eric E. Dolecki  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.

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/archive%40mail-archive.com

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


Re: Checking if a string is valid JSON

2018-01-29 Thread Jens Alfke


> On Jan 29, 2018, at 12:17 PM, Eric E. Dolecki  wrote:
> 
> I am generating a String of JSON. Before I use it, I want to check to make 
> sure that it's valid.

Is there a reason you need to generate the JSON by hand, instead of creating a 
Dictionary or Array and letting NSJSONSerialization encode it for you?
If so, are you sure you're handling all the string-quoting and floating-point 
encoding correctly?

—Jens
___

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


Re: Checking if a string is valid JSON

2018-01-29 Thread Eric E. Dolecki
Cool - thanks. So this would do it?


let jsonString = composedString

let jsonData = jsonString.data(using: String.Encoding.utf8)

do {

_ = try JSONSerialization.jsonObject(with: jsonData!)

print("json seems okay.")

} catch {

print("Error deserializing JSON: \(error)")

}



On Mon, Jan 29, 2018 at 3:34 PM Saagar Jha  wrote:

> I believe jsonObject(with:options) will throw if the JSON is invalid, so
> you might be able to get away with just the try/catch. Besides, your JSON
> top level object might be an array, in which case I’d expect that casting
> to an NSDictionary would fail.
>
> Saagar Jha
>
> On Jan 29, 2018, at 12:17, Eric E. Dolecki  wrote:
>
> I am generating a String of JSON. Before I use it, I want to check to make
> sure that it's valid. My code is below. Does this look alright?
>
> Thanks,
> Eric
>
>
>
>let jsonString = composedString
>
>let jsonData = jsonString.data(using: String.Encoding.utf8)
>
>do {
>
>if (try JSONSerialization.jsonObject(with: jsonData!, options:
> []) as? NSDictionary) != nil {
>
>print("JSON is a dictionary. Valid.")
>
>} else {
>
>print("Not valid JSON data.")
>
>}
>
>} catch let error as NSError {
>
>print("Not valid JSON. \(error.localizedDescription)")
>
>}
>
> ___
>
> 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


Re: Checking if a string is valid JSON

2018-01-29 Thread Saagar Jha
I believe jsonObject(with:options) will throw if the JSON is invalid, so you 
might be able to get away with just the try/catch. Besides, your JSON top level 
object might be an array, in which case I’d expect that casting to an 
NSDictionary would fail.

Saagar Jha

> On Jan 29, 2018, at 12:17, Eric E. Dolecki  wrote:
> 
> I am generating a String of JSON. Before I use it, I want to check to make
> sure that it's valid. My code is below. Does this look alright?
> 
> Thanks,
> Eric
> 
> 
> 
>let jsonString = composedString
> 
>let jsonData = jsonString.data(using: String.Encoding.utf8)
> 
>do {
> 
>if (try JSONSerialization.jsonObject(with: jsonData!, options:
> []) as? NSDictionary) != nil {
> 
>print("JSON is a dictionary. Valid.")
> 
>} else {
> 
>print("Not valid JSON data.")
> 
>}
> 
>} catch let error as NSError {
> 
>print("Not valid JSON. \(error.localizedDescription)")
> 
>}
> ___
> 
> 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


Checking if a string is valid JSON

2018-01-29 Thread Eric E. Dolecki
I am generating a String of JSON. Before I use it, I want to check to make
sure that it's valid. My code is below. Does this look alright?

Thanks,
Eric



let jsonString = composedString

let jsonData = jsonString.data(using: String.Encoding.utf8)

do {

if (try JSONSerialization.jsonObject(with: jsonData!, options:
[]) as? NSDictionary) != nil {

print("JSON is a dictionary. Valid.")

} else {

print("Not valid JSON data.")

}

} catch let error as NSError {

print("Not valid JSON. \(error.localizedDescription)")

}
___

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