1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253module juptune.data.json.builder;
struct JsonBuilder(
SinkT
)
{
/++ FUNCTIONS ++/
/// Selectively calls endObject or endArray depending on the latest value in the depth buffer.
Result end()();
/// Ends the current array.
Result endArray()();
/// Ends the current object.
Result endObject()();
/// Checks whether the depth buffer is empty (i.e. the value can be considered "finished").
Result finish();
///
bool inArray();
///
bool inObject();
///
Result put()(const char[] str);
///
Result put(IntT)(IntT number);
///
Result put()(bool bool_);
///
Result put()(typeof(TODO) _);
/// Puts a value within the current array.
Result putArrayValue(ItemT)(ItemT value);
/// Puts a value with the specified key within the current object.
Result putObjectValue(ItemT)(const char[] key, ItemT value);
/// Starts a new array, using the given key if this array is nested inside an object.
Result startArray()(const char[] keyIfInObject = "");
/// Starts a new object, using the given key if this object is nested inside another object.
Result startObject()(const char[] keyIfInObject = "");
/++ MANIFEST CONSTANTS ++/
///
enum int BITS_PER_BYTE = 8;
/++ VARIABLES ++/
///
size_t _depth;
///
ubyte[] _depthMarkers;
///
bool _isFirstItem;
///
SinkT _sink;
}
A lowish-level builder for writing JSON output to a sink.
This struct does not allocate memory, so in order to keep track of whether the builder is inside an object or array, the caller must provide a depth buffer.
Each byte in the buffer allows for 8 levels of depth, so an 8 byte buffer would allow for a max of 64 levels of nesting.
The following types can be passed as values to the putObjectValueand putArrayValuefunctions:
const(char)[]for strings - strings will be ran through juptune.data.utf8.utf8Validate, and outputted as UTF-8.
bool- handled as you'd expect.
Any integer type - signed and unsigned are handled correctly.
Currently there's no support for floating point types due to the lack of a string converter for these types.
The SinkTshould be a type that matches the following: Result delegate(scope const(char)[] text).
This type is templated in order to easily support the sink having whatever sleuth of attributes you desire. Most functions in this struct are templated so that the compiler can infer attributes.
You'll usually need to store the delegate inside a separate scope variable in order to use this struct under @nogc- e.g. scope sink = delegate(scope const(char)[] text) { return Result.noError; }.
This builder does not support outputting UTF-16 surrogate pairs, so some parsers may choke on the generated output if they follow the JSON RFC strictly.
This builder currently does not support pretty printing, largely due to lack of effort :D
This builder does not prevent certain common errors, such as ensuring object keys are unique. This is because it's assumed stuff like this has been handled at a higher level before calling into the builder.
| Name | Description |
|---|---|
| BITS_PER_BYTE |
| Name | Description |
|---|---|
| end |
Selectively calls |
| endArray |
Ends the current array. |
| endObject |
Ends the current object. |
| finish |
Checks whether the depth buffer is empty (i.e. the value can be considered "finished"). |
| inArray | |
| inObject | |
| put | |
| put | |
| put | |
| put | |
| putArrayValue |
Puts a value within the current array. |
| putObjectValue |
Puts a value with the specified key within the current object. |
| startArray |
Starts a new array, using the given key if this array is nested inside an object. |
| startObject |
Starts a new object, using the given key if this object is nested inside another object. |
| Name | Description |
|---|---|
| _depth | |
| _depthMarkers | |
| _isFirstItem | |
| _sink |