Triton Man Triton Man - 8 months ago 75
iOS Question

How do I get NSJSONSerialization to output a boolean as true or false?

I'm using NSJSONSerialization dataWithJSONObject to serialize my classes to JSON. When it serializes a BOOL, it gives it the value 1 or 0 in the JSON string. I need this to be true or false instead. Is this possible to do generically?


No, the Foundation Object for a Bool is NSNumber numberWithBool, which becomes either 0 or 1. We have no Bool Object. Same go for reading JSON. True/false will become a NSNumber again.

You could create an Bool Class and build your own parser. Arrays are Arrays and JSON Objects are NSDictionary. You can query the keys, test what Class lies behind and build the JSON String from this.