jorgen / json_struct

json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa
Other
422 stars 57 forks source link

Cannot parse std::tuple<bool> #42

Closed guycook closed 1 year ago

guycook commented 1 year ago

On MSVC, I'm unable parse fields like "member": [true] to a type std::tuple<bool> member. A minimal example is below

#include <iostream>
#include "json_struct.h"

struct TestInt {
  std::tuple<int32_t> member;
  JS_OBJ(member);
};

struct TestBool {
  std::tuple<bool> member;
  JS_OBJ(member);
};

int main(int argc, char** argv) {
  TestInt tiStruct;
  JS::ParseContext intContext(R"({ "member": [5] })");
  if (intContext.parseTo(tiStruct) == JS::Error::NoError) {
    std::cout << "Success, member is: " << std::get<0>(tiStruct.member) << std::endl;
  }
  else {
    std::cout << intContext.makeErrorString();
  }

  TestBool tbStruct;
  JS::ParseContext boolContext(R"({ "member": [true] })");
  if (boolContext.parseTo(tbStruct) == JS::Error::NoError) {
    std::cout << "Success, member is: " << std::get<0>(tbStruct.member) << std::endl;
  }
  else {
    std::cout << boolContext.makeErrorString();
  }
}

Output:

Success, member is: 5
Error ExpectedDelimiter:
{ "member": [true] }
                  ^

I'm not sure what the cause here is, all I can say from current investigation is that single item tuples of other basic types (number/string) still work, and tuples involving bools + other types work, e.g. std::tuple<bool, int> will parse fine if given [true, 2]

jorgen commented 1 year ago

Hi and thank you for your interest in json_struct. There was an error in a lookup table, making the end of token be the ] and not the e. Its fixed in the latest commit.

guycook commented 1 year ago

Thanks for the quick resolution, can confirm this fixes the problem in my project as well as the test case. json_struct has been great so far, cheers!