Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2e2615

Browse files
authored
[Serialization] JSON Serialization (#223)
1 parent 5df6311 commit c2e2615

27 files changed

+252
-196
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@
2222
[submodule "optional"]
2323
path = third_party/tl_optional
2424
url = https://github.com/TartanLlama/optional.git
25+
[submodule "json"]
26+
path = third_party/json
27+
url = https://github.com/nlohmann/json.git

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ include(cmake/tool/doxygen.cmake)
7373

7474
# Libraries
7575
include(cmake/target/flatbuffers.cmake)
76+
include(cmake/target/json.cmake)
77+
7678
include(cmake/target/fmt.cmake)
7779
include(cmake/target/spdlog.cmake)
7880
include(cmake/target/cli11.cmake)

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Build C, C++ and ASM files in C++
2828
- `C++11 thread` library support
2929
- Third Party Libraries (See License below)
3030
- Flatbuffers v2.0.0
31+
- Nlohmann::Json v3.11.2
3132
- Taskflow v3.1.0
3233
- CLI11 v2.1.0
3334
- Tiny Process Library v2.0.4
@@ -176,6 +177,7 @@ _BuildCC_ is licensed under the Apache License, Version 2.0. See [LICENSE](LICEN
176177
- [Tiny Process Library](https://gitlab.com/eidheim/tiny-process-library) (Process handling) [MIT License]
177178
- [Taskflow](https://github.com/taskflow/taskflow) (Parallel Programming) [MIT License] [Header Only]
178179
- See also [3rd-Party](https://github.com/taskflow/taskflow/tree/master/3rd-party) used by Taskflow
180+
- [Nlohmann::Json](https://github.com/nlohmann/json) (JSON Serialization) [MIT License] [Header Only]
179181
- [Flatbuffers](https://github.com/google/flatbuffers) (Serialization) [Apache-2.0 License]
180182
- [CLI11](https://github.com/CLIUtils/CLI11) (Argument Parsing) [BSD-3-Clause License] [Header Only]
181183
- [CppUTest](https://github.com/cpputest/cpputest) (Unit Testing/Mocking) [BSD-3-Clause License]

bootstrap/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_executable(buildcc_lib_bootstrap
33
)
44
target_sources(buildcc_lib_bootstrap PRIVATE
55
src/build_flatbuffers.cpp
6+
src/build_nlohmann_json.cpp
67
src/build_cli11.cpp
78
src/build_fmtlib.cpp
89
src/build_spdlog.cpp

bootstrap/include/bootstrap/build_buildcc.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "build_cli11.h"
2323
#include "build_flatbuffers.h"
2424
#include "build_fmtlib.h"
25+
#include "build_nlohmann_json.h"
2526
#include "build_spdlog.h"
2627
#include "build_taskflow.h"
2728
#include "build_tl_optional.h"
@@ -32,7 +33,8 @@ namespace buildcc {
3233
void schema_gen_cb(FileGenerator &generator, const BaseTarget &flatc_exe);
3334

3435
void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
35-
const TargetInfo &flatbuffers_ho, const TargetInfo &fmt_ho,
36+
const TargetInfo &flatbuffers_ho,
37+
const TargetInfo &nlohmann_json_ho, const TargetInfo &fmt_ho,
3638
const TargetInfo &spdlog_ho, const TargetInfo &cli11_ho,
3739
const TargetInfo &taskflow_ho, const TargetInfo &tl_optional_ho,
3840
const BaseTarget &tpl);
@@ -45,6 +47,7 @@ class BuildBuildCC {
4547
public:
4648
// TargetInfo / Header Only
4749
static constexpr const char *const kFlatbuffersHoName = "flatbuffers_ho";
50+
static constexpr const char *const kNlohmannJsonHoName = "nlohmann_json_ho";
4851
static constexpr const char *const kCli11HoName = "cli11_ho";
4952
static constexpr const char *const kFmtHoName = "fmtlib_ho";
5053
static constexpr const char *const kSpdlogHoName = "spdlog_ho";
@@ -89,6 +92,9 @@ class BuildBuildCC {
8992
TargetInfo &GetFlatbuffersHo() {
9093
return storage_.Ref<TargetInfo>(kFlatbuffersHoName);
9194
}
95+
TargetInfo &GetNlohmannJsonHo() {
96+
return storage_.Ref<TargetInfo>(kNlohmannJsonHoName);
97+
}
9298
TargetInfo &GetCli11Ho() { return storage_.Ref<TargetInfo>(kCli11HoName); }
9399
TargetInfo &GetFmtHo() { return storage_.Ref<TargetInfo>(kFmtHoName); }
94100
TargetInfo &GetSpdlogHo() { return storage_.Ref<TargetInfo>(kSpdlogHoName); }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2021-2022 Niket Naidu. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef BOOTSTRAP_BUILD_NLOHMANN_JSON_H_
18+
#define BOOTSTRAP_BUILD_NLOHMANN_JSON_H_
19+
20+
#include "buildcc.h"
21+
22+
namespace buildcc {
23+
24+
void nlohmann_json_ho_cb(TargetInfo &info);
25+
26+
} // namespace buildcc
27+
28+
#endif

bootstrap/src/build_buildcc.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,12 @@ namespace buildcc {
2020

2121
void schema_gen_cb(FileGenerator &generator, const BaseTarget &flatc_exe) {
2222
generator.AddPattern("path_fbs", "{current_root_dir}/path.fbs");
23-
generator.AddPattern("custom_generator_fbs",
24-
"{current_root_dir}/custom_generator.fbs");
2523
generator.AddPattern("target_fbs", "{current_root_dir}/target.fbs");
2624

2725
generator.AddInput("{path_fbs}");
28-
generator.AddInput("{custom_generator_fbs}");
2926
generator.AddInput("{target_fbs}");
3027

3128
generator.AddOutput("{current_build_dir}/path_generated.h");
32-
generator.AddOutput("{current_build_dir}/custom_generator_generated.h");
3329
generator.AddOutput("{current_build_dir}/target_generated.h");
3430

3531
generator.AddPatterns({
@@ -38,13 +34,14 @@ void schema_gen_cb(FileGenerator &generator, const BaseTarget &flatc_exe) {
3834
// generator.AddCommand("{flatc_compiler} --help");
3935
generator.AddCommand("{flatc_compiler} -o {current_build_dir} -I "
4036
"{current_root_dir} --gen-object-api "
41-
"--cpp {path_fbs} {custom_generator_fbs} {target_fbs}");
37+
"--cpp {path_fbs} {target_fbs}");
4238

4339
generator.Build();
4440
}
4541

4642
void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
47-
const TargetInfo &flatbuffers_ho, const TargetInfo &fmt_ho,
43+
const TargetInfo &flatbuffers_ho,
44+
const TargetInfo &nlohmann_json_ho, const TargetInfo &fmt_ho,
4845
const TargetInfo &spdlog_ho, const TargetInfo &cli11_ho,
4946
const TargetInfo &taskflow_ho, const TargetInfo &tl_optional_ho,
5047
const BaseTarget &tpl) {
@@ -119,6 +116,9 @@ void buildcc_cb(BaseTarget &target, const FileGenerator &schema_gen,
119116
// FLATBUFFERS HO
120117
target.Insert(flatbuffers_ho, kInsertOptions);
121118

119+
// NLOHMANN JSON HO
120+
target.Insert(nlohmann_json_ho, kInsertOptions);
121+
122122
// FMT HO
123123
target.Insert(fmt_ho, kInsertOptions);
124124

@@ -217,6 +217,12 @@ void BuildBuildCC::Initialize() {
217217
TargetEnv(env_.GetTargetRootDir() / "third_party" / "flatbuffers",
218218
env_.GetTargetBuildDir()));
219219

220+
// Nlohmann json HO lib
221+
(void)storage_.Add<TargetInfo>(
222+
kNlohmannJsonHoName, toolchain_,
223+
TargetEnv(env_.GetTargetRootDir() / "third_party" / "json",
224+
env_.GetTargetBuildDir()));
225+
220226
// CLI11 HO lib
221227
(void)storage_.Add<TargetInfo>(
222228
kCli11HoName, toolchain_,
@@ -268,6 +274,7 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
268274
auto &flatc_exe = GetFlatc();
269275
auto &schema_gen = GetSchemaGen();
270276
auto &flatbuffers_ho_lib = GetFlatbuffersHo();
277+
auto &nlohmann_json_ho_lib = GetNlohmannJsonHo();
271278
auto &cli11_ho_lib = GetCli11Ho();
272279
auto &fmt_ho_lib = GetFmtHo();
273280
auto &spdlog_ho_lib = GetSpdlogHo();
@@ -281,6 +288,7 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
281288
.Build(schema_gen_cb, schema_gen, flatc_exe)
282289
.Dep(schema_gen, flatc_exe)
283290
.Func(flatbuffers_ho_cb, flatbuffers_ho_lib)
291+
.Func(nlohmann_json_ho_cb, nlohmann_json_ho_lib)
284292
.Func(cli11_ho_cb, cli11_ho_lib)
285293
.Func(fmt_ho_cb, fmt_ho_lib)
286294
.Func(spdlog_ho_cb, spdlog_ho_lib)
@@ -290,8 +298,8 @@ void BuildBuildCC::Setup(const ArgToolchainState &state) {
290298
.Build(tpl_cb, tpl_lib)
291299
.Func(global_flags_cb, buildcc_lib, toolchain_)
292300
.Build(buildcc_cb, buildcc_lib, schema_gen, flatbuffers_ho_lib,
293-
fmt_ho_lib, spdlog_ho_lib, cli11_ho_lib, taskflow_ho_lib,
294-
tl_optional_ho_lib, tpl_lib)
301+
nlohmann_json_ho_lib, fmt_ho_lib, spdlog_ho_lib, cli11_ho_lib,
302+
taskflow_ho_lib, tl_optional_ho_lib, tpl_lib)
295303
.Dep(buildcc_lib, schema_gen)
296304
.Dep(buildcc_lib, tpl_lib);
297305
}

bootstrap/src/build_nlohmann_json.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2021-2022 Niket Naidu. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "bootstrap/build_nlohmann_json.h"
18+
19+
namespace buildcc {
20+
21+
void nlohmann_json_ho_cb(TargetInfo &info) {
22+
info.AddIncludeDir("include");
23+
info.GlobHeaders("include/nlohmann");
24+
info.GlobHeaders("include/nlohmann/thirdparty/hedley");
25+
info.GlobHeaders("include/nlohmann/detail");
26+
info.GlobHeaders("include/nlohmann/detail/conversions");
27+
info.GlobHeaders("include/nlohmann/detail/input");
28+
info.GlobHeaders("include/nlohmann/detail/iterators");
29+
info.GlobHeaders("include/nlohmann/detail/meta");
30+
info.GlobHeaders("include/nlohmann/detail/meta/call_std");
31+
info.GlobHeaders("include/nlohmann/detail/output");
32+
}
33+
34+
} // namespace buildcc

buildcc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ if(${BUILDCC_BUILD_AS_SINGLE_LIB})
1414
fmt::fmt
1515
tl::optional
1616
flatbuffers
17+
nlohmann_json::nlohmann_json
1718
Taskflow
1819
CLI11::CLI11
1920
)

buildcc/lib/target/include/target/custom_generator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class CustomBlobHandler {
8686
virtual std::vector<uint8_t> Serialize() const = 0;
8787
};
8888

89-
struct UserGenInfo : internal::GenInfo {
89+
struct UserGenInfo : internal::CustomGeneratorSchema::IdInfo {
9090
fs_unordered_set inputs;
9191
GenerateCb generate_cb;
9292
std::shared_ptr<CustomBlobHandler> blob_handler{nullptr};
@@ -99,7 +99,7 @@ struct UserCustomGeneratorSchema : public internal::CustomGeneratorSchema {
9999
for (auto &[id, gen_info] : gen_info_map) {
100100
gen_info.internal_inputs = path_schema_convert(
101101
gen_info.inputs, internal::Path::CreateExistingPath);
102-
auto [_, success] = internal_gen_info_map.try_emplace(id, gen_info);
102+
auto [_, success] = internal_ids.try_emplace(id, gen_info);
103103
env::assert_fatal(success, fmt::format("Could not save {}", id));
104104
}
105105
}

0 commit comments

Comments
 (0)