Skip to content

Commit 50e1802

Browse files
committed
Code cleanup and any test case
1 parent 067fd07 commit 50e1802

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ The library has a number of features, which are listed below:
2727

2828
## What's new on master
2929

30-
- Enhancements to processing CSV files to output JSON, see example below.
31-
- The json value `v` stored in a `json_deserializer` is now retrieved by calling the member function `get_result`, which returns it as `std::move(v)`.
30+
- Enhances parser for CSV files that outputs JSON, see example below.
31+
- Adds `get_result` member function to `json_deserializer`, which returns the json value `v` stored in a `json_deserializer` as `std::move(v)`. The `root()` member function has been deprecated but is still supported.
32+
- Adds `is_valid` member function to `json_deserializer`
33+
- Enhances json::any class, adds type checks when casting back to original value
3234

3335
## Using the jsoncons library
3436

src/jsoncons/json_deserializer.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,17 @@ class basic_json_deserializer : public basic_json_input_handler<Char>
5050
basic_json<Char,Alloc> get_result()
5151
{
5252
is_valid_ = false;
53-
return std::move(result);
53+
return std::move(result_);
5454
}
5555

5656
// Deprecated
5757
basic_json<Char,Alloc>& root()
5858
{
59-
return result;
59+
return result_;
6060
}
6161

62-
basic_json<Char,Alloc> result;
63-
6462
private:
63+
basic_json<Char,Alloc> result_;
6564

6665
void push_object()
6766
{
@@ -129,7 +128,7 @@ class basic_json_deserializer : public basic_json_input_handler<Char>
129128
}
130129
else
131130
{
132-
result.swap(stack_[0].value);
131+
result_.swap(stack_[0].value);
133132
}
134133
pop_object();
135134
}
@@ -154,7 +153,7 @@ class basic_json_deserializer : public basic_json_input_handler<Char>
154153
}
155154
else
156155
{
157-
result.swap(stack_[0].value);
156+
result_.swap(stack_[0].value);
158157
}
159158
pop_array();
160159
}

test_suite/src/json_any_tests.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ using jsoncons::json_exception;
2222
using std::string;
2323
using boost::numeric::ublas::matrix;
2424

25+
bool check_any_exception( jsoncons::json_exception_0 const& ex ) { return true; }
26+
27+
BOOST_AUTO_TEST_CASE(test_any_const_ref)
28+
{
29+
json obj;
30+
matrix<double> A(2,2);
31+
A(0,0) = 1;
32+
A(0,1) = 2;
33+
A(1,0) = 3;
34+
A(1,1) = 4;
35+
36+
obj.set("A",json::any(A));
37+
38+
const matrix<double>& B = obj["A"].any_cast<const matrix<double>&>();
39+
40+
BOOST_CHECK_CLOSE(B(0,0),1.0,0.0000001);
41+
42+
const matrix<double> C = obj["A"].any_cast<const matrix<double>>();
43+
44+
BOOST_CHECK_CLOSE(C(0,0),1.0,0.0000001);
45+
46+
BOOST_CHECK_EXCEPTION(obj["A"].any_cast<const matrix<int>>(), jsoncons::json_exception_0, check_any_exception);
47+
}
48+
2549
BOOST_AUTO_TEST_CASE(test_any)
2650
{
2751
json obj;

0 commit comments

Comments
 (0)