Skip to content

Commit

Permalink
ResolveURI -> URIResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed Dec 30, 2024
1 parent 1caef39 commit 1074887
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 82 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
to `bignum_format_kind::raw`. Rationale: `bigint_chars_format` was misnamed, as it applied to `bigdec` as well as `bigint` numbers, and
defaulting to `bigint_chars_format::base10` produced surprising results for users of our lossless number option.

- The URI argument passed to the jsonschema ResolveURI function object now included the fragment part of the URI.

- Fixed bugs:

- Git Issue #554: [jsonpath] evaluation throws on json containing json_const_pointer
Expand Down
14 changes: 7 additions & 7 deletions doc/ref/jsonschema/jsonschema.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,11 @@ second schema defined in an external file, `name-defs.json`,
}
```

jsoncons allows you to write a resolve function object to handle the URI
jsoncons allows you to write a resolver function object to handle the URI
of the external file and translate it into a physical pathname.

```cpp
auto resolve = [](const jsoncons::uri& uri) -> json
auto resolver = [](const jsoncons::uri& uri) -> json
{
std::cout << "Requested URI: " << uri.string() << "\n";
std::cout << "base: " << uri.base().string() << ", path: " << uri.path() << "\n\n";
Expand Down Expand Up @@ -435,9 +435,9 @@ int main()
try
{
// Throws schema_error if JSON Schema compilation fails
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema, resolve);
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema, resolver);
auto report = [](const jsonschema::validation_message& msg) -> jsonschema::walk_result
auto reporter = [](const jsonschema::validation_message& msg) -> jsonschema::walk_result
{
std::cout << msg.instance_location().string() << ": " << msg.message() << "\n";
for (const auto& detail : msg.details())
Expand All @@ -447,8 +447,8 @@ int main()
return jsonschema::walk_result::advance;
};
// Will call report function object for each schema violation
compiled.validate(data, report);
// Will call the message handler function object for each schema violation
compiled.validate(data, reporter);
}
catch (const std::exception& e)
{
Expand Down Expand Up @@ -636,7 +636,7 @@ int main()
json data = json::parse("{}");
// will throw schema_error if JSON Schema compilation fails
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema, resolve);
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema);
// will throw a validation_error when a schema violation happens
json patch;
Expand Down
20 changes: 10 additions & 10 deletions doc/ref/jsonschema/make_json_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ template <typename Json>
json_schema<Json> make_json_schema(Json sch, (since 0.175.0)
evaluation_options options = evaluation_options{});

template <typename Json,class ResolveURI>
template <typename Json,class URIResolver>
json_schema<Json> make_json_schema(const Json& sch, (until 0.175.0)
const ResolveURI& resolve,
const URIResolver& resolver,
evaluation_options options = evaluation_options{}); (2)

template <typename Json,class ResolveURI>
template <typename Json,class URIResolver>
json_schema<Json> make_json_schema(Json sch, (since 0.175.0)
const ResolveURI& resolve,
const URIResolver& resolver,
evaluation_options options = evaluation_options{});

template <typename Json>
Expand All @@ -31,16 +31,16 @@ json_schema<Json> make_json_schema(Json sch, (since 0.175
const std::string& retrieval_uri,
evaluation_options options = evaluation_options{});

template <typename Json,class ResolveURI>
template <typename Json,class URIResolver>
json_schema<Json> make_json_schema(const Json& sch, (until 0.175.0)
const std::string& retrieval_uri,
const ResolveURI& resolve,
const URIResolver& resolver,
evaluation_options options = evaluation_options{});
(4)
template <typename Json,class ResolveURI>
template <typename Json,class URIResolver>
json_schema<Json> make_json_schema(Json sch, (since 0.175.0)
const std::string& retrieval_uri,
const ResolveURI& resolve,
const URIResolver& resolver,
evaluation_options options = evaluation_options{});
```
Expand All @@ -54,8 +54,8 @@ Returns a [json_schema<Json>](json_schema.md) that represents a compiled JSON Sc
<td>JSON Schema</td>
</tr>
<tr>
<td>resolve</td>
<td>A function object with the signature of <code>resolve</code> being equivalent to
<td>resolver</td>
<td>A function object with the signature of <code>resolver</code> being equivalent to
<pre>
Json fun(const <a href="../corelib/utility/uri.md">jsoncons::uri</a>& uri);</pre>
If unable to resolve the resource, it should return <code>Json::null()</code>.
Expand Down
10 changes: 5 additions & 5 deletions doc/ref/jsonschema/make_schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ template <typename Json>
std::shared_ptr<json_schema<Json>> make_schema(const Json& schema,
const std::string& retrieval_uri); (2) (since 0.173.0)

template <typename Json,class ResolveURI>
template <typename Json,class URIResolver>
std::shared_ptr<json_schema<Json>> make_schema(const Json& schema,
const std::string& retrieval_uri, const ResolveURI& resolve); (3) (since 0.173.0)
const std::string& retrieval_uri, const URIResolver& resolver); (3) (since 0.173.0)

template <typename Json,class ResolveURI>
template <typename Json,class URIResolver>
std::shared_ptr<json_schema<Json>> make_schema(const Json& schema,
const ResolveURI& resolve); (4)
const URIResolver& resolver); (4)
```
Returns a `shared_ptr` to a `json_schema<Json>`.
Expand All @@ -31,7 +31,7 @@ Returns a `shared_ptr` to a `json_schema<Json>`.
<td>JSON Schema</td>
</tr>
<tr>
<td>resolve</td>
<td>resolver</td>
<td>A function object with the signature of <code>resolver</code> being equivalent to
<pre>
Json fun(const <a href="../corelib/utility/uri.md">jsoncons::uri</a>& uri);</pre></td>
Expand Down
4 changes: 2 additions & 2 deletions examples/src/jsonschema_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void resolve_uri_example()
}
)";

auto resolve = [](const jsoncons::uri& uri) -> json
auto resolver = [](const jsoncons::uri& uri) -> json
{
std::cout << "Requested URI: " << uri.string() << "\n";
std::cout << "base: " << uri.base().string() << ", path: " << uri.path() << "\n\n";
Expand Down Expand Up @@ -151,7 +151,7 @@ void resolve_uri_example()
try
{
// Throws schema_error if JSON Schema compilation fails
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema, resolve);
jsonschema::json_schema<json> compiled = jsonschema::make_json_schema(schema, resolver);

auto report = [](const jsonschema::validation_message& msg) -> jsonschema::walk_result
{
Expand Down
22 changes: 10 additions & 12 deletions include/jsoncons_ext/cbor/cbor_encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,7 @@ class basic_cbor_encoder final : public basic_json_visitor<char>
else
{
bool more = this->begin_array(data.size(), semantic_tag::none,context, ec);
for (auto p = data.begin(); more && p != data.end(); ++p)
for (const auto* p = data.begin(); more && p != data.end(); ++p)
{
more = this->double_value(*p,semantic_tag::none,context, ec);
}
Expand All @@ -1556,19 +1556,17 @@ class basic_cbor_encoder final : public basic_json_visitor<char>
write_byte_string_value(byte_string_view(v));
return true;
}
else

bool more = this->begin_array(data.size(), semantic_tag::none,context, ec);
for (auto p = data.begin(); more && p != data.end(); ++p)
{
bool more = this->begin_array(data.size(), semantic_tag::none,context, ec);
for (auto p = data.begin(); more && p != data.end(); ++p)
{
more = this->double_value(*p,semantic_tag::none,context, ec);
}
if (more)
{
more = this->end_array(context, ec);
}
return more;
more = this->double_value(*p,semantic_tag::none,context, ec);
}
if (more)
{
more = this->end_array(context, ec);
}
return more;
}
/*
bool visit_typed_array(const jsoncons::span<const float128_type>&,
Expand Down
23 changes: 15 additions & 8 deletions include/jsoncons_ext/cbor/cbor_parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct parse_state

parse_state(const parse_state&) = default;
parse_state(parse_state&&) = default;
parse_state& operator=(const parse_state&) = default;
parse_state& operator=(parse_state&&) = default;

~parse_state() = default;
};
Expand Down Expand Up @@ -89,9 +91,11 @@ class basic_cbor_parser : public ser_context
}

mapped_string(const mapped_string&) = default;

mapped_string(mapped_string&&) noexcept = default;

mapped_string& operator=(const mapped_string&) = default;
mapped_string& operator=(mapped_string&&) noexcept = default;

mapped_string(const mapped_string& other, const allocator_type& alloc)
: type(other.type), str(other.str,alloc), bytes(other.bytes,alloc)
{
Expand All @@ -102,9 +106,7 @@ class basic_cbor_parser : public ser_context
{
}

mapped_string& operator=(const mapped_string&) = default;

mapped_string& operator=(mapped_string&&) = default;
~mapped_string() = default;
};

using mapped_string_allocator_type = typename std::allocator_traits<allocator_type>:: template rebind_alloc<mapped_string>;
Expand All @@ -122,8 +124,8 @@ class basic_cbor_parser : public ser_context
Source source_;
cbor_decode_options options_;

bool more_;
bool done_;
bool more_{true};
bool done_{false};
string_type text_buffer_;
byte_string_type bytes_buffer_;
uint64_t item_tag_;
Expand Down Expand Up @@ -177,8 +179,6 @@ class basic_cbor_parser : public ser_context
: alloc_(alloc),
source_(std::forward<Sourceable>(source)),
options_(options),
more_(true),
done_(false),
text_buffer_(alloc),
bytes_buffer_(alloc),
item_tag_(0),
Expand All @@ -190,6 +190,13 @@ class basic_cbor_parser : public ser_context
{
state_stack_.emplace_back(parse_mode::root,0);
}

basic_cbor_parser(const basic_cbor_parser&) = delete;
basic_cbor_parser(basic_cbor_parser&&) = delete;
basic_cbor_parser& operator=(const basic_cbor_parser&) = delete;
basic_cbor_parser& operator=(basic_cbor_parser&&) = delete;

~basic_cbor_parser() = default;

void restart()
{
Expand Down
28 changes: 14 additions & 14 deletions include/jsoncons_ext/jsonschema/common/schema_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("maxLength");
if (!sch.is_number())
{
std::string message("maxLength must be a number value");
const std::string message("maxLength must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
auto value = sch.template as<std::size_t>();
Expand All @@ -366,7 +366,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("minLength");
if (!sch.is_number())
{
std::string message("minLength must be an integer value");
const std::string message("minLength must be an integer value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
auto value = sch.template as<std::size_t>();
Expand Down Expand Up @@ -409,7 +409,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("maximum");
if (!sch.is_number())
{
std::string message("maximum must be a number value");
const std::string message("maximum must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
return jsoncons::make_unique<maximum_validator<Json>>(parent, schema_location, sch);
Expand All @@ -421,7 +421,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("exclusiveMaximum");
if (!sch.is_number())
{
std::string message("exclusiveMaximum must be a number value");
const std::string message("exclusiveMaximum must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
return jsoncons::make_unique<exclusive_maximum_validator<Json>>(parent, schema_location, sch);
Expand All @@ -434,7 +434,7 @@ namespace jsonschema {

if (!sch.is_number())
{
std::string message("minimum must be an integer");
const std::string message("minimum must be an integer");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
return jsoncons::make_unique<minimum_validator<Json>>(parent, schema_location, sch);
Expand All @@ -446,7 +446,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("exclusiveMinimum");
if (!sch.is_number())
{
std::string message("exclusiveMinimum must be a number value");
const std::string message("exclusiveMinimum must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
return jsoncons::make_unique<exclusive_minimum_validator<Json>>(parent, schema_location, sch);
Expand All @@ -458,7 +458,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("multipleOf");
if (!sch.is_number())
{
std::string message("multipleOf must be a number value");
const std::string message("multipleOf must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
auto value = sch.template as<double>();
Expand Down Expand Up @@ -559,7 +559,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("contentEncoding");
if (!sch.is_string())
{
std::string message("contentEncoding must be a string");
const std::string message("contentEncoding must be a string");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
auto value = sch.template as<std::string>();
Expand All @@ -572,7 +572,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("contentMediaType");
if (!sch.is_string())
{
std::string message("contentMediaType must be a string");
const std::string message("contentMediaType must be a string");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}

Expand All @@ -582,7 +582,7 @@ namespace jsonschema {
{
if (!it->value().is_string())
{
std::string message("contentEncoding must be a string");
const std::string message("contentEncoding must be a string");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}

Expand Down Expand Up @@ -671,7 +671,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("maxItems");
if (!sch.is_number())
{
std::string message("maxItems must be a number value");
const std::string message("maxItems must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
auto value = sch.template as<std::size_t>();
Expand All @@ -684,7 +684,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("minItems");
if (!sch.is_number())
{
std::string message("minItems must be a number value");
const std::string message("minItems must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
auto value = sch.template as<std::size_t>();
Expand All @@ -697,7 +697,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("maxProperties");
if (!sch.is_number())
{
std::string message("maxProperties must be a number value");
const std::string message("maxProperties must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
auto value = sch.template as<std::size_t>();
Expand All @@ -710,7 +710,7 @@ namespace jsonschema {
uri schema_location = context.make_schema_location("minProperties");
if (!sch.is_number())
{
std::string message("minProperties must be a number value");
const std::string message("minProperties must be a number value");
JSONCONS_THROW(schema_error(schema_location.string() + ": " + message));
}
auto value = sch.template as<std::size_t>();
Expand Down
4 changes: 2 additions & 2 deletions include/jsoncons_ext/jsonschema/draft4/schema_builder_4.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ namespace draft4 {
uri schema_location = context.make_schema_location("maximum");
if (!sch.is_number())
{
std::string message("maximum must be a number value");
const std::string message("maximum must be a number value");
JSONCONS_THROW(schema_error(message));
}

Expand Down Expand Up @@ -334,7 +334,7 @@ namespace draft4 {

if (!sch.is_number())
{
std::string message("minimum must be an integer");
const std::string message("minimum must be an integer");
JSONCONS_THROW(schema_error(message));
}

Expand Down
Loading

0 comments on commit 1074887

Please sign in to comment.