Skip to content

Commit fc0a657

Browse files
committed
Improved utility, added macros for structs that cannot inherit, updated readme.
1 parent 669c9e8 commit fc0a657

File tree

8 files changed

+135
-129
lines changed

8 files changed

+135
-129
lines changed

README.md

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
2. [Introduction](https://github.com/5cript/SimpleJSON#introduction)
66
3. [Example 1](https://github.com/5cript/SimpleJSON#Example1)
77
4. [Example 2](https://github.com/5cript/SimpleJSON#Example2)
8+
4. [Useful Utility](https://github.com/5cript/SimpleJSON#Useful-Utility)
89
5. [Details](https://github.com/5cript/SimpleJSON#details)
910
* [How does stringify work?](https://github.com/5cript/SimpleJSON#how-does-stringify-work)
1011
* [stringify behaviour (and STL stringification)](https://github.com/5cript/SimpleJSON#stringify-behaviour-and-stl-stuff-stringification)
1112
6. [Reference](https://github.com/5cript/SimpleJSON#reference)
1213

1314
## Preface
14-
Please submit pull requests if you don't agree with some behaviour or found a bug, I would appreciate it.
15+
Please submit pull requests if you don't agree with some behavior or found a bug, I would appreciate it.
1516
The library is further matured now and changes less.
1617

17-
This library can parse and stringify and is designed for easy use.
18+
This library can parse and stringify and is designed for ease of use.
1819
Nobody wants to write parsing and stringification methods for every class they write. We rather want it
1920
to work "just like that" without thinking about it. This is where this library fits in.
2021
This idea of producing and consuming JSON has become the "Hello World of Introspection".
@@ -24,19 +25,19 @@ Since release 0.3, the library also features basic JSON beautification using boo
2425
## Introduction
2526
A JSON stringifier / parser that uses boost fusion introspection methods for automagic struct <-> JSON conversion
2627

27-
Its supports almost all STL contstructs in stringify and the most important for parse.
28+
It supports almost all STL contstructs in stringify and the most important for parse.
2829
With the STL as a basis it is an easy to extend mechanism using classes. Use boost fusion and the provided utility
2930
(see example below) or provide your own parse/stringify methods.
3031

3132
NOTE: The performance of this library is mostly influenced by boost property tree which is used for parsing JSON.
32-
The main focus of this library is not speed, but ease of use and convenience. If you want to be fast, try RapidJson (not saying it is particullarly slow, but probably not suitable for high data frequency or big bulk data application)
33+
The main focus of this library is not speed, but ease of use and convenience. If you want to be fast, try RapidJson (not saying it is particularly slow, but probably not suitable for high data frequency or big bulk data application)
3334

3435
Dependencies:
3536
> boost/property_tree <br>
3637
> boost/fusion <br>
3738
> boost/mpl <br>
3839
39-
## Example0
40+
## Example 0
4041
```C++
4142
#ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt
4243
# include <SimpleJSON/parse/jsd.hpp>
@@ -65,21 +66,13 @@ int main()
6566
}
6667
```
6768
68-
## Example1
69+
## Example 1
6970
```C++
70-
#ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt
71-
# include <SimpleJSON/parse/jsd.hpp>
72-
# include <SimpleJSON/parse/jsd_convenience.hpp>
73-
# include <SimpleJSON/stringify/jss.hpp>
74-
# include <SimpleJSON/stringify/jss_fusion_adapted_struct.hpp>
75-
#endif
76-
7771
#include <string>
7872
#include <vector>
7973
#include <sstream>
8074
81-
struct ConfigContent : public JSON::Stringifiable <ConfigContent>
82-
, public JSON::Parsable <ConfigContent>
75+
struct ConfigContent
8376
{
8477
int id;
8578
std::string libPath;
@@ -94,21 +87,8 @@ BOOST_FUSION_ADAPT_STRUCT
9487
(std::vector <std::string>, someContainer)
9588
)
9689
97-
ConfigContent parse(std::istream& json)
98-
{
99-
ConfigContent cc;
100-
auto tree = JSON::parse_json(json);
101-
JSON::parse(cc, "config_content", tree);
102-
return cc;
103-
}
104-
105-
std::ostream& stringify(std::ostream& stream, ConfigContent const& cc)
106-
{
107-
stream << "{";
108-
JSON::try_stringify(stream, "config_content", cc, JSON::ProduceNamedOutput);
109-
stream << "}";
110-
return stream;
111-
}
90+
JSON_INJECT_STRINGIFY(ConfigContent)
91+
JSON_INJECT_PARSE(ConfigContent)
11292
11393
int main()
11494
{
@@ -118,8 +98,9 @@ int main()
11898
cc.someContainer = {"Hello", "World"};
11999
120100
std::stringstream sstr;
121-
stringify(sstr, cc);
122-
auto unnecessarilyComplexCopy = parse(sstr);
101+
JSON::stringify(sstr, "", cc);
102+
ConfigContent unnecessarilyComplexCopy;
103+
JSON::parse(unnecessarilyComplexCopy, "", JSON::parse_json(sstr));
123104
124105
/////////////////////////////////////////////////////////////////////////
125106
// Lets check if we got what we set
@@ -133,7 +114,30 @@ int main()
133114
}
134115
```
135116

136-
## Example2
117+
##Example 1B
118+
An alternatvie to deriving. Does not handle polymorphic structs.
119+
Especially useful for structs, you did not create.
120+
```C++
121+
struct ConfigContent
122+
{
123+
int id;
124+
std::string libPath;
125+
std::vector <std::string> someContainer;
126+
};
127+
128+
BOOST_FUSION_ADAPT_STRUCT
129+
(
130+
ConfigContent,
131+
(int, id)
132+
(std::string, libPath)
133+
(std::vector <std::string>, someContainer)
134+
)
135+
136+
JSON_INJECT_STRINGIFY(ConfigContent)
137+
JSON_INJECT_PARSE(ConfigContent)
138+
```
139+
140+
## Example 2
137141
(Showing polymorphic serlialization / deserialization)
138142
```C++
139143
#ifndef Q_MOC_RUN // A Qt workaround, for those of you who use Qt
@@ -235,6 +239,29 @@ int main()
235239
}
236240
```
237241

242+
## Useful Utility
243+
### JSON::Base64 <T> mem;
244+
mem will be handled as a base64 string. useful if mem can contain any character or binary sequence, including quotes.
245+
246+
### fill_missing
247+
```C++
248+
auto tree = JSON::parse_json(str); // str contains some JSON.
249+
JSON::fill_missing <MyJsonType> ("", tree); // missing members in the tree of MyJsonType get default constructed.
250+
```
251+
### JSON::rename <T, Name>
252+
Renames a member. This is necessary if something is not a valid C++ identifier, such as "." or "6".
253+
```C++
254+
struct MyJsonType : /* ... */
255+
{
256+
/**
257+
* SJSON_SHORT_STRING -> handles up to 16 characters. (prefer whenever possible)
258+
* SJSON_STRING -> handles up to 64 characters.
259+
* SJSON_LONG_STRING -> 256 chars. (dont use if possible, heavy compiler performance hit)
260+
* SJSON_LONG_LONG_STRING -> 1024 chars. (dont use if possible, heavy compiler performance hit)
261+
*/
262+
JSON::rename <std::string, SJSON_SHORT_STRING("__.bla")> blaProperty;
263+
};
264+
```
238265

239266
## Details
240267
### How does stringify work?

parse/jsd_fusion_adapted_struct.hpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ namespace JSON
1919
class AdaptedParser
2020
{
2121
public:
22-
typename std::enable_if <Internal::isParsable<T>::value, void>::type
22+
//typename std::enable_if <Internal::isParsable<T>::value, void>::type
23+
void
2324
operator()(T& object, std::string const& name, PropertyTree const& tree, ParsingOptions const& options) const
2425
{
2526
//! If you get an Error here, you likely forgot to use BOOST_FUSION_ADAPT_STRUCT !
@@ -73,4 +74,19 @@ namespace JSON
7374
}
7475
virtual ~Parsable() = default;
7576
};
77+
}
78+
79+
#define JSON_INJECT_PARSE(ClassName) \
80+
namespace JSON \
81+
{ \
82+
void parse( \
83+
ClassName& obj, \
84+
std::string const& name, \
85+
PropertyTree const& tree, \
86+
ParsingOptions const& options = {} \
87+
) \
88+
{ \
89+
AdaptedParser<ClassName> parser; \
90+
parser(obj, name, tree, options); \
91+
} \
7692
}

stringify/jss_container.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
#include "jss_core.hpp"
44
#include "jss_object.hpp"
55
#include "jss_check.hpp"
6-
#include "jss_optional.hpp"
6+
#include "jss_optional.hpp"
7+
#include "../utility/optional_info.hpp"
78
#include <functional>
89

910
namespace JSON { namespace Internal

stringify/jss_fusion_adapted_struct.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,22 @@ namespace JSON
117117
}
118118
virtual ~Stringifiable() = default;
119119
};
120+
}
121+
122+
#define JSON_INJECT_STRINGIFY(ClassName) \
123+
namespace JSON \
124+
{ \
125+
std::ostream& stringify( \
126+
std::ostream& stream, \
127+
std::string const& name, \
128+
ClassName const& value, \
129+
StringificationOptions options = {} \
130+
) \
131+
{ \
132+
WRITE_NAME(stream); \
133+
options.in_object = true; \
134+
options.ignore_name = false; \
135+
AdaptedStringifier <ClassName> stringifier; \
136+
return stringifier(stream, value, options); \
137+
} \
120138
}

stringify/jss_object.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,6 @@ namespace JSON
5252

5353
std::ostream& js_make_object(std::ostream& stream, std::vector <std::string> const& elements, StringificationOptions const& options = {});
5454

55-
#define JSON_VERIFY_CLASS_VALIDITY(T) \
55+
#define JSON_ASSERT_CLASS_VALIDITY(T) \
5656
static_assert (JSON::Internal::is_js_object <T>::value, "The class/struct is not a stringifiable struct, please check the function signature");
5757
}

test.cpp

Lines changed: 0 additions & 59 deletions
This file was deleted.

test.hpp

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)