Open Lighting Architecture: include/ola/web/JsonTypes.h Source File

| Open Lighting Architecture 0.10.9

Loading...
Searching...
No Matches
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
29
30#ifndef INCLUDE_OLA_WEB_JSONTYPES_H_
31#define INCLUDE_OLA_WEB_JSONTYPES_H_
32
33#include <stdint.h>
34#include <string>
35
36namespace ola {
37namespace web {
38
44 enum JsonType {
45 JSON_ARRAY,
46 JSON_BOOLEAN,
47 JSON_INTEGER,
48 JSON_NULL,
49 JSON_NUMBER,
50 JSON_OBJECT,
51 JSON_STRING,
52 JSON_UNDEFINED,
53};
54
60std::string JsonTypeToString(JsonType type);
61
68 JsonType StringToJsonType(const std::string &type);
69
73template <typename T>
74 JsonType TypeFromValue(const T&);
75
79template <typename T>
80std::string GetTypename(const T&t) {
81 return TypeToString(TypeFromValue(t));
82}
83
84template <>
85inline JsonType TypeFromValue<std::string>(const std::string&) {
86 return JSON_STRING;
87}
88
89template <>
90inline JsonType TypeFromValue<bool>(const bool&) { return JSON_BOOLEAN; }
91
92template <>
93inline JsonType TypeFromValue<uint32_t>(const uint32_t&) {
94 return JSON_INTEGER;
95}
96
97template <>
98inline JsonType TypeFromValue<int32_t>(const int32_t&) { return JSON_INTEGER; }
99
100template <>
101inline JsonType TypeFromValue<uint64_t>(const uint64_t&) {
102 return JSON_INTEGER;
103}
104
105template <>
106inline JsonType TypeFromValue<int64_t>(const int64_t&) { return JSON_INTEGER; }
107
108template <>
109inline JsonType TypeFromValue<double>(const double&) { return JSON_NUMBER; }
110
111template <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_
ola::web
Classes to deal with web services.
Definition Json.cpp:29
ola::web::JsonType
JsonType
The type of JSON data element.
Definition JsonTypes.h:44
ola::web::JSON_NUMBER
@ JSON_NUMBER
Definition JsonTypes.h:49
ola::web::JSON_STRING
@ JSON_STRING
Definition JsonTypes.h:51
ola::web::JSON_OBJECT
@ JSON_OBJECT
Definition JsonTypes.h:50
ola::web::JSON_BOOLEAN
@ JSON_BOOLEAN
Definition JsonTypes.h:46
ola::web::JSON_NULL
@ JSON_NULL
Definition JsonTypes.h:48
ola::web::JSON_INTEGER
@ JSON_INTEGER
Definition JsonTypes.h:47
ola::web::JSON_ARRAY
@ JSON_ARRAY
Definition JsonTypes.h:45
ola::web::JSON_UNDEFINED
@ JSON_UNDEFINED
Definition JsonTypes.h:52
ola::web::JsonTypeToString
string JsonTypeToString(JsonType type)
Get the string corresponding to a JsonType.
Definition JsonTypes.cpp:29
ola::web::TypeFromValue
JsonType TypeFromValue(const T &)
Given a value, return the JsonType this value corresponds to.
Definition JsonTypes.h:112
ola::web::StringToJsonType
JsonType StringToJsonType(const string &type)
Convert a string to a JsonType.
Definition JsonTypes.cpp:52
ola::web::GetTypename
std::string GetTypename(const T &t)
Given a value, return the string name this value corresponds to.
Definition JsonTypes.h:80
ola
The namespace containing all OLA symbols.
Definition Credentials.cpp:44