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, To<formats::json::Value>) {
20 return json::ValueBuilder(duration.count()).ExtractValue();
21}
22
23// Declare WriteToStream() as static as multiple translation units might declare
24// different implementations of std::chrono::duration<> serializations.
25template <class Rep, class Period>
26static void WriteToStream(std::chrono::duration<Rep, Period> duration, formats::json::StringBuilder& sw) {
27 WriteToStream(duration.count(), sw);
28}
29
30} // namespace formats::serialize
31
32USERVER_NAMESPACE_END