3

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.

Sam
  • 3,453
  • 1
  • 33
  • 57

3 Answers3

6

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 :)

ilya n.
  • 18,398
  • 15
  • 71
  • 89
  • yes, people kept removing my C tag and I'm not allowed to imply you suggest a C library (or my question will be removed. – Sam Dec 13 '13 at 13:55
  • Looks good, although not the neatest, as I'll have to munge my NSDictionary back to a string then to a C-string, but it does looks like a very mature library, so it should hopefully be worth it. Thanks – Sam Dec 13 '13 at 13:56
  • 1
    @Sam, it's better to use just `c` tag, then. – ilya n. Dec 13 '13 at 13:56
  • didn't want to open 3 questions ;) – Sam Dec 13 '13 at 14:00
  • Update: seems to be working! thanks. I think I may start work on a pure obj-c version at some point... – Sam Dec 13 '13 at 15:50
  • just finished and released v0.1 - it currently passes the JSON schema test suite. hope it's useful. – Sam Mar 06 '14 at 14:37
4

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

Sam
  • 3,453
  • 1
  • 33
  • 57
1

For anyone who is looking for an MIT licensed JSON schema validator, "JsonSchemaValidator" does a nice job and is available here: https://bitbucket.org/nut_code_monkey/jsonschemavalidator

jondring
  • 43
  • 7
  • this is good, but doesn't look like it can parse a schema file, as defined by the JSON-Schema spec. It's a nice way of approaching the problem though, as it has typed and validated description language. – Sam Jan 29 '14 at 16:05