leadpony / justify

Justify is a JSON validator based on JSON Schema Specification and Jakarta JSON Processing API (JSON-P).
Apache License 2.0
96 stars 18 forks source link

Read JsonObject directly #14

Open atomictag opened 5 years ago

atomictag commented 5 years ago

Hi there, thanks for this really well-thought library.

In my use case I already have a concrete JsonObject which I want to validate against a schema and apply defaults (withDefaultValues(true)), returning a new validated JsonObject with defaults applied. How can this be achieved with justify? JsonReaderFactory::createReader expects an InputStream or a Reader, but not a JsonObject and I had little luck with JsonParserFactory::createParser which accepts a JsonObject input but I don't seem to manage to collect the stream into another JsonObject. Any help would be greatly appreciated!

leadpony commented 5 years ago

Hello @atomictag Thank you for using this small library. Unfortunately the current version of this library does not support direct validation of JsonArray or JsonObject in memory. The feature itself is not difficult to implement, because javax.json.stream.JsonParserFactory in JSON-P API 1.1 defines methods createParser(JsonArray) and createParser(JsonObject). But there exists a small problem that neither of the Reference Implementation and Apache Johnzon provide full-fledged parser through these factory methods. In order to realize the feature, I will try implementing complete parser which parses in-memory JsonArray and JsonObject. Please wait and see the outcome.

atomictag commented 5 years ago

Thank you so much. I am about to try out https://github.com/pgelinas/jackson-javax-json to see if that can help (need to switch to Jackson at some point anyway). Thanks again!

leadpony commented 5 years ago

Thank you @atomictag, I didn't know about the parser up until now. To tell the truth, I am currently working on another implementation of JSON-P API from scratch, which provides more robust and faster parser than other implementations. The repo JSON-P Test Suite is built for that purpose.

atomictag commented 5 years ago

Using a recent version of johnzon-core solved the problem for me

leadpony commented 5 years ago

You are right. I confirmed that Johnzon works for it. Thank you for letting me know. One thing to care: getString() method of in-memory parser in Johnzon has a bug that it wrongly throws an exception when the current value is a number, as my test suite reveals.

leadpony commented 5 years ago

Hello @atomictag The latest release 0.17.0 resolved this issue. I believe both of createParser(JsonArray) and createParser(JsonObject) in JsonParserFactory created by this library, work well with the Reference Implementation besides Apache Johnzon. However I am not fully satisfied the performance of the current implementation. This library is a streaming style validator and where processing of parsing plus validation is needed, the library is one of the fastest of all validator implementations for JSON Schema. On the other hand, streaming-style validators are not so good at validating the once-loaded JSON value, because they must produce parser events to be processed from the JSON value before validating it. The performance issue will be mitigated by coming releases in near future.