userver: userver/formats/json/serialize_duration.hpp Source File
Loading...
Searching...
No Matches
serialize_duration.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/formats/json/serialize_duration.hpp
4/// @brief Serializers for std::chrono::duration types.
5/// @ingroup userver_universal userver_formats_serialize
6
7#include <chrono>
8
9#include <userver/formats/json/string_builder_fwd.hpp>
10#include <userver/formats/json/value_builder.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace formats::serialize {
15
16// Declare Serialize() as static as multiple translation units might declare
17// different implementations of std::chrono::duration<> serializations.
18template <class Rep, class Period>
19static json::Value Serialize(std::chrono::duration<Rep, Period> duration,
20 To<formats::json::Value>) {
21 return json::ValueBuilder(duration.count()).ExtractValue();
22}
23
24// Declare WriteToStream() as static as multiple translation units might declare
25// different implementations of std::chrono::duration<> serializations.
26template <class Rep, class Period>
27static void WriteToStream(std::chrono::duration<Rep, Period> duration,
28 formats::json::StringBuilder& sw) {
29 WriteToStream(duration.count(), sw);
30}
31
32} // namespace formats::serialize
33
34USERVER_NAMESPACE_END