Skip to content

Commit 22e67d7

Browse files
committed
Added function to boil parsing down to one line. Also added example to show super mega easy example.
1 parent fc071b4 commit 22e67d7

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,35 @@ Dependencies:
3636
> boost/fusion <br>
3737
> boost/mpl <br>
3838
39+
## Example0
40+
```C++
41+
#ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt
42+
# include "SimpleJSON/parse/jsd.hpp"
43+
# include "SimpleJSON/stringify/jss.hpp"
44+
# include "SimpleJSON/stringify/jss_fusion_adapted_struct.hpp"
45+
#endif
46+
47+
struct Object : public JSON::Stringifiable <Object>
48+
, public JSON::Parsable <Object>
49+
{
50+
int A;
51+
std::string B;
52+
float C;
53+
};
54+
BOOST_FUSION_ADAPT_STRUCT(Object, A, B, C)
55+
56+
int main()
57+
{
58+
auto o = JSON::make_from_json <Object> (R"(
59+
{
60+
"A": 0,
61+
"B": "Hello",
62+
"C": 2.4
63+
}
64+
)");
65+
}
66+
```
67+
3968
## Example1
4069
```C++
4170
#ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt

parse/jsd.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
#include "jsd_object.hpp"
1616
#include "jsd_renamed.hpp"
1717
#include "jsd_unique_ptr.hpp"
18-
#include "jsd_shared_ptr.hpp"
18+
#include "jsd_shared_ptr.hpp"
19+
#include "jsd_convenience.hpp"

parse/jsd_convenience.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ namespace JSON
1818
typename std::enable_if <!Internal::can_parse<T>::value, int>::type* = nullptr)
1919
{
2020
static_assert (Internal::can_parse<T>::value, "the object you try to parse has no applicable interface");
21+
}
22+
23+
template <typename T>
24+
T make_from_json(std::istream& stream)
25+
{
26+
T res;
27+
JSON::parse(res, "", JSON::parse_json(stream));
28+
return res;
29+
}
30+
31+
template <typename T>
32+
T make_from_json(std::string const& str)
33+
{
34+
T res;
35+
JSON::parse(res, "", JSON::parse_json(str));
36+
return res;
2137
}
2238
}
2339

0 commit comments

Comments
 (0)