jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
msgpack_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 <sstream>
18 #include <string>
19 #include <msgpack.hpp>
20 #include "datum.hpp"
21 #include "msgpack_converter.hpp"
22 
23 namespace jubatus {
24 namespace core {
25 namespace fv_converter {
26 
27 namespace {
28 
29 const char* NULL_VALUE = "NULL";
30 
31 void iter_convert(
32  const msgpack::object& object,
33  const std::string& path,
34  datum& datum) {
35  switch (object.type) {
36  case msgpack::type::NIL: {
37  datum.string_values_.push_back(std::make_pair(path, NULL_VALUE));
38  break;
39  }
40 
41  case msgpack::type::BOOLEAN: {
42  double v = object.via.boolean ? 1. : 0.;
43  datum.num_values_.push_back(std::make_pair(path, v));
44  break;
45  }
46 
47  case msgpack::type::POSITIVE_INTEGER: {
48  double v = object.via.u64;
49  datum.num_values_.push_back(std::make_pair(path, v));
50  break;
51  }
52 
53  case msgpack::type::NEGATIVE_INTEGER: {
54  double v = object.via.i64;
55  datum.num_values_.push_back(std::make_pair(path, v));
56  break;
57  }
58 
59  case msgpack::type::DOUBLE: {
60  double v = object.via.dec;
61  datum.num_values_.push_back(std::make_pair(path, v));
62  break;
63  }
64 
65  case msgpack::type::RAW: {
66  const msgpack::object_raw& raw = object.via.raw;
67  datum.string_values_.push_back(
68  std::make_pair(path, std::string(raw.ptr, raw.size)));
69  break;
70  }
71 
72  case msgpack::type::ARRAY: {
73  const msgpack::object_array& array = object.via.array;
74  for (uint32_t i = 0; i < array.size; ++i) {
75  std::ostringstream oss;
76  oss << path << "[" << i << "]";
77  iter_convert(array.ptr[i], oss.str(), datum);
78  }
79  break;
80  }
81 
82  case msgpack::type::MAP: {
83  const msgpack::object_map& map = object.via.map;
84  for (uint32_t i = 0; i < map.size; ++i) {
85  const msgpack::object_kv& kv = map.ptr[i];
86  std::ostringstream oss;
87  oss << path << "/" << kv.key;
88  iter_convert(kv.val, oss.str(), datum);
89  }
90  }
91  }
92 }
93 
94 } // namespace
95 
96 void msgpack_converter::convert(const msgpack::object& object, datum& datum) {
97  iter_convert(object, "", datum);
98 }
99 
100 } // namespace fv_converter
101 } // namespace core
102 } // namespace jubatus
std::vector< T > v(size)
static void convert(const msgpack::object &object, datum &ret_datum)