Skip to content

Commit

Permalink
Example update
Browse files Browse the repository at this point in the history
  • Loading branch information
danielaparker committed May 18, 2020
1 parent f9af44a commit fdeed03
Show file tree
Hide file tree
Showing 19 changed files with 365 additions and 270 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -400,21 +400,20 @@ int main()
name = ev.get<std::string>();
return false;
}
else if (name == "rated")
if (name == "rated")
{
name.clear();
return true;
}
else
{
return false;
}
return false;
};

json_cursor cursor(data, filter);
for (; !cursor.done(); cursor.next())
json_cursor cursor(data);

auto filtered_c = cursor | filter;
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = cursor.current();
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::string_value:
Expand Down
59 changes: 25 additions & 34 deletions doc/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,54 +562,45 @@ end_array
#include <jsoncons/json_cursor.hpp>
#include <fstream>

// A stream filter to filter out all events except name
// and restrict name to "author"
// Filter out all events except names of authors

struct author_filter
int main()
{
bool accept_next_ = false;
std::ifstream is("book_catalog.json");

json_cursor cursor(is);

bool operator()(const staj_event& event, const ser_context&)
bool author_next = false;
auto filtered_c = cursor |
[&](const staj_event& event, const ser_context&) -> bool
{
if (event.event_type() == staj_event_type::key &&
if (event.event_type() == staj_event_type::key &&
event.get<jsoncons::string_view>() == "author")
{
accept_next_ = true;
author_next = true;
return false;
}
else if (accept_next_)
if (author_next)
{
accept_next_ = false;
author_next = false;
return true;
}
else
return false;
};

for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = filtered_c.current();
switch (event.event_type())
{
accept_next_ = false;
return false;
case staj_event_type::string_value:
std::cout << event.get<jsoncons::string_view>() << "\n";
break;
default:
std::cout << "Unhandled event type: " << event.event_type() << " " << "\n";
break;
}
}
};

int main()
{
std::ifstream is("book_catalog.json");

author_filter filter;
json_cursor cursor(is, filter);

for (; !cursor.done(); cursor.next())
{
const auto& event = cursor.current();
switch (event.event_type())
{
case staj_event_type::string_value:
std::cout << event.get<jsoncons::string_view>() << "\n";
break;
default:
std::cout << "Unhandled event type: " << event.event_type() << " " << "\n";
break;
}
}
}
```
Output:
Expand Down
81 changes: 20 additions & 61 deletions doc/ref/basic_json_cursor.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,55 +40,22 @@ wjson_cursor |`basic_json_cursor<wchar_t>`
const Allocator& alloc = Allocator()); // (1)
template <class Source>
basic_json_cursor(Source&& source,
std::function<bool(const basic_staj_event<CharT>&, const ser_context&)> filter,
const basic_json_decode_options<CharT>& options = basic_json_decode_options<CharT>(),
std::function<bool(json_errc,const ser_context&)> err_handler = default_json_parsing(),
const Allocator& alloc = Allocator()); // (2)
template <class Source>
basic_json_cursor(Source&& source, std::error_code& ec); // (3)
basic_json_cursor(Source&& source, std::error_code& ec); // (2)
template <class Source>
basic_json_cursor(Source&& source,
const basic_json_decode_options<CharT>& options,
std::error_code& ec) // (4)
std::error_code& ec) // (3)
template <class Source>
basic_json_cursor(Source&& source,
const basic_json_decode_options<CharT>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
std::error_code& ec) // (5)
template <class Source>
basic_json_cursor(Source&& source,
std::function<bool(const basic_staj_event<CharT>&, const ser_context&)> filter,
std::error_code& ec); // (6)
template <class Source>
basic_json_cursor(Source&& source,
std::function<bool(const basic_staj_event<CharT>&, const ser_context&)> filter,
const basic_json_decode_options<CharT>& options,
std::error_code& ec) // (7)
template <class Source>
basic_json_cursor(Source&& source,
std::function<bool(const basic_staj_event<CharT>&, const ser_context&)> filter,
const basic_json_decode_options<CharT>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
std::error_code& ec) // (8)
template <class Source>
basic_json_cursor(std::allocator_arg_t, const Allocator& alloc,
Source&& source,
std::function<bool(const basic_staj_event<CharT>&, const ser_context&)> filter,
const basic_json_decode_options<CharT>& options,
std::function<bool(json_errc,const ser_context&)> err_handler,
std::error_code& ec); // (9)
std::error_code& ec) // (4)
Constructors (1)-(2) read from a character sequence or stream and throw a
Constructor (1) reads from a character sequence or stream and throws a
[ser_error](ser_error.md) if a parsing error is encountered while processing the initial event.
Constructors (3)-(9) read from a character sequence or stream and set `ec`
Constructors (2)-(4) read from a character sequence or stream and set `ec`
if a parsing error is encountered while processing the initial event.
Note: It is the programmer's responsibility to ensure that `basic_json_cursor` does not outlive the source,
Expand Down Expand Up @@ -259,41 +226,33 @@ end_array

using namespace jsoncons;

struct author_filter
int main()
{
bool accept_next_ = false;
std::ifstream is("book_catalog.json");

bool operator()(const staj_event& event, const ser_context&)
json_cursor cursor(is);

bool author_next = false;
auto filtered_c = cursor |
[&](const staj_event& event, const ser_context&) -> bool
{
if (event.event_type() == staj_event_type::key &&
if (event.event_type() == staj_event_type::key &&
event.get<jsoncons::string_view>() == "author")
{
accept_next_ = true;
author_next = true;
return false;
}
else if (accept_next_)
if (author_next)
{
accept_next_ = false;
author_next = false;
return true;
}
else
{
accept_next_ = false;
return false;
}
}
};
return false;
};

int main()
{
std::ifstream is("book_catalog.json");

author_filter filter;
json_cursor cursor(is, filter);

for (; !cursor.done(); cursor.next())
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = cursor.current();
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::string_value:
Expand Down
15 changes: 6 additions & 9 deletions doc/ref/bson/basic_bson_cursor.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,12 @@ struct author_filter
accept_next_ = true;
return false;
}
else if (accept_next_)
if (accept_next_)
{
accept_next_ = false;
return true;
}
else
{
accept_next_ = false;
return false;
}
return false;
}
};

Expand All @@ -257,11 +253,12 @@ int main()
std::ifstream is("book_catalog.json");

author_filter filter;
bson_cursor cursor(is, filter);
bson_cursor cursor(is);

for (; !cursor.done(); cursor.next())
auto filtered_c = cursor | filter;
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = cursor.current();
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::string_value:
Expand Down
7 changes: 4 additions & 3 deletions doc/ref/cbor/basic_cbor_cursor.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,12 @@ int main()
std::ifstream is("book_catalog.json");

author_filter filter;
cbor_cursor cursor(is, filter);
cbor_cursor cursor(is);

for (; !cursor.done(); cursor.next())
auto filtered_c = cursor | filter;
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = cursor.current();
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::string_value:
Expand Down
8 changes: 5 additions & 3 deletions doc/ref/cbor/cbor.md
Original file line number Diff line number Diff line change
Expand Up @@ -341,10 +341,12 @@ int main()
return (ev.tag() == semantic_tag::bigdec) || (ev.tag() == semantic_tag::bigfloat);
};

cbor::cbor_bytes_cursor cursor(data, filter);
for (; !cursor.done(); cursor.next())
cbor::cbor_bytes_cursor cursor(data);

auto filtered_c = cursor | filter;
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = cursor.current();
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::string_value:
Expand Down
8 changes: 5 additions & 3 deletions doc/ref/ubjson/ubjson.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,12 @@ int main()
return (ev.event_type() == staj_event_type::double_value) && (ev.get<double>() < 30.0);
};

ubjson::ubjson_bytes_cursor cursor(data, filter);
for (; !cursor.done(); cursor.next())
ubjson::ubjson_bytes_cursor cursor(data);

auto filtered_c = cursor | filter;
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = cursor.current();
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::double_value:
Expand Down
8 changes: 5 additions & 3 deletions examples/src/cbor_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,12 @@ namespace {
return (ev.tag() == semantic_tag::bigdec) || (ev.tag() == semantic_tag::bigfloat);
};

cbor::cbor_bytes_cursor cursor(data, filter);
for (; !cursor.done(); cursor.next())
cbor::cbor_bytes_cursor cursor(data);

auto filtered_c = cursor | filter;
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = cursor.current();
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::string_value:
Expand Down
36 changes: 14 additions & 22 deletions examples/src/json_cursor_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,40 +106,32 @@ namespace {
}
}

struct author_filter
// Filtering the stream
void filtering_a_json_stream()
{
bool accept_next_ = false;
json_cursor cursor(example);

bool operator()(const staj_event& event, const ser_context&)
bool author_next = false;
auto filtered_c = cursor |
[&](const staj_event& event, const ser_context&) -> bool
{
if (event.event_type() == staj_event_type::key &&
if (event.event_type() == staj_event_type::key &&
event.get<jsoncons::string_view>() == "author")
{
accept_next_ = true;
author_next = true;
return false;
}
else if (accept_next_)
if (author_next)
{
accept_next_ = false;
author_next = false;
return true;
}
else
{
accept_next_ = false;
return false;
}
}
};
return false;
};

// Filtering the stream
void filtering_a_json_stream()
{
author_filter filter;
json_cursor cursor(example, filter);

for (; !cursor.done(); cursor.next())
for (; !filtered_c.done(); filtered_c.next())
{
const auto& event = cursor.current();
const auto& event = filtered_c.current();
switch (event.event_type())
{
case staj_event_type::string_value:
Expand Down
Loading

0 comments on commit fdeed03

Please sign in to comment.