Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate JSON Schema in Objective-C?

I have JSON returned from a server, and I would like to validate it against a JSON Schema (probably draft V3, can change though).

I thought that perhaps NSDictionary would have the functionality, but it doesn't seem to, here is my attempt:

[self.dictionary ]

(where dictionary is an NSDictionary)

There weren't any methods for validating the NSDictionary against a JSON schema that I could find. How do I do this?

Please note that you can write C and C++ in objective-c, hence these tags being present in the question.

like image 224
Sam Avatar asked Feb 02 '26 07:02

Sam


2 Answers

Objective-C is a superset of C. JSON Schema site lists a C library wjelement, you can adapt it.

The example function call is

WJESchemaValidate(schema, json, schema_error, schema_load, NULL, format)

You'll need to supply a C string, rather than NSString, naturally.

Note: after I wrote the answer, I realised you were fine with pure C from the start :)

like image 200
ilya n. Avatar answered Feb 04 '26 22:02

ilya n.


After not finding a satisfactory answer to this question - I went away and developed my own schema validator in native Objective-c here: https://github.com/samskiter/KiteJSONValidator

I've just got it passing all the tests in the JSON-Schema test suite so I've released v0.1.

It's released under the MIT license so I hope it's useful to someone...

like image 33
Sam Avatar answered Feb 04 '26 22:02

Sam