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