Tencent / rapidjson

A fast JSON parser/generator for C++ with both SAX/DOM style API
http://rapidjson.org/
Other
14.24k stars 3.54k forks source link

Any equivalent to seekp for inserting when using the outputstream #593

Open adrianboston opened 8 years ago

adrianboston commented 8 years ago
    size_t pos = os_.Tell(); // Yes there is a tellp equivalent, cool
    os_.Seek(pos - 7); // :( but not a seekp
miloyip commented 8 years ago

No. Currently the concept of output stream is sequential. Tell() does not affect the sequential property but seeking will make some streams difficult to write, or may not be possible at all.

Can you describe your usage scenario which requires seeking?

adrianboston commented 8 years ago

the usage is converting nice friendly json into a clunky unfriendly anti-json format. Is it necessary to use FileWriteStream or can one use the ostreamwrapper or a none FP stream yet still use the Outputstream found in jsonx. Thanks for the response.

miloyip commented 8 years ago

You can use existing output streams, or you can implement your own output stream. For the latter case you only need to implement several functions in the concept. But if the JSON is not too large, StringBuffer should be suitable in most cases.

For conversion from JSON to other format, I think you actually only need to use Reader (the SAX API) and create a handler to receive and process the events. You may refer to this example which converts JSON to JSONX.

adrianboston commented 8 years ago

jsonx is a good example. however, it seems to use a standard Reader and then use a Writer class with the handlers to receive and process events, aka bool String(). but.... perhaps im wrong there. ah. ok i see the !reader.Parse(is, writer) so i guess it is done on the Reader.

is there an api describing the "you only need to implement several functions in the concept"

miloyip commented 8 years ago

You may check SAX.