Skip to content

How to read json file with optional entries and entries with different types #1281

@dubnde

Description

@dubnde

First of all thansks for this fantastic library. It got me reading dozens of json test files withing minutes.

How do I read from a json file with test vectors in which have optional entries as well as entries that can change the type of the value?

I am wondering how to go about writing the to_json/from_json methods. I particularly I am interested in the from_json to start with. Some of the entries as shown in the example below are optional. For example, the decoded entry as well as the diagnostic entry may or may not appear for each object. Additionally the decoded entry could have values of different types i.e integer, double or string . So I just need to read them out of the file in the best way possible.

    auto j = R"([
                  { 
                    "cbor" : "GBk=", 
                    "hex" : "1819", 
                    "roundtrip" : true, 
                    "decoded" : 25 
                  },
                  { 
                    "cbor" : "+T4A", 
                    "hex" : "f93e00", 
                    "roundtrip" : true, 
                    "decoded" : 1.5 
                  },
                  { "cbor" : "YsO8", 
                    "hex" : "62c3bc", 
                    "roundtrip" : true, 
                    "decoded" : "ü" 
                  },
                  { "cbor" : "+X4A", 
                    "hex" : "f97e00", 
                    "roundtrip" : true, 
                    "diagnostic" : "NaN" 
                  },
                  {
                    "cbor" : "nwGCAgOfBAX//w==",
                    "hex" : "9f018202039f0405ffff",
                    "roundtrip" : false,
                    "decoded" : [ 1, [ 2, 3 ], [ 4, 5 ] ]
                  },
                  {
                    "cbor" : "gmFhv2FiYWP/",
                    "hex" : "826161bf61626163ff",
                    "roundtrip" : false,
                    "decoded" : [ "a", { "b" : "c" } ]
                  }
                ])"_json;

So I have figured out how to do this for the first three entries(cbor, hex, roundtrip) that are always present and don't morph into different types. Could I please have some advice how to handle the other two optional and mutating entries i.e. decoded and diagnostic?

  struct json_test_entry
  {
    std::string cbor;
    std::string hex;
    bool        roundtrip;
    // @TODO Need to add optional/variant "decode/diagnostic" here
  };

  void to_json(nlohmann::json &j, const json_test_entry &p)
  {
    j = json{
      { "cbor", p.cbor }, { "hex", p.hex }, { "roundtrip", p.roundtrip },
      // @TODO Need to add optional/variant "decode/diagnostic" here

    };
  }

  void from_json(const nlohmann::json &j, json_test_entry &p)
  {
    p.cbor      = j.at("cbor").get< std::string >();
    p.hex       = j.at("hex").get< std::string >();
    p.roundtrip = j.at("roundtrip").get< bool >();
    // @TODO Need to decode optional/variant "decode/diagnostic" here
  }

thanks very much as I am sure there is a way of doing this but cannot just see it for now.

the complete file with test vectors is locate here

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions