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 > .

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

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

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

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 {

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,

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