Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support python-style '#' comments #839

Open
rh-m2 opened this issue Jan 13, 2023 · 2 comments
Open

Support python-style '#' comments #839

rh-m2 opened this issue Jan 13, 2023 · 2 comments

Comments

@rh-m2
Copy link

rh-m2 commented Jan 13, 2023

PLEASE DON'T FORGET TO "STAR" THIS REPOSITORY :)

When reporting a bug please include the following:

Version of Boost

1.81

Steps necessary to reproduce the problem

auto parse = [](json::string_view s)
{
    return json::parse(
        s,
        json::storage_ptr(),
        json::parse_options { .allow_comments        = true,
                              .allow_trailing_commas = true });
};

parse(R"_json_(
{
    "exchange": "bitmex",
    "from": "2019-10-01",
    "to": "2019-10-02",
    "symbols": ["XBTUSD", "ETHUSD"],
    "withDisconnectMessages": true,
    "dataTypes": ["trade", "book_change", "book_snapshot_10_100ms"],
    # other available data types examples:
    # 'book_snapshot_10_100ms', 'derivative_ticker', 'quote',
    # 'trade_bar_10ms', 'trade_bar_10s'
}
)_json_")

This JSON will be parsed by python's parser, but not by boost.json

All relevant compiler information

Any

Arguments for supporting this

I am writing a back testing and systematic trading engine. The engine is configured with JSON, picked up either off a config file or via a data bus such as RabbitMQ.

Some components of the system are written in C++ and some in Python. It would be useful to be able to add comments to config files that were parsed by both kinds of components.

Workaround:

parse(R"_json_(
{
    "exchange": "bitmex",
    "from": "2019-10-01",
    "to": "2019-10-02",
    "symbols": ["XBTUSD", "ETHUSD"],
    "withDisconnectMessages": true,
    "dataTypes": ["trade", "book_change", "book_snapshot_10_100ms"],
    "_1": "other available data types examples:",
    "_2": 'book_snapshot_10_100ms', 'derivative_ticker', 'quote','trade_bar_10ms', 'trade_bar_10s'"
}
)_json_")

But you know, yuk!

@rh-m2 rh-m2 changed the title Comments incompatible with python Support python-style '#' comments Jan 13, 2023
@vinniefalco
Copy link
Member

In theory this can be done without harming performance when allow_comments=false. Or at least it will be no worse than how we currently support the C and C++ style comments.

@rh-m2
Copy link
Author

rh-m2 commented Jan 16, 2023

Another workaround:

std::string
depython(std::string const &in)
{
    static const auto reg = std::regex("^[ \t]*#.+(#\n|$)");
    return std::regex_replace(in, reg, "");
}

auto val = json::parse(depython(json_text));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants