jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
json_converter.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #include "json_converter.hpp"
18 
19 #include <sstream>
20 #include <string>
21 #include "jubatus/util/text/json.h"
22 #include "datum.hpp"
23 
24 using jubatus::util::text::json::json_array;
25 using jubatus::util::text::json::json_bool;
26 using jubatus::util::text::json::json_float;
27 using jubatus::util::text::json::json_integer;
28 using jubatus::util::text::json::json_null;
29 using jubatus::util::text::json::json_object;
30 using jubatus::util::text::json::json_string;
31 
32 namespace jubatus {
33 namespace core {
34 namespace fv_converter {
35 
36 const char* json_converter::NULL_STRING = "null";
37 
38 namespace {
39 
40 void iter_convert(
41  const jubatus::util::text::json::json& json,
42  std::string& root_path,
43  datum& ret_datum);
44 
45 void convert_integer(
46  const json_integer& value,
47  const std::string& path,
48  datum& ret_datum) {
49  ret_datum.num_values_.push_back(std::make_pair(path, value.get()));
50 }
51 
52 void convert_float(
53  const json_float& value,
54  const std::string& path,
55  datum& ret_datum) {
56  ret_datum.num_values_.push_back(std::make_pair(path, value.get()));
57 }
58 
59 void convert_string(
60  const json_string& value,
61  const std::string& path,
62  datum& ret_datum) {
63  ret_datum.string_values_.push_back(std::make_pair(path, value.get()));
64 }
65 
66 void convert_bool(
67  const json_bool& value,
68  const std::string& path,
69  datum& ret_datum) {
70  double v = value.get() ? 1 : 0;
71  ret_datum.num_values_.push_back(std::make_pair(path, v));
72 }
73 
74 void convert_null(
75  const json_null& value,
76  const std::string& path,
77  datum& ret_datum) {
78  ret_datum.string_values_.push_back(
79  std::make_pair(path, json_converter::NULL_STRING));
80 }
81 
82 void convert_array(
83  const jubatus::util::text::json::json_array& value,
84  std::string& path,
85  datum& ret_datum) {
86  size_t len = path.size();
87  for (size_t i = 0; i < value.size(); ++i) {
88  std::ostringstream oss;
89  oss << "[" << i << "]";
90  path += oss.str();
91  iter_convert(value[i], path, ret_datum);
92  path.resize(len);
93  }
94 }
95 
96 void convert_object(
97  const json_object& value,
98  std::string& path,
99  datum& ret_datum) {
100  size_t len = path.size();
101  for (json_object::const_iterator it = value.begin(); it != value.end();
102  ++it) {
103  const std::string& key = it->first;
104  const jubatus::util::text::json::json& val = it->second;
105  path += '/';
106  path += key;
107  iter_convert(val, path, ret_datum);
108  path.resize(len);
109  }
110 }
111 
112 void iter_convert(
113  const jubatus::util::text::json::json& json,
114  std::string& root_path,
115  datum& ret_datum) {
116  jubatus::util::text::json::json_value* value = json.get();
117  if (typeid(*value) == typeid(json_integer)) {
118  json_integer* int_value = dynamic_cast<json_integer*>(value);
119  convert_integer(*int_value, root_path, ret_datum);
120 
121  } else if (typeid(*value) == typeid(json_float)) {
122  json_float* float_value = dynamic_cast<json_float*>(value);
123  convert_float(*float_value, root_path, ret_datum);
124 
125  } else if (typeid(*value) == typeid(json_string)) {
126  json_string* string_value = dynamic_cast<json_string*>(value);
127  convert_string(*string_value, root_path, ret_datum);
128 
129  } else if (typeid(*value) == typeid(json_bool)) {
130  json_bool* bool_value = dynamic_cast<json_bool*>(value);
131  convert_bool(*bool_value, root_path, ret_datum);
132 
133  } else if (typeid(*value) == typeid(json_null)) {
134  json_null* null_value = dynamic_cast<json_null*>(value);
135  convert_null(*null_value, root_path, ret_datum);
136 
137  } else if (typeid(*value) == typeid(jubatus::util::text::json::json_array)) {
138  json_array* array_value = dynamic_cast<json_array*>(value);
139  convert_array(*array_value, root_path, ret_datum);
140 
141  } else if (typeid(*value) == typeid(json_object)) {
142  json_object* object_value = dynamic_cast<json_object*>(value);
143  convert_object(*object_value, root_path, ret_datum);
144  }
145 }
146 
147 } // namespace
148 
150  const jubatus::util::text::json::json& json,
151  datum& ret_datum) {
152  std::string path = "";
153  iter_convert(json, path, ret_datum);
154 }
155 
156 } // namespace fv_converter
157 } // namespace core
158 } // namespace jubatus
static void convert(const jubatus::util::text::json::json &jason, datum &ret_datum)
std::vector< T > v(size)