A JSON library.
#include <cassert>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
namespace usage_tetengo::json
{
void parsing()
{
static const std::string json_text{
"{\n"
" \"hoge\": 42,\n"
" \"fuga\": [ \"foo\", \"bar\" ]\n"
"}\n"
};
auto p_json_stream = std::make_unique<std::istringstream>(json_text);
auto p_reader = std::make_unique<tetengo::json::stream_reader>(std::move(p_json_stream));
std::vector<std::string> elements{};
while (parser.has_next())
{
const auto& element = parser.peek();
elements.push_back(to_string(element));
parser.next();
}
static const std::vector<std::string> expected{
"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:",
};
assert(elements == expected);
}
{
std::string result{};
{
result = "string:";
break;
result = "number:";
break;
result = "boolean:";
break;
result = "null:";
break;
result = "object:";
break;
result = "member:";
break;
default:
result = "array:";
break;
}
{
break;
result += "open:";
break;
default:
result += "close:";
break;
}
for (
const auto& attribute: element_.
attributes())
{
result += attribute.first + "=" + attribute.second + ":";
}
result += element_.
value();
return result;
}
}
#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()
{
static const char* const json_text =
"{\n"
" \"hoge\": 42,\n"
" \"fuga\": [ \"foo\", \"bar\" ]\n"
"}\n";
static const char* const json_file_path = "jsonParser_sample.json";
if (!make_json_file(json_text, json_file_path))
{
return;
}
tetengo_json_reader_t* const p_reader =
tetengo_json_jsonParser_t* const p_parser =
char element_list_string[384] = { 0 };
{
strcat(element_list_string, to_string(p_element));
}
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";
assert(strcmp(element_list_string, expected) == 0);
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';
{
strcat(result, "string:");
}
{
strcat(result, "number:");
}
{
strcat(result, "boolean:");
}
{
strcat(result, "null:");
}
{
strcat(result, "object:");
}
{
strcat(result, "member:");
}
else
{
strcat(result, "array:");
}
{}
{
strcat(result, "open:");
}
else
{
strcat(result, "close:");
}
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, ":");
}
free((void*)p_keys);
}
strcat(result, "\n");
return result;
}