Leosac  0.8.0
Open Source Access Control
JSONUtils.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "core/auth/AuthFwd.hpp"
24 #include "tools/Uuid.hpp"
25 #include <boost/lexical_cast.hpp>
26 #include <chrono>
27 #include <nlohmann/json.hpp>
28 #include <type_traits>
29 
30 
38 namespace nlohmann
39 {
40 
41 template <>
42 struct adl_serializer<Leosac::UUID>
43 {
44  static void to_json(json &j, const Leosac::UUID &uuid)
45  {
46  if (uuid.is_nil())
47  {
48  j = nullptr;
49  }
50  else
51  {
52  j = uuid.to_string();
53  }
54  }
55 
60  static void from_json(const json &j, Leosac::UUID &uuid)
61  {
62  if (j.is_null())
63  {
64  uuid = Leosac::UUID::null_uuid();
65  }
66  else
67  {
68  if (j.is_string())
69  {
70  std::string str = j.get<std::string>();
71  uuid = Leosac::UUID(boost::lexical_cast<boost::uuids::uuid>(str));
72  }
73  else if (j.is_number_unsigned() && j.get<uint64_t>() == 0)
74  {
75  uuid = Leosac::UUID::null_uuid();
76  }
77  else
78  {
79  throw LEOSACException("Failed to unserialize UUID");
80  }
81  }
82  }
83 };
84 }
85 
86 namespace Leosac
87 {
88 
89 using json = nlohmann::json;
90 
96 namespace JSONUtil
97 {
104 template <typename T>
105 typename std::enable_if<!std::is_same<const char *, T>::value &&
106  !std::is_same<std::chrono::system_clock::time_point,
107  std::remove_reference_t<T>>::value &&
108  !std::is_enum<T>::value,
109  T>::type
110 extract_with_default(const nlohmann::json &obj, const std::string &key,
111  T default_value)
112 {
113  T ret = default_value;
114  try
115  {
116  if (!obj.at(key).is_null())
117  ret = obj.at(key).get<T>();
118  }
119  catch (const json::out_of_range &e)
120  {
121  }
122  return ret;
123 }
124 
125 template <typename T>
126 typename std::enable_if<std::is_enum<T>::value, T>::type
127 extract_with_default(const nlohmann::json &obj, const std::string &key,
128  T default_value)
129 {
130  T ret = default_value;
131  try
132  {
133  if (!obj.at(key).is_null())
134  {
135  // Extract using enum's underlying type, then case back.
136  ret = static_cast<T>(obj.at(key).get<std::underlying_type_t<T>>());
137  }
138  }
139  catch (const json::out_of_range &e)
140  {
141  }
142  return ret;
143 }
144 
145 template <typename T>
146 typename std::enable_if<std::is_same<const char *, T>::value, std::string>::type
147 extract_with_default(const nlohmann::json &obj, const std::string &key,
148  T default_value)
149 {
150  return extract_with_default<std::string>(obj, key, default_value);
151 }
152 
157 std::chrono::system_clock::time_point
158 extract_with_default(const nlohmann::json &obj, const std::string &key,
159  const std::chrono::system_clock::time_point &tp);
160 
170  const std::string &base_key,
171  const Auth::ValidityInfo &def);
172 }
173 }
Uuid.hpp
Leosac::UUID::null_uuid
static UUID null_uuid()
Returns a null UUID with a full zero value.
Definition: Uuid.hpp:70
Leosac::JSONUtil::extract_with_default
std::chrono::system_clock::time_point extract_with_default(const nlohmann::json &obj, const std::string &key, const std::chrono::system_clock::time_point &tp)
Extract an ISO 8601 datetime string from a json object.
Definition: JSONUtils.cpp:45
json
nlohmann::json json
Definition: WSServer.cpp:76
AuthFwd.hpp
nlohmann
Below we add a serializer into the nlohmann namespace to serialize the Leosac::UUID ype.
Definition: JSONUtils.hpp:38
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
leosacexception.hpp
Exception class for LEOSAC Project related errors.
nlohmann::adl_serializer< Leosac::UUID >::to_json
static void to_json(json &j, const Leosac::UUID &uuid)
Definition: JSONUtils.hpp:44
LEOSACException
A base class for Leosac specific exception.
Definition: leosacexception.hpp:40
Leosac::UUID::is_nil
bool is_nil() const
Definition: Uuid.hpp:57
Leosac::UUID::to_string
std::string to_string() const
Definition: Uuid.hpp:62
Leosac::JSONUtil::extract_validity_with_default
Auth::ValidityInfo extract_validity_with_default(const nlohmann::json &obj, const std::string &base_key, const Auth::ValidityInfo &def)
Extract fields representing a ValidityInfo object.
Definition: JSONUtils.cpp:29
nlohmann::adl_serializer< Leosac::UUID >::from_json
static void from_json(const json &j, Leosac::UUID &uuid)
For unserialization we expect either a string representing the UUID, or a number.
Definition: JSONUtils.hpp:60
Leosac::Auth::ValidityInfo
A simple class that stores (and can be queried for) the validity of some objects.
Definition: ValidityInfo.hpp:42
Leosac::UUID
Thin wrapper around boost::uuids::uuid.
Definition: Uuid.hpp:35