1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
// Copyright 2013-2025 Daniel Parker
// Distributed under Boost license
#include <jsoncons_ext/csv/csv.hpp>
#include <jsoncons/json.hpp>
#include <sstream>
#include <vector>
#include <utility>
#include <ctime>
#include <cassert>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
namespace jsoncons {
template <typename Json>
struct json_type_traits<Json,boost::gregorian::date>
{
static bool is(const Json& val) noexcept
{
if (!val.is_string())
return false;
try
{
std::string s = val.template as<std::string>();
boost::gregorian::from_simple_string(s);
return true;
}
catch (...)
{
return false;
}
}
static boost::gregorian::date as(const Json& val)
{
std::string s = val.template as<std::string>();
return boost::gregorian::from_simple_string(s);
}
static Json to_json(boost::gregorian::date val,
typename Json::allocator_type alloc = Json::allocator_type())
{
return Json(to_iso_extended_string(val), alloc);
}
};
template <typename Json,typename Backend>
struct json_type_traits<Json,boost::multiprecision::number<Backend>>
{
using multiprecision_type = boost::multiprecision::number<Backend>;
static bool is(const Json& val) noexcept
{
if (!(val.is_string() && val.tag() == semantic_tag::bigdec))
{
return false;
}
else
{
return true;
}
}
static multiprecision_type as(const Json& val)
{
return multiprecision_type(val.template as<std::string>());
}
static Json to_json(multiprecision_type val)
{
return Json(val.str(), semantic_tag::bigdec);
}
};
template <typename Json,typename T>
struct json_type_traits<Json,boost::numeric::ublas::matrix<T>>
{
using allocator_type = typename Json::allocator_type;
static bool is(const Json& val) noexcept
{
if (!val.is_array())
{
return false;
}
if (val.size() > 0)
{
std::size_t n = val[0].size();
for (const auto& a: val.array_range())
{
if (!(a.is_array() && a.size() == n))
{
return false;
}
for (auto x: a.array_range())
{
if (!x.template is<T>())
{
return false;
}
}
}
}
return true;
}
static boost::numeric::ublas::matrix<T> as(const Json& val)
{
if (val.is_array() && val.size() > 0)
{
std::size_t m = val.size();
std::size_t n = 0;
for (const auto& a : val.array_range())
{
if (a.size() > n)
{
n = a.size();
}
}
boost::numeric::ublas::matrix<T> A(m,n,T());
for (std::size_t i = 0; i < m; ++i)
{
const auto& a = val[i];
for (std::size_t j = 0; j < a.size(); ++j)
{
A(i,j) = a[j].template as<T>();
}
}
return A;
}
else
{
boost::numeric::ublas::matrix<T> A;
return A;
}
}
static Json to_json(const boost::numeric::ublas::matrix<T>& val,
allocator_type alloc = allocator_type())
{
Json a = Json::template make_array<2>(val.size1(), val.size2(), T());
for (std::size_t i = 0; i < val.size1(); ++i)
{
for (std::size_t j = 0; j < val.size1(); ++j)
{
a[i][j] = val(i,j);
}
}
return a;
}
};
} // namespace jsoncons
namespace ns {
class fixing
{
std::string index_id_;
boost::gregorian::date observation_date_;
double rate_;
public:
fixing(const std::string& index_id, boost::gregorian::date observation_date, double rate)
: index_id_(index_id), observation_date_(observation_date), rate_(rate)
{
}
const std::string& index_id() const {return index_id_;}
boost::gregorian::date observation_date() const {return observation_date_;}
double rate() const {return rate_;}
};
} namespace ns
JSONCONS_ALL_CTOR_GETTER_TRAITS(ns::fixing, index_id, observation_date, rate)
using namespace jsoncons;
using boost::numeric::ublas::matrix;
void boost_date_conversions()
{
using boost::gregorian::date;
json deal;
deal["maturity"] = date(2014,10,14);
json observation_dates(json_array_arg);
observation_dates.push_back(date(2014,2,14));
observation_dates.push_back(date(2014,2,21));
deal["observationDates"] = std::move(observation_dates);
date maturity = deal["maturity"].as<date>();
assert(deal["maturity"].as<date>() == date(2014,10,14));
assert(deal["observationDates"].is_array());
assert(deal["observationDates"].size() == 2);
assert(deal["observationDates"][0].as<date>() == date(2014,2,14));
assert(deal["observationDates"][1].as<date>() == date(2014,2,21));
std::cout << pretty_print(deal) << '\n';
}
void boost_matrix_conversions()
{
matrix<double> A(2, 2);
A(0, 0) = 1.1;
A(0, 1) = 2.1;
A(1, 0) = 3.1;
A(1, 1) = 4.1;
json a = A;
assert(a.is<matrix<double>>());
assert(!a.is<matrix<int>>());
assert(a[0][0].as<double>()==A(0,0));
assert(a[0][1].as<double>()==A(0,1));
assert(a[1][0].as<double>()==A(1,0));
assert(a[1][1].as<double>()==A(1,1));
matrix<double> B = a.as<matrix<double>>();
assert(B.size1() ==a.size());
assert(B.size2() ==a[0].size());
assert(a[0][0].as<double>()==B(0,0));
assert(a[0][1].as<double>()==B(0,1));
assert(a[1][0].as<double>()==B(1,0));
assert(a[1][1].as<double>()==B(1,1));
}
void boost_multiprecison_conversions()
{
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float_50> multiprecision_type;
std::string s = "[100000000000000000000000000000000.1234]";
auto options = json_options{}
.lossless_number(true);
json j = json::parse(s, options);
multiprecision_type x = j[0].as<multiprecision_type>();
std::cout << "(1) " << std::setprecision(std::numeric_limits<multiprecision_type>::max_digits10)
<< x << "\n";
json j2(json_array_arg,{x});
std::cout << "(2) " << j2[0].as<std::string>() << "\n";
}
void csv_strongly_typed_example()
{
const std::string data = R"(index_id,observation_date,rate
EUR_LIBOR_06M,2015-10-23,0.0000214
EUR_LIBOR_06M,2015-10-26,0.0000143
EUR_LIBOR_06M,2015-10-27,0.0000001
)";
auto options = csv::csv_options{}
.assume_header(true);
// Decode the CSV data into a c++ structure
std::vector<ns::fixing> v = csv::decode_csv<std::vector<ns::fixing>>(data, options);
// Iterate over values
std::cout << std::fixed << std::setprecision(7);
std::cout << "(1)\n";
for (const auto& item : v)
{
std::cout << item.index_id() << ", " << item.observation_date() << ", " << item.rate() << "\n";
}
// Encode the c++ structure into CSV data
std::string s;
csv::encode_csv(v, s);
std::cout << "(2)\n";
std::cout << s << "\n";
//std::vector<json> v = csv::decode_csv<std::vector<json>>(data, options);
//std::cout << v[0]["observation_date"].as<boost::gregorian::date>() << "\n";
}
void extensibility_examples()
{
std::cout << "extensibility examples\n\n";
boost_date_conversions();
boost_matrix_conversions();
boost_multiprecison_conversions();
csv_strongly_typed_example();
}
|