libkazv
Loading...
Searching...
No Matches
jsonwrap.hpp
Go to the documentation of this file.
1/*
2 * This file is part of libkazv.
3 * SPDX-FileCopyrightText: 2020-2021 Tusooa Zhu <tusooa@kazv.moe>
4 * SPDX-License-Identifier: AGPL-3.0-or-later
5 */
6
7
8#pragma once
9#include "libkazv-config.hpp"
10
11#include <boost/serialization/version.hpp>
12#include <boost/serialization/string.hpp>
13#include <boost/serialization/split_member.hpp>
14
15#include <nlohmann/json.hpp>
16#include <immer/box.hpp>
17
18namespace Kazv
19{
21
23 {
24 // Cannot directly use box here, because it causes the resulting json
25 // to be wrapped into an array.
26 // https://github.com/arximboldi/immer/issues/155
27 struct Private
28 {
29 json j;
30 friend bool operator==(const Private &a, const Private &b) = default;
31 friend bool operator!=(const Private &a, const Private &b) = default;
32 };
33
34 immer::box<Private> m_d;
35 public:
36 JsonWrap() : m_d(Private{json()}) {}
37 JsonWrap(json&& j) : m_d(Private{std::move(j)}) {}
38 JsonWrap(const json& j) : m_d(Private{j}) {}
39
40 const json &get() const { return m_d.get().j; }
41 operator json() const { return m_d.get().j; }
42
43 template <class Archive>
44 void save(Archive &ar, std::uint32_t const /*version*/) const {
45 ar << get().dump();
46 }
47
48 template <class Archive>
49 void load(Archive &ar, std::uint32_t const /*version*/) {
50 std::string j;
51 ar >> j;
52 m_d = immer::box<Private>(Private{json::parse(std::move(j))});
53 }
54 BOOST_SERIALIZATION_SPLIT_MEMBER()
55 friend bool operator==(const JsonWrap &a, const JsonWrap &b) = default;
56 friend bool operator!=(const JsonWrap &a, const JsonWrap &b) = default;
57 };
58}
59
60BOOST_CLASS_VERSION(Kazv::JsonWrap, 0)
61
62namespace nlohmann
63{
64 template <>
65 struct adl_serializer<Kazv::JsonWrap> {
66 static void to_json(json& j, Kazv::JsonWrap w) {
67 j = w.get();
68 }
69
70 static void from_json(const json& j, Kazv::JsonWrap &w) {
71 w = Kazv::JsonWrap(j);
72 }
73 };
74}
Definition jsonwrap.hpp:23
const json & get() const
Definition jsonwrap.hpp:40
JsonWrap()
Definition jsonwrap.hpp:36
void load(Archive &ar, std::uint32_t const)
Definition jsonwrap.hpp:49
JsonWrap(const json &j)
Definition jsonwrap.hpp:38
JsonWrap(json &&j)
Definition jsonwrap.hpp:37
void save(Archive &ar, std::uint32_t const) const
Definition jsonwrap.hpp:44
Definition location.hpp:10
@ Private
Definition client-model.hpp:41
nlohmann::json json
Definition jsonwrap.hpp:20
bool operator==(const BaseJob &a, const BaseJob &b)
Definition basejob.cpp:282
bool operator!=(const BaseJob &a, const BaseJob &b)
Definition basejob.cpp:290
Definition location.hpp:27
Definition clientutil.hpp:140
static void from_json(const json &j, Kazv::JsonWrap &w)
Definition jsonwrap.hpp:70
static void to_json(json &j, Kazv::JsonWrap w)
Definition jsonwrap.hpp:66