Turi Create  4.0
json_include.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 /******************************************************************************
7  * WARNING! This JSON utility is deprecated. *
8  * For bidirectional JSON serialization of flexible_type, *
9  * use "json_flexible_type.hpp" in src/extensions/json. *
10  ******************************************************************************/
11 
12 #ifndef TURI_JSON_INCLUDE
13 #define TURI_JSON_INCLUDE
14 
15 #define JSON_ISO_STRICT
16 
17 // Get rid of stupid warnings for this library
18 #ifdef __GNUC__
19 #pragma GCC diagnostic push
20 #pragma GCC diagnostic ignored "-Wreorder"
21 #endif
22 
23 #ifdef __clang__
24 #pragma clang diagnostic push
25 #pragma clang diagnostic ignored "-Wreorder"
26 #endif
27 
28 
29 #include <libjson/libjson.h>
30 #ifdef __GNUC__
31 #pragma GCC diagnostic pop
32 #endif
33 
34 #ifdef __clang__
35 #pragma clang diagnostic pop
36 #endif
37 #include <vector>
38 #include <map>
39 // some useful utilities
40 namespace turi {
41 namespace json {
42 
43 
44 /**
45  * Writes a vector of values into a JSON entry.
46  *
47  * For instance, given a 3 element vector containing {"hello", "pika", "chu"}
48  * The vector be represented as
49  *
50  * key:["hello", "pika", "chu"]
51  *
52  * \see read_sequence_section
53  *
54  */
55 template <typename T>
56 JSONNode to_json_node(const std::string& key,
57  const std::vector<T>& values) {
58  JSONNode ret(JSON_ARRAY);
59  ret.set_name(key);
60  for (size_t i = 0; i < values.size(); ++i) {
61  ret.push_back(JSONNode("", values[i]));
62  }
63  return ret;
64 }
65 
66 #ifdef _WIN32
67 // this specialization is needed on some compilers because
68 // size_t --> JSONNode int types is ambiguous. Because unsigned long is 32 bit.
69 // on windows and size_t is 64-bit, there is no JSONNode overload to handle
70 // 64 bit integers and casting has to happen. So it becomes ambiguous what
71 // you can cast to.
72 template <>
73 inline JSONNode to_json_node<size_t>(const std::string& key,
74  const std::vector<size_t>& values) {
75  JSONNode ret(JSON_ARRAY);
76  ret.set_name(key);
77  for (size_t i = 0; i < values.size(); ++i) {
78  ret.push_back(JSONNode("", (unsigned long)values[i]));
79  }
80  return ret;
81 }
82 #endif
83 /**
84  * Writes a dictionary of values into an JSON entry.
85  * For instance, given a 3 element map containing
86  * {"fish":"hello", "and":"pika", "chips":"chu"}.
87  *
88  * In the json file this will be represented as:
89  *
90  * {"key": {"fish":"hello",
91  * "and":"pika",
92  * "chips":"chu"}}
93  *
94  * \see read_dictionary_section
95  *
96  */
97 template <typename T>
98 JSONNode to_json_node(const std::string& key,
99  const std::map<std::string, T>& values) {
100  JSONNode ret(JSON_NODE);
101  ret.set_name(key);
102  for(const auto& map_entry : values) {
103  ret.push_back(JSONNode(map_entry.first, map_entry.second));
104  }
105  return ret;
106 }
107 
108 } // json
109 } // turicreate
110 #endif
std::set< T > values(const std::map< Key, T > &map)
Definition: stl_util.hpp:386