File: test_pointer_fields.cpp

package info (click to toggle)
reflect-cpp 0.21.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,128 kB
  • sloc: cpp: 50,336; python: 139; makefile: 30; sh: 3
file content (53 lines) | stat: -rw-r--r-- 1,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>
#include <vector>

#include "write_and_read.hpp"

namespace test_pointer_fields {

struct Person {
  rfl::Rename<"firstName", std::string> first_name;
  rfl::Rename<"lastName", std::string> last_name = "Simpson";
  std::vector<Person>* children;
};

TEST(json, test_pointer_fields) {
  const auto bart = Person{.first_name = "Bart", .children = nullptr};

  const auto lisa = Person{.first_name = "Lisa", .children = nullptr};

  const auto maggie = Person{.first_name = "Maggie", .children = nullptr};

  const auto homer =
      Person{.first_name = "Homer",
             .children = new std::vector<Person>({bart, lisa, maggie})};

  const std::string expected =
      R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson","children":null},{"firstName":"Lisa","lastName":"Simpson","children":null},{"firstName":"Maggie","lastName":"Simpson","children":null}]})";

  const auto json_str = rfl::json::write(homer);

  EXPECT_EQ(json_str, expected);

  const auto homer2 =
      rfl::json::read<Person, rfl::AllowRawPtrs>(json_str).value();
  const auto json_str2 = rfl::json::write(homer2);

  EXPECT_EQ(json_str2, expected);

  delete homer.children;
  delete homer2.children;

  // test string_view
  const std::string_view str = "wa oh what a coincidence";
  const auto json_str_view = rfl::json::write(str);
  const auto str_view_back =
      rfl::json::read<std::string_view, rfl::AllowRawPtrs>(json_str_view)
          .value();
  EXPECT_EQ(str, str_view_back);
  delete str_view_back.data();
}
}  // namespace test_pointer_fields