blob: 6d4064ffc76f962ec4c6a05cca93b5463fc1e4db [file] [log] [blame]
Austin Schuh2dd86a92022-09-14 21:19:23 -07001#include "json_test.h"
2
3#include "flatbuffers/flatbuffers.h"
4#include "flatbuffers/idl.h"
5#include "monster_test_generated.h"
6#include "monster_test_bfbs_generated.h"
7#include "optional_scalars_generated.h"
8#include "test_assert.h"
9
10namespace flatbuffers {
11namespace tests {
12
13using namespace MyGame::Example;
14
15// Check stringify of an default enum value to json
16void JsonDefaultTest(const std::string& tests_data_path) {
17 // load FlatBuffer schema (.fbs) from disk
18 std::string schemafile;
19 TEST_EQ(flatbuffers::LoadFile((tests_data_path + "monster_test.fbs").c_str(),
20 false, &schemafile),
21 true);
22 // parse schema first, so we can use it to parse the data after
23 flatbuffers::Parser parser;
24 auto include_test_path =
25 flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
26 const char *include_directories[] = { tests_data_path.c_str(),
27 include_test_path.c_str(), nullptr };
28
29 TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
30 // create incomplete monster and store to json
31 parser.opts.output_default_scalars_in_json = true;
32 parser.opts.output_enum_identifiers = true;
33 flatbuffers::FlatBufferBuilder builder;
34 auto name = builder.CreateString("default_enum");
35 MonsterBuilder color_monster(builder);
36 color_monster.add_name(name);
37 FinishMonsterBuffer(builder, color_monster.Finish());
38 std::string jsongen;
39 auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen);
40 TEST_EQ(result, true);
41 // default value of the "color" field is Blue
42 TEST_EQ(std::string::npos != jsongen.find("color: \"Blue\""), true);
43 // default value of the "testf" field is 3.14159
44 TEST_EQ(std::string::npos != jsongen.find("testf: 3.14159"), true);
45}
46
47void JsonEnumsTest(const std::string& tests_data_path) {
48 // load FlatBuffer schema (.fbs) from disk
49 std::string schemafile;
50 TEST_EQ(flatbuffers::LoadFile((tests_data_path + "monster_test.fbs").c_str(),
51 false, &schemafile),
52 true);
53 // parse schema first, so we can use it to parse the data after
54 flatbuffers::Parser parser;
55 auto include_test_path =
56 flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
57 const char *include_directories[] = { tests_data_path.c_str(),
58 include_test_path.c_str(), nullptr };
59 parser.opts.output_enum_identifiers = true;
60 TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
61 flatbuffers::FlatBufferBuilder builder;
62 auto name = builder.CreateString("bitflag_enum");
63 MonsterBuilder color_monster(builder);
64 color_monster.add_name(name);
65 color_monster.add_color(Color(Color_Blue | Color_Red));
66 FinishMonsterBuffer(builder, color_monster.Finish());
67 std::string jsongen;
68 auto result = GenerateText(parser, builder.GetBufferPointer(), &jsongen);
69 TEST_EQ(result, true);
70 TEST_EQ(std::string::npos != jsongen.find("color: \"Red Blue\""), true);
71 // Test forward compatibility with 'output_enum_identifiers = true'.
72 // Current Color doesn't have '(1u << 2)' field, let's add it.
73 builder.Clear();
74 std::string future_json;
75 auto future_name = builder.CreateString("future bitflag_enum");
76 MonsterBuilder future_color(builder);
77 future_color.add_name(future_name);
78 future_color.add_color(
79 static_cast<Color>((1u << 2) | Color_Blue | Color_Red));
80 FinishMonsterBuffer(builder, future_color.Finish());
81 result = GenerateText(parser, builder.GetBufferPointer(), &future_json);
82 TEST_EQ(result, true);
83 TEST_EQ(std::string::npos != future_json.find("color: 13"), true);
84}
85
86void JsonOptionalTest(const std::string& tests_data_path, bool default_scalars) {
87 // load FlatBuffer schema (.fbs) and JSON from disk
88 std::string schemafile;
89 std::string jsonfile;
90 TEST_EQ(
91 flatbuffers::LoadFile((tests_data_path + "optional_scalars.fbs").c_str(),
92 false, &schemafile),
93 true);
94 TEST_EQ(flatbuffers::LoadFile((tests_data_path + "optional_scalars" +
95 (default_scalars ? "_defaults" : "") + ".json")
96 .c_str(),
97 false, &jsonfile),
98 true);
99
100 auto include_test_path =
101 flatbuffers::ConCatPathFileName(tests_data_path, "include_test");
102 const char *include_directories[] = { tests_data_path.c_str(),
103 include_test_path.c_str(), nullptr };
104
105 // parse schema first, so we can use it to parse the data after
106 flatbuffers::Parser parser;
107 parser.opts.output_default_scalars_in_json = default_scalars;
108 TEST_EQ(parser.Parse(schemafile.c_str(), include_directories), true);
109 TEST_EQ(parser.ParseJson(jsonfile.c_str()), true);
110
111 // here, parser.builder_ contains a binary buffer that is the parsed data.
112
113 // First, verify it, just in case:
114 flatbuffers::Verifier verifier(parser.builder_.GetBufferPointer(),
115 parser.builder_.GetSize());
116 TEST_EQ(optional_scalars::VerifyScalarStuffBuffer(verifier), true);
117
118 // to ensure it is correct, we now generate text back from the binary,
119 // and compare the two:
120 std::string jsongen;
121 auto result =
122 GenerateText(parser, parser.builder_.GetBufferPointer(), &jsongen);
123 TEST_EQ(result, true);
124 TEST_EQ_STR(jsongen.c_str(), jsonfile.c_str());
125}
126
127void ParseIncorrectMonsterJsonTest(const std::string& tests_data_path) {
128 std::string schemafile;
129 TEST_EQ(flatbuffers::LoadFile((tests_data_path + "monster_test.bfbs").c_str(),
130 true, &schemafile),
131 true);
132 flatbuffers::Parser parser;
133 flatbuffers::Verifier verifier(
134 reinterpret_cast<const uint8_t *>(schemafile.c_str()), schemafile.size());
135 TEST_EQ(reflection::VerifySchemaBuffer(verifier), true);
136 TEST_EQ(
137 parser.Deserialize(reinterpret_cast<const uint8_t *>(schemafile.c_str()),
138 schemafile.size()),
139 true);
140 TEST_EQ(parser.ParseJson("{name:\"monster\"}"), true);
141 TEST_EQ(parser.ParseJson(""), false);
142 TEST_EQ(parser.ParseJson("{name: 1}"), false);
143 TEST_EQ(parser.ParseJson("{name:+1}"), false);
144 TEST_EQ(parser.ParseJson("{name:-1}"), false);
145 TEST_EQ(parser.ParseJson("{name:-f}"), false);
146 TEST_EQ(parser.ParseJson("{name:+f}"), false);
147}
148
149void JsonUnsortedArrayTest() {
150 flatbuffers::Parser parser;
151 TEST_EQ(parser.Deserialize(MyGame::Example::MonsterBinarySchema::data(),
152 MyGame::Example::MonsterBinarySchema::size()),
153 true);
154 auto jsonStr = R"(
155 {
156 "name": "lookupTest",
157 "testarrayoftables": [
158 { "name": "aaa" },
159 { "name": "ccc" },
160 { "name": "bbb" }
161 ]
162 }
163 )";
164 TEST_EQ(parser.ParseJson(jsonStr), true);
165 auto monster = flatbuffers::GetRoot<MyGame::Example::Monster>(
166 parser.builder_.GetBufferPointer());
167
168 TEST_NOTNULL(monster->testarrayoftables()->LookupByKey("aaa"));
169 TEST_NOTNULL(monster->testarrayoftables()->LookupByKey("bbb"));
170 TEST_NOTNULL(monster->testarrayoftables()->LookupByKey("ccc"));
171}
172
173} // namespace tests
174} // namespace flatbuffers