Open Lighting Architecture  Latest Git
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
JsonTypes.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * JsonTypes.h
17  * Enums for the Json types.
18  * See http://www.json.org/
19  * Copyright (C) 2014 Simon Newton
20  */
21 
30 #ifndef INCLUDE_OLA_WEB_JSONTYPES_H_
31 #define INCLUDE_OLA_WEB_JSONTYPES_H_
32 
33 #include <stdint.h>
34 #include <string>
35 
36 namespace ola {
37 namespace web {
38 
44 enum JsonType {
53 };
54 
60 std::string JsonTypeToString(JsonType type);
61 
68 JsonType StringToJsonType(const std::string &type);
69 
73 template <typename T>
74 JsonType TypeFromValue(const T&);
75 
79 template <typename T>
80 std::string GetTypename(const T&t) {
81  return TypeToString(TypeFromValue(t));
82 }
83 
84 template <>
85 inline JsonType TypeFromValue<std::string>(const std::string&) {
86  return JSON_STRING;
87 }
88 
89 template <>
90 inline JsonType TypeFromValue<bool>(const bool&) { return JSON_BOOLEAN; }
91 
92 template <>
93 inline JsonType TypeFromValue<uint32_t>(const uint32_t&) {
94  return JSON_INTEGER;
95 }
96 
97 template <>
98 inline JsonType TypeFromValue<int32_t>(const int32_t&) { return JSON_INTEGER; }
99 
100 template <>
101 inline JsonType TypeFromValue<uint64_t>(const uint64_t&) {
102  return JSON_INTEGER;
103 }
104 
105 template <>
106 inline JsonType TypeFromValue<int64_t>(const int64_t&) { return JSON_INTEGER; }
107 
108 template <>
109 inline JsonType TypeFromValue<double>(const double&) { return JSON_NUMBER; }
110 
111 template <typename T>
112 inline JsonType TypeFromValue(const T&) { return JSON_UNDEFINED; }
113 
114 } // namespace web
115 } // namespace ola
116 #endif // INCLUDE_OLA_WEB_JSONTYPES_H_
std::string GetTypename(const T &t)
Given a value, return the string name this value corresponds to.
Definition: JsonTypes.h:80
Definition: JsonTypes.h:50
Definition: JsonTypes.h:51
JsonType StringToJsonType(const string &type)
Convert a string to a JsonType.
Definition: JsonTypes.cpp:52
JsonType
The type of JSON data element.
Definition: JsonTypes.h:44
Definition: JsonTypes.h:47
Definition: JsonTypes.h:45
Definition: JsonTypes.h:52
JsonType TypeFromValue(const T &)
Given a value, return the JsonType this value corresponds to.
Definition: JsonTypes.h:112
string JsonTypeToString(JsonType type)
Get the string corresponding to a JsonType.
Definition: JsonTypes.cpp:29
Definition: JsonTypes.h:48
Definition: JsonTypes.h:46
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: JsonTypes.h:49