tetengo 1.8.1
A multipurpose library set
Loading...
Searching...
No Matches
tetengo::json Namespace Reference

A JSON library. More...

Classes

class  channel
 A channel. More...
 
class  comment_removing_reader
 A comment removing reader. More...
 
class  element
 An element. More...
 
class  file_location
 A file location. More...
 
class  json_grammar
 A JSON grammar. More...
 
class  json_parser
 A JSON parser. More...
 
class  line_counting_reader
 A line counting reader. More...
 
class  reader
 A reader. More...
 
class  reader_iterator
 A reader iterator. More...
 
class  stream_reader
 A stream reader. More...
 

Detailed Description

A JSON library.

Usage

Parsing JSON

C++

#include <cassert>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
namespace usage_tetengo::json
{
std::string to_string(const tetengo::json::element& element_);
void parsing()
{
static const std::string json_text{
// clang-format off
"{\n"
" \"hoge\": 42,\n"
" \"fuga\": [ \"foo\", \"bar\" ]\n"
"}\n"
// clang-format on
};
auto p_json_stream = std::make_unique<std::istringstream>(json_text);
// Creates a reader from std::stream.
auto p_reader = std::make_unique<tetengo::json::stream_reader>(std::move(p_json_stream));
// Creates a JSON parser.
tetengo::json::json_parser parser{ std::move(p_reader) };
// Iteration.
std::vector<std::string> elements{};
while (parser.has_next())
{
// Obtains the current element.
const auto& element = parser.peek();
elements.push_back(to_string(element));
// Moves to the next element.
parser.next();
}
static const std::vector<std::string> expected{
// clang-format off
"object:open:",
"member:open:name=hoge:",
"number:42",
"member:close:",
"member:open:name=fuga:",
"array:open:",
"string:foo",
"string:bar",
"array:close:",
"member:close:",
"object:close:",
// clang-format on
};
assert(elements == expected);
}
std::string to_string(const tetengo::json::element& element_)
{
std::string result{};
// Obtains the element type name.
switch (element_.type().name)
{
result = "string:";
break;
result = "number:";
break;
result = "boolean:";
break;
result = "null:";
break;
result = "object:";
break;
result = "member:";
break;
default:
result = "array:";
break;
}
// Obtains the element type category.
switch (element_.type().category)
{
break;
result += "open:";
break;
default:
result += "close:";
break;
}
// Obtains the element attributes.
for (const auto& attribute: element_.attributes())
{
result += attribute.first + "=" + attribute.second + ":";
}
// Obtains the element value.
result += element_.value();
return result;
}
}
An element.
Definition element.hpp:22
const type_type & type() const
Returns the type.
const std::string & value() const
Returns the value.
A JSON parser.
Definition json_parser.hpp:28
const element & peek() const
Returns the current element.
An element.
A JSON parser.
A reader.
A stream reader.
type_name_type name
A name.
Definition element.hpp:50
type_category_type category
A category.
Definition element.hpp:53

C

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int make_json_file(const char* text, const char* path);
const char* to_string(const tetengo_json_element_t* p_element);
void usage_tetengo_json_parsing()
{
// clang-format off
static const char* const json_text =
"{\n"
" \"hoge\": 42,\n"
" \"fuga\": [ \"foo\", \"bar\" ]\n"
"}\n";
// clang-format on
static const char* const json_file_path = "jsonParser_sample.json";
if (!make_json_file(json_text, json_file_path))
{
return;
}
// Creates a reader from a file path.
tetengo_json_reader_t* const p_reader =
// Creates a JSON parser
tetengo_json_jsonParser_t* const p_parser =
// Iteration.
char element_list_string[384] = { 0 };
{
// Obtains the current element.
const tetengo_json_element_t* const p_element = tetengo_json_jsonParser_peek(p_parser);
strcat(element_list_string, to_string(p_element));
// Moves to the next element.
}
// clang-format off
static const char* const expected =
"object:open:\n"
"member:open:name=hoge:\n"
"number:42\n"
"member:close:\n"
"member:open:name=fuga:\n"
"array:open:\n"
"string:foo\n"
"string:bar\n"
"array:close:\n"
"member:close:\n"
"object:close:\n";
// clang-format on
assert(strcmp(element_list_string, expected) == 0);
// Destroys the JSON parser. The reader inside is also destroyed.
remove(json_file_path);
}
int make_json_file(const char* const text, const char* const path)
{
FILE* const stream = fopen(path, "w");
if (!stream)
{
fprintf(stderr, "Can't create a file: %s", path);
return 0;
}
fputs(text, stream);
fclose(stream);
return 1;
}
const char* to_string(const tetengo_json_element_t* const p_element)
{
static char result[32] = { 0 };
result[0] = '\0';
// Obtains the element type name.
const tetengo_json_element_type_t* const p_type = tetengo_json_element_type(p_element);
{
strcat(result, "string:");
}
{
strcat(result, "number:");
}
{
strcat(result, "boolean:");
}
{
strcat(result, "null:");
}
{
strcat(result, "object:");
}
{
strcat(result, "member:");
}
else
{
strcat(result, "array:");
}
// Obtains the element type category.
{
strcat(result, "open:");
}
else
{
strcat(result, "close:");
}
// Obtains the element attributes.
const size_t attribute_count = tetengo_json_element_attributeKeys(p_element, NULL);
if (attribute_count > 0)
{
const char** const p_keys = (const char**)malloc(attribute_count * sizeof(const char*));
if (!p_keys)
{
fprintf(stderr, "Failed to allocate a memory.");
return result;
}
for (size_t i = 0; i < attribute_count; ++i)
{
strcat(result, p_keys[i]);
strcat(result, "=");
strcat(result, tetengo_json_element_attributeValueOf(p_element, p_keys[i]));
strcat(result, ":");
}
free((void*)p_keys);
}
// Obtains the element value.
strcat(result, tetengo_json_element_value(p_element));
strcat(result, "\n");
return result;
}
An element.
int tetengo_json_element_typeName_object()
Returns the type name of object.
int tetengo_json_element_typeName_member()
Returns the type name of member.
const char * tetengo_json_element_value(const tetengo_json_element_t *p_element)
Returns the value.
int tetengo_json_element_typeName_array()
Returns the type name of array.
int tetengo_json_element_typeCategory_primitive()
Returns the type category of primitive.
int tetengo_json_element_typeName_number()
Returns the type name of number.
int tetengo_json_element_typeName_boolean()
Returns the type name of boolean.
size_t tetengo_json_element_attributeKeys(const tetengo_json_element_t *p_element, const char **p_keys)
Returns the attribute keys.
const char * tetengo_json_element_attributeValueOf(const tetengo_json_element_t *p_element, const char *key)
Returns the attribute value.
const tetengo_json_element_type_t * tetengo_json_element_type(const tetengo_json_element_t *p_element)
Returns the type.
int tetengo_json_element_typeCategory_structureOpen()
Returns the type category of opening structure.
int tetengo_json_element_typeName_null()
Returns the type name of null.
int tetengo_json_element_typeCategory_structureClose()
Returns the type category of closing structure.
int tetengo_json_element_typeName_string()
Returns the type name of string.
A JSON parser.
void tetengo_json_jsonParser_destroy(const tetengo_json_jsonParser_t *p_parser)
Destroys a JSON parser.
const tetengo_json_element_t * tetengo_json_jsonParser_peek(const tetengo_json_jsonParser_t *p_parser)
Returns the current element.
bool tetengo_json_jsonParser_hasNext(const tetengo_json_jsonParser_t *p_parser)
Returns true when the next element exists.
tetengo_json_jsonParser_t * tetengo_json_jsonParser_create(tetengo_json_reader_t *p_reader, size_t buffer_capacity)
Creates a JSON parser.
void tetengo_json_jsonParser_next(tetengo_json_jsonParser_t *p_parser)
Moves to the next element.
size_t tetengo_json_jsonParser_defaultBufferCapacity(void)
Returns the default buffer capacity.
A reader.
size_t tetengo_json_reader_streamReaderDefaultBufferCapacity()
Returns the default buffer capacity of the stream reader.
tetengo_json_reader_t * tetengo_json_reader_createStreamReader(const char *file_path, size_t buffer_capacity)
Creates a stream reader.
Definition element.h:94
int category
Definition element.h:99
int name
Definition element.h:96