Adobe Source Libraries  1.43
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/json.hpp>
12 #include <adobe/any_regular.hpp>
13 #include <adobe/array.hpp>
14 #include <adobe/dictionary.hpp>
15 
16 /**************************************************************************************************/
17 
18 namespace adobe {
19 
20 /**************************************************************************************************/
29  /*
30  We use std::string as key type instead of adobe::name_t because of the
31  append, move, etc., capabilities of the former type. When we go to
32  insert a key/value pair into the dictionary we convert the key to a name_t
33  first. This is a better route to take than building out name_t to behave
34  more like a string type, which it is not intended to be.
35  */
36  typedef adobe::any_regular_t value_type;
39  typedef adobe::dictionary_t object_type;
41  typedef object_type::value_type pair_type;
42 
43  static json_type type(const value_type& x) {
44  const std::type_info& type(x.type_info());
45 
46  if (type == typeid(object_type)) return json_type::object;
47  else if (type == typeid(array_type)) return json_type::array;
48  else if (type == typeid(string_type)) return json_type::string;
49  else if (type == typeid(double)) return json_type::number;
50  else if (type == typeid(bool)) return json_type::boolean;
51  else if (type == typeid(adobe::empty_t)) return json_type::null;
52 
53  ADOBE_ASSERT(false && "invalid type for serialization");
54  }
55 
56  template <typename T>
57  static const T& as(const value_type& x) {
58  return x.cast<T>();
59  }
60 
61  static void move_append(object_type& obj, key_type& key, value_type& value) {
62  obj[adobe::name_t(key.c_str())] = std::move(value);
63  key.clear();
64  }
65  static void append(string_type& str, const char* f, const char* l) {
66  str.append(f, l);
67  }
68  static void move_append(array_type& array, value_type& value) {
69  array.emplace_back(std::move(value));
70  }
71 };
72 
73 /**************************************************************************************************/
86 inline adobe::any_regular_t json_parse(const char* data)
87 {
88  return json_parser<asl_json_helper_t>(data).parse();
89 }
90 
111 template <typename O>
112 inline O json_generate(const adobe::any_regular_t& x, O out)
113 {
114  return json_generator<asl_json_helper_t, O>(out).generate(x);
115 }
116 
117 /**************************************************************************************************/
118 
119 } // namespace adobe
120 
121 /**************************************************************************************************/
122 // ADOBE_JSON_HELPER_HPP
123 #endif
124 
125 /**************************************************************************************************/
A utility class that uses a helper class to access a provided data structure and output well-formed J...
Definition: json.hpp:401
adobe::dictionary_t object_type
Definition: json_helper.hpp:39
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...
A utility class that parses raw JSON data and uses a helper class to construct the desired representa...
Definition: json.hpp:53
static void append(string_type &str, const char *f, const char *l)
Definition: json_helper.hpp:65
adobe::array_t array_type
Definition: json_helper.hpp:40
static void move_append(array_type &array, value_type &value)
Definition: json_helper.hpp:68
json_type
Definition: json.hpp:385
static json_type type(const value_type &x)
Definition: json_helper.hpp:43
static const T & as(const value_type &x)
Definition: json_helper.hpp:57
static void move_append(object_type &obj, key_type &key, value_type &value)
Definition: json_helper.hpp:61
std::vector< any_regular_t > array_t
Definition: array_fwd.hpp:24
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...
Definition: json_helper.hpp:86
object_type::value_type pair_type
Definition: json_helper.hpp:41
A utility class can be used with json_parser and json_generator to round trip JSON data through the c...
Definition: json_helper.hpp:28
adobe::any_regular_t value_type
Definition: json_helper.hpp:36