CCF
Loading...
Searching...
No Matches
json_serialiser.h
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the Apache 2.0 License.
3#pragma once
4
6
7#include <nlohmann/json.hpp>
8
10{
11 // Converts values to and from JSON, using nlohmann JSON. To add support for
12 // custom types, make them convertible to nlohmann::json. You may do this
13 // manually with to_json/from_json functions, or use the DECLARE_JSON...
14 // macros from include/ccf/ds/json.h to generate converters for POD structs.
15 template <typename T>
17 {
18 static SerialisedEntry to_serialised(const T& t)
19 {
20 static_assert(
21 std::is_convertible_v<T, nlohmann::json>,
22 "Cannot convert this type to JSON - either define to_json or use "
23 "DECLARE_JSON... macros");
24
25 const nlohmann::json j = t;
26 const auto dumped = j.dump();
27 return SerialisedEntry(dumped.begin(), dumped.end());
28 }
29
30 static T from_serialised(const SerialisedEntry& rep)
31 {
32 const auto j = nlohmann::json::parse(rep.begin(), rep.end());
33 return j.get<T>();
34 }
35 };
36}
Definition sha256_hash.h:80
ccf::ByteVector SerialisedEntry
Definition serialised_entry.h:8
Definition json_serialiser.h:17
static T from_serialised(const SerialisedEntry &rep)
Definition json_serialiser.h:30
static SerialisedEntry to_serialised(const T &t)
Definition json_serialiser.h:18