Adobe Source Libraries 2.0.0
A collection of C++ libraries.
Loading...
Searching...
No Matches
json_helper.hpp
Go to the documentation of this file.
1/*
2 Copyright 2013 Adobe
3 Distributed under the Boost Software License, Version 1.0.
4 (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5*/
6/**************************************************************************************************/
7
8#ifndef ADOBE_JSON_HELPER_HPP
9#define ADOBE_JSON_HELPER_HPP
10
11#include <adobe/any_regular.hpp>
12#include <adobe/array.hpp>
13#include <adobe/dictionary.hpp>
14#include <adobe/exception.hpp>
15#include <adobe/json.hpp>
17
18/**************************************************************************************************/
19
20namespace adobe {
21
22/**************************************************************************************************/
31 /*
32 We use std::string as key type instead of adobe::name_t because of the
33 append, move, etc., capabilities of the former type. When we go to
34 insert a key/value pair into the dictionary we convert the key to a name_t
35 first. This is a better route to take than building out name_t to behave
36 more like a string type, which it is not intended to be.
37 */
39 typedef std::string string_type;
44
45 static json_type type(const value_type& x) {
46 const std::type_info& type(x.type_info());
47
48 if (type == typeid(object_type))
49 return json_type::object;
50 else if (type == typeid(array_type))
51 return json_type::array;
52 else if (type == typeid(string_type))
53 return json_type::string;
54 else if (type == typeid(double))
55 return json_type::number;
56 else if (type == typeid(bool))
57 return json_type::boolean;
58 else if (type == typeid(adobe::empty_t))
59 return json_type::null;
60
61 terminate(ADOBE_MESSAGE("invalid type for serialization"));
62 }
63
64 template <typename T>
65 static const T& as(const value_type& x) {
66 return x.cast<T>();
67 }
68
69 static void move_append(object_type& obj, key_type& key, value_type& value) {
70 obj[adobe::name_t(key.c_str())] = std::move(value);
71 key.clear();
72 }
73 static void append(string_type& str, const char* f, const char* l) { str.append(f, l); }
74 static void move_append(array_type& array, value_type& value) {
75 array.emplace_back(std::move(value));
76 }
77};
78
79/**************************************************************************************************/
92inline adobe::any_regular_t json_parse(const char* data) {
94}
95
116template <typename O>
117inline O json_generate(const adobe::any_regular_t& x, O out) {
119}
120
121/**************************************************************************************************/
122
123} // namespace adobe
124
125/**************************************************************************************************/
126// ADOBE_JSON_HELPER_HPP
127#endif
128
129/**************************************************************************************************/
A utility class that uses a helper class to access a provided data structure and output well-formed J...
Definition json.hpp:481
O generate(const value_type &value, std::size_t indent=0)
Definition json.hpp:492
A utility class that parses raw JSON data and uses a helper class to construct the desired representa...
Definition json.hpp:49
value_type parse()
Definition json.hpp:67
A runtime polymorphic type similar to boost::any which can hold any type which models Regular.
const std::type_info & type_info() const
O json_generate(const adobe::any_regular_t &x, O out)
A utility routine that takes in an any_regular_t and produces from it a well-formed representation of...
adobe::any_regular_t json_parse(const char *data)
A utility routine that takes in raw JSON data and produces a representation of that data using the co...
#define ADOBE_MESSAGE(message)
closed_hash_map< name_t, any_regular_t > dictionary_t
void terminate(const char *)
std::vector< any_regular_t > array_t
Definition array_fwd.hpp:23
json_type
Definition json.hpp:468
A utility class can be used with json_parser and json_generator to round trip JSON data through the c...
static void append(string_type &str, const char *f, const char *l)
static void move_append(object_type &obj, key_type &key, value_type &value)
static json_type type(const value_type &x)
adobe::array_t array_type
static void move_append(array_type &array, value_type &value)
adobe::dictionary_t object_type
object_type::value_type pair_type
adobe::any_regular_t value_type
static const T & as(const value_type &x)
A character string class for immutable strings.
Definition name.hpp:220
An empty regular- and less-than-comparable- type.
Definition empty.hpp:42