test-json-serialization.cpp 1021 B

12345678910111213141516171819202122232425262728
  1. #include "tests.h"
  2. void test_json_serialization(testing &t) {
  3. auto original = build_peg_parser([](common_peg_parser_builder & p) {
  4. return "<tool_call>" + p.json() + "</tool_call>";
  5. });
  6. auto json_serialized = original.to_json().dump();
  7. t.test("compare before/after", [&](testing &t) {
  8. auto deserialized = common_peg_arena::from_json(nlohmann::json::parse(json_serialized));
  9. // Test complex JSON
  10. std::string input = R"({"name": "test", "values": [1, 2, 3], "nested": {"a": true}})";
  11. common_peg_parse_context ctx1(input);
  12. common_peg_parse_context ctx2(input);
  13. auto result1 = original.parse(ctx1);
  14. auto result2 = deserialized.parse(ctx2);
  15. t.assert_equal("both_succeed", result1.success(), result2.success());
  16. t.assert_equal("same_end_pos", result1.end, result2.end);
  17. });
  18. t.bench("deserialize", [&]() {
  19. auto deserialized = common_peg_arena::from_json(nlohmann::json::parse(json_serialized));
  20. }, 100);
  21. }