$darkmode
output_serializer_json.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 Robert Bosch GmbH
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  * SPDX-License-Identifier: Apache-2.0
17  */
22 #pragma once
23 
24 #include <string> // for string
25 
26 #include <cloe/core.hpp> // for Json
27 #include <cloe/utility/output_serializer.hpp> // for Outputstream, JSONSerializer, ...
28 
29 namespace cloe {
30 namespace utility {
31 
32 enum class JsonFileType {
33  JSON_GZIP,
34  JSON_ZIP,
35  JSON,
36 };
37 
38 // clang-format off
39 ENUM_SERIALIZATION(JsonFileType, ({
40  {cloe::utility::JsonFileType::JSON_GZIP , "json.gz" },
41  {cloe::utility::JsonFileType::JSON_ZIP , "json.zip" },
42  {cloe::utility::JsonFileType::JSON , "json" },
43 }))
44 // clang-format on
45 
46 class AbstractJsonSerializerBase {
47  protected:
48  static const std::string json_array_open;
49  static const std::string json_array_close;
50 };
51 
52 template <typename... TSerializerArgs>
53 class AbstractJsonSerializer : public Serializer<TSerializerArgs...>,
54  public AbstractJsonSerializerBase {
55  public:
56  using base = Serializer<TSerializerArgs...>;
57  using Serializer<TSerializerArgs...>::Serializer;
58  virtual ~AbstractJsonSerializer() = default;
59  std::string make_default_filename(const std::string& default_filename) override {
60  return default_filename + ".json";
61  }
62  void start_array() override { base::write(json_array_open); }
63  void end_array() override { base::write(json_array_close); }
64 };
65 
66 class SimpleJsonSerializer : public AbstractJsonSerializer<const Json&, bool> {
67  public:
69  using json_base::json_base;
70  void serialize(const Json& j, bool write_delim) override {
71  if (write_delim) {
72  write(",\n"); // serialize delimiting comma, if already one dataset was serialized
73  }
74  auto txt = j.dump(2); // serialize with level 2 indent
75  write(txt);
76  }
77 };
78 
79 // JsonFileSerializer is
80 // 1) Interface for the consumer class
81 // 2) the anchor point for exactly one instance of the default_filename
83  public:
84  virtual ~JsonFileSerializer() = default;
85 
86  [[nodiscard]]
87  virtual bool open_file(const std::string& filename) = 0;
88 
89  virtual void serialize(const Json& j) = 0;
90  virtual void close_file() = 0;
91 
92  protected:
93  static const std::string default_filename;
94  bool prepend_delimiter{false};
95 };
96 
97 // JsonFileSerializerImpl is the implementation of JsonFileSerializer
98 template <typename TOutputStream>
100  : public SequentialFileSerializer<SimpleJsonSerializer, TOutputStream, const Json&, bool>,
101  public JsonFileSerializer {
102  using file_base =
104 
105  public:
106  JsonFileSerializerImpl(Logger logger) : file_base(logger), JsonFileSerializer() {}
107  virtual ~JsonFileSerializerImpl() = default;
108  using file_base::open_file;
109 
110  [[nodiscard]]
111  bool open_file(const std::string& filename) override {
112  std::string default_name = this->outputstream_.make_default_filename(
113  this->serializer_.make_default_filename(default_filename));
114  return file_base::open_file(filename, default_name);
115  }
116 
117  using file_base::serialize;
118  void serialize(const Json& j) override {
119  file_base::serialize(j, prepend_delimiter);
120  prepend_delimiter = true;
121  }
122  void close_file() override { file_base::close_file(); }
123 
124  protected:
125  void on_file_opened() override { this->serializer_.start_array(); }
126  void on_file_closing() override { this->serializer_.end_array(); }
127 };
128 
132 
133 std::unique_ptr<JsonFileSerializer> make_json_file_serializer(JsonFileType type, Logger log);
134 
135 } // namespace utility
136 } // namespace cloe
Definition: output_serializer_json.hpp:54
Definition: output_serializer_json.hpp:101
Definition: output_serializer_json.hpp:82
SequentialFileSerializer is a FileSerializer for sequences of objects of the same type.
Definition: output_serializer.hpp:199
Definition: output_serializer.hpp:60
Definition: output_serializer_json.hpp:66
#define ENUM_SERIALIZATION(xType, xMap)
Definition: enum.hpp:51