Add documentation
This commit is contained in:
parent
c44d552498
commit
546eaa5d4d
1 changed files with 30 additions and 0 deletions
30
json.c
30
json.c
|
|
@ -16,6 +16,8 @@ Json json_init(FILE* stream, Whitespace whitespace)
|
||||||
|
|
||||||
void json_free(Json* json)
|
void json_free(Json* json)
|
||||||
{
|
{
|
||||||
|
// `json_free` just exists for symmetry with `json_init`.
|
||||||
|
// It does nothing, for now.
|
||||||
}
|
}
|
||||||
|
|
||||||
void begin_value(Json* json);
|
void begin_value(Json* json);
|
||||||
|
|
@ -24,6 +26,7 @@ void push_indentation(Json* json);
|
||||||
void pop_indentation(Json* json);
|
void pop_indentation(Json* json);
|
||||||
void indent(Json* json);
|
void indent(Json* json);
|
||||||
|
|
||||||
|
/// Begin a JSON array value.
|
||||||
void json_begin_array(Json* json)
|
void json_begin_array(Json* json)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
|
|
@ -32,6 +35,7 @@ void json_begin_array(Json* json)
|
||||||
json->next_punctuation = NONE;
|
json->next_punctuation = NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// End a JSON array value.
|
||||||
void json_end_array(Json* json)
|
void json_end_array(Json* json)
|
||||||
{
|
{
|
||||||
pop_indentation(json);
|
pop_indentation(json);
|
||||||
|
|
@ -43,12 +47,17 @@ void json_end_array(Json* json)
|
||||||
break;
|
break;
|
||||||
case BEGINNING:
|
case BEGINNING:
|
||||||
case COLON:
|
case COLON:
|
||||||
|
// The only way to get here is by either:
|
||||||
|
// - calling `json_end_array` right after `json_init`
|
||||||
|
// - calling `json_end_array` right after `json_add_object_field`
|
||||||
|
// - setting `next_punctuation` explicitly
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
putc(']', json->stream);
|
putc(']', json->stream);
|
||||||
end_value(json);
|
end_value(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Begin a JSON object value.
|
||||||
void json_begin_object(Json* json)
|
void json_begin_object(Json* json)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
|
|
@ -57,6 +66,7 @@ void json_begin_object(Json* json)
|
||||||
json->next_punctuation = NONE;
|
json->next_punctuation = NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// End a JSON object value.
|
||||||
void json_end_object(Json* json)
|
void json_end_object(Json* json)
|
||||||
{
|
{
|
||||||
pop_indentation(json);
|
pop_indentation(json);
|
||||||
|
|
@ -68,12 +78,18 @@ void json_end_object(Json* json)
|
||||||
break;
|
break;
|
||||||
case BEGINNING:
|
case BEGINNING:
|
||||||
case COLON:
|
case COLON:
|
||||||
|
// The only way to get here is by either:
|
||||||
|
// - calling `json_end_object` right after `json_init`
|
||||||
|
// - calling `json_end_object` right after `json_add_object_field`
|
||||||
|
// - settings `next_punctuation` explicitly
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
putc('}', json->stream);
|
putc('}', json->stream);
|
||||||
end_value(json);
|
end_value(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a JSON object field key.
|
||||||
|
/// This should always be followed by adding a JSON value.
|
||||||
void json_add_object_field(Json* json, const char* key)
|
void json_add_object_field(Json* json, const char* key)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
|
|
@ -81,6 +97,7 @@ void json_add_object_field(Json* json, const char* key)
|
||||||
json->next_punctuation = COLON;
|
json->next_punctuation = COLON;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a JSON string value.
|
||||||
void json_add_string(Json* json, const char* value)
|
void json_add_string(Json* json, const char* value)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
|
|
@ -88,6 +105,7 @@ void json_add_string(Json* json, const char* value)
|
||||||
end_value(json);
|
end_value(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a JSON long value.
|
||||||
void json_add_long(Json* json, long value)
|
void json_add_long(Json* json, long value)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
|
|
@ -95,6 +113,7 @@ void json_add_long(Json* json, long value)
|
||||||
end_value(json);
|
end_value(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a JSON double value.
|
||||||
void json_add_double(Json* json, double value)
|
void json_add_double(Json* json, double value)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
|
|
@ -102,6 +121,7 @@ void json_add_double(Json* json, double value)
|
||||||
end_value(json);
|
end_value(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a JSON bool value.
|
||||||
void json_add_bool(Json* json, bool value)
|
void json_add_bool(Json* json, bool value)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
|
|
@ -115,6 +135,7 @@ void json_add_bool(Json* json, bool value)
|
||||||
end_value(json);
|
end_value(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a JSON null value
|
||||||
void json_add_null(Json* json)
|
void json_add_null(Json* json)
|
||||||
{
|
{
|
||||||
begin_value(json);
|
begin_value(json);
|
||||||
|
|
@ -122,6 +143,8 @@ void json_add_null(Json* json)
|
||||||
end_value(json);
|
end_value(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Begin a new JSON value.
|
||||||
|
/// Appends the appropriate punctuation to the stream.
|
||||||
void begin_value(Json* json)
|
void begin_value(Json* json)
|
||||||
{
|
{
|
||||||
switch (json->next_punctuation) {
|
switch (json->next_punctuation) {
|
||||||
|
|
@ -143,27 +166,33 @@ void begin_value(Json* json)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// End a JSON value.
|
||||||
void end_value(Json* json)
|
void end_value(Json* json)
|
||||||
{
|
{
|
||||||
json->next_punctuation = COMMA;
|
json->next_punctuation = COMMA;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Increment the indent level by one.
|
||||||
void push_indentation(Json* json)
|
void push_indentation(Json* json)
|
||||||
{
|
{
|
||||||
json->indent_level += 1;
|
json->indent_level += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Decrement the indent level by one.
|
||||||
void pop_indentation(Json* json)
|
void pop_indentation(Json* json)
|
||||||
{
|
{
|
||||||
json->indent_level -= 1;
|
json->indent_level -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Append a newline and whitespace for the next line,
|
||||||
|
/// except when `whitespace` is set to `MINIFIED`.
|
||||||
void indent(Json* json)
|
void indent(Json* json)
|
||||||
{
|
{
|
||||||
char indent_char = ' ';
|
char indent_char = ' ';
|
||||||
int n;
|
int n;
|
||||||
switch (json->whitespace) {
|
switch (json->whitespace) {
|
||||||
case MINIFIED:
|
case MINIFIED:
|
||||||
|
// In minified mode, we don't want newlines or whitespace.
|
||||||
return;
|
return;
|
||||||
case INDENT_1:
|
case INDENT_1:
|
||||||
n = 1 * json->indent_level;
|
n = 1 * json->indent_level;
|
||||||
|
|
@ -185,6 +214,7 @@ void indent(Json* json)
|
||||||
n = json->indent_level;
|
n = json->indent_level;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
putc('\n', json->stream);
|
putc('\n', json->stream);
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
putc(indent_char, json->stream);
|
putc(indent_char, json->stream);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue