Skip to content

Commit 541c944

Browse files
committed
Update JSON documentation to describe response::Writer
1 parent 7fbcab6 commit 541c944

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

doc/json.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,49 @@ comment in that file for more information:
3737
# implementation, and you should set BUILD_GRAPHQLJSON so that the test dependencies know
3838
# about your version of graphqljson.
3939
option(GRAPHQL_USE_RAPIDJSON "Use RapidJSON for JSON serialization." ON)
40-
```
40+
```
41+
42+
## response::Writer
43+
44+
You can plug-in a type-erased streaming `response::Writer` to serialize a `response::Value`
45+
to some other output mechanism, without building a single string buffer for the entire
46+
document in memory. For example, you might use this to write directly to a buffered IPC pipe
47+
or network connection:
48+
```cpp
49+
class Writer
50+
{
51+
private:
52+
struct Concept
53+
{
54+
virtual ~Concept() = default;
55+
56+
virtual void start_object() const = 0;
57+
virtual void add_member(const std::string& key) const = 0;
58+
virtual void end_object() const = 0;
59+
60+
virtual void start_array() const = 0;
61+
virtual void end_arrary() const = 0;
62+
63+
virtual void write_null() const = 0;
64+
virtual void write_string(const std::string& value) const = 0;
65+
virtual void write_bool(bool value) const = 0;
66+
virtual void write_int(int value) const = 0;
67+
virtual void write_float(double value) const = 0;
68+
};
69+
...
70+
71+
public:
72+
template <class T>
73+
Writer(std::unique_ptr<T> writer)
74+
: _concept { std::static_pointer_cast<Concept>(
75+
std::make_shared<Model<T>>(std::move(writer))) }
76+
{
77+
}
78+
79+
GRAPHQLRESPONSE_EXPORT void write(Value value) const;
80+
};
81+
```
82+
83+
Internally, this is what `graphqljson` uses to implement `response::toJSON` with RapidJSON.
84+
It wraps a `rapidjson::Writer` in `response::Writer` and then writes into a
85+
`rapidjson::StringBuffer` through that.

0 commit comments

Comments
 (0)