Better diagnostic for value_to errors on described structures

Open
#1,069 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
cpp
Domain
backend

Research direction

Start at boost/json/detail/value_to.hpp around the reported line and reproduce the described C++ example with the missing currency member. Trace how value_to reports the unknown name and determine how the nested JSON location can be retained. Done means the exception diagnostic includes the path to the missing member, with coverage for the described-structure conversion.

Written by the indexing model from the issue text.

Description

Given the following use case that comes from the documentation:

#include <iostream>
#include <string>

#include <boost/json/src.hpp>
#include <boost/describe.hpp>
#include <boost/exception/diagnostic_information.hpp>

struct money
{
    std::string currency;
    double value;
};

BOOST_DESCRIBE_STRUCT(money, (), (currency, value))

struct test
{
    double pi;
    bool happy;
    money object;
};

BOOST_DESCRIBE_STRUCT(test, (), (pi, happy, object))

int main()
{
    boost::json::value jv = {
        { "pi", 3.141 },
        { "happy", true },
        {"object", {
            //{ "currency", "USD" },
            { "value", 42.99 }
        }}
        };

    try {
        test res = boost::json::value_to<test>(jv);
    }
    catch (std::exception const& ex) {
        std::cerr << boost::diagnostic_information(ex, true) << std::endl;
    }

    return 0;
}

The missing currency member gives the following exception:

Dynamic exception type: boost::detail::with_throw_location<boost::system::system_error>
std::exception::what: unknown name [boost.json:39 at /nobackup/lid00lima21/debionne/miniconda3/envs/lima2/include/boost/json/detail/value_to.hpp:371 in function 'operator()']

It would be interesting to have an information where the exception occurred in the json value during value_to conversion. Ideally something like errinfo_json_path="object.concurreny".

Dominant language
C++
Stars
478
Forks
110
Avg merge
8d 12h
Merged PRs (30d)
6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from boostorg/json

All issues in boostorg/json

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.