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

Error triggered but not surfaced when setting allow_missing_members or allow_unassigned_required_members #20

Closed yahooguntu closed 2 years ago

yahooguntu commented 2 years ago

I believe I found a bug when setting allow_missing_members or allow_unassigned_required_members. In the example below, parseTo() does indeed return an error (in one of my tests, a JS::Error::MissingPropertyMember), but when makeErrorString() is called it returns "Error NoError:".

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

struct MyStruct {
  int a;
  int b;

  JS_OBJ(a, b);
};

int main() {
  std::string json = "{\"a\":1, \"c\": 3}";

  MyStruct my_struct;

  JS::ParseContext parse_ctx(json);

  parse_ctx.allow_missing_members = false;
  parse_ctx.allow_unnasigned_required_members = false;

  if (parse_ctx.parseTo(my_struct) != JS::Error::NoError) {
    std::cout << "Error parsing struct: " << parse_ctx.makeErrorString() << "\n";
  }

}

Output: Error parsing struct: Error NoError:

Expected: either a MissingPropertyMember or UnassignedRequiredMember error string, depending on the circumstances.

jorgen commented 2 years ago

Thank you for your bugreport. I will fix this tonight.

jorgen commented 2 years ago

Hello! Thank you again for your bugreport! This should now be fixed. https://godbolt.org/z/9rhTsrMa5 Please note that the JSON and C++ members leading to errors are accessible in the ParseContext through the members:

  std::vector<std::string> missing_members;
  std::vector<std::string> unassigned_required_members;

and that they are populated even though the allow_* members are true.

Please don't hesitate to file other bugs/requests.

yahooguntu commented 2 years ago

Thanks for the quick fix, and thanks for publishing such a great library!