|
| 1 | +/*! \file |
| 2 | + \brief The usage of JSON parsing. |
| 3 | +
|
| 4 | + Copyright (C) 2019-2021 kaoru https://www.tetengo.org/ |
| 5 | +*/ |
| 6 | + |
| 7 | +#include "usage_tetengo.json.parsing_c.h" |
| 8 | + |
| 9 | +/* [parsing] */ |
| 10 | +#include <assert.h> |
| 11 | +#include <stdio.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | +#include <tetengo/json/element.h> |
| 16 | +#include <tetengo/json/jsonParser.h> |
| 17 | +#include <tetengo/json/reader.h> |
| 18 | + |
| 19 | + |
| 20 | +int make_json_file(const char* text, const char* path); |
| 21 | +const char* to_string(const tetengo_json_element_t* p_element); |
| 22 | + |
| 23 | +void usage_tetengo_json_parsing() |
| 24 | +{ |
| 25 | + /* clang-format off */ |
| 26 | + static const char* const json_text = |
| 27 | + "{\n" |
| 28 | + " \"hoge\": 42,\n" |
| 29 | + " \"fuga\": [ \"foo\", \"bar\" ]\n" |
| 30 | + "}\n"; |
| 31 | + /* clang-format on */ |
| 32 | + static const char* const json_file_path = "jsonParser_sample.json"; |
| 33 | + if (!make_json_file(json_text, json_file_path)) |
| 34 | + { |
| 35 | + return; |
| 36 | + } |
| 37 | + { |
| 38 | + /* Creates a reader from a file path. */ |
| 39 | + tetengo_json_reader_t* const p_reader = tetengo_json_reader_createStreamReader( |
| 40 | + json_file_path, tetengo_json_reader_streamReaderDefaultBufferCapacity()); |
| 41 | + |
| 42 | + /* Creates a JSON parser */ |
| 43 | + tetengo_json_jsonParser_t* const p_parser = |
| 44 | + tetengo_json_jsonParser_create(p_reader, tetengo_json_jsonParser_defaultBufferCapacity()); |
| 45 | + |
| 46 | + /* Iteration. */ |
| 47 | + char element_list_string[384] = { 0 }; |
| 48 | + while (tetengo_json_jsonParser_hasNext(p_parser)) |
| 49 | + { |
| 50 | + /* Obtains the current element. */ |
| 51 | + const tetengo_json_element_t* const p_element = tetengo_json_jsonParser_peek(p_parser); |
| 52 | + |
| 53 | + strcat(element_list_string, to_string(p_element)); |
| 54 | + |
| 55 | + /* Moves to the next element. */ |
| 56 | + tetengo_json_jsonParser_next(p_parser); |
| 57 | + } |
| 58 | + |
| 59 | + { |
| 60 | + /* clang-format off */ |
| 61 | + static const char* const expected = |
| 62 | + "object:open:\n" |
| 63 | + "member:open:name=hoge:\n" |
| 64 | + "number:42\n" |
| 65 | + "member:close:\n" |
| 66 | + "member:open:name=fuga:\n" |
| 67 | + "array:open:\n" |
| 68 | + "string:foo\n" |
| 69 | + "string:bar\n" |
| 70 | + "array:close:\n" |
| 71 | + "member:close:\n" |
| 72 | + "object:close:\n"; |
| 73 | + /* clang-format on */ |
| 74 | + assert(strcmp(element_list_string, expected) == 0); |
| 75 | + } |
| 76 | + |
| 77 | + /* Destroys the JSON parser. The reader inside is also destroyed. */ |
| 78 | + tetengo_json_jsonParser_destroy(p_parser); |
| 79 | + } |
| 80 | + remove(json_file_path); |
| 81 | +} |
| 82 | + |
| 83 | +int make_json_file(const char* const text, const char* const path) |
| 84 | +{ |
| 85 | + FILE* const stream = fopen(path, "w"); |
| 86 | + if (!stream) |
| 87 | + { |
| 88 | + fprintf(stderr, "Can't create a file: %s", path); |
| 89 | + return 0; |
| 90 | + } |
| 91 | + fputs(text, stream); |
| 92 | + fclose(stream); |
| 93 | + return 1; |
| 94 | +} |
| 95 | + |
| 96 | +const char* to_string(const tetengo_json_element_t* const p_element) |
| 97 | +{ |
| 98 | + static char result[32] = { 0 }; |
| 99 | + result[0] = '\0'; |
| 100 | + { |
| 101 | + /* Obtains the element type name. */ |
| 102 | + const tetengo_json_element_type_t* const p_type = tetengo_json_element_type(p_element); |
| 103 | + if (p_type->name == tetengo_json_element_typeName_string()) |
| 104 | + { |
| 105 | + strcat(result, "string:"); |
| 106 | + } |
| 107 | + else if (p_type->name == tetengo_json_element_typeName_number()) |
| 108 | + { |
| 109 | + strcat(result, "number:"); |
| 110 | + } |
| 111 | + else if (p_type->name == tetengo_json_element_typeName_boolean()) |
| 112 | + { |
| 113 | + strcat(result, "boolean:"); |
| 114 | + } |
| 115 | + else if (p_type->name == tetengo_json_element_typeName_null()) |
| 116 | + { |
| 117 | + strcat(result, "null:"); |
| 118 | + } |
| 119 | + else if (p_type->name == tetengo_json_element_typeName_object()) |
| 120 | + { |
| 121 | + strcat(result, "object:"); |
| 122 | + } |
| 123 | + else if (p_type->name == tetengo_json_element_typeName_member()) |
| 124 | + { |
| 125 | + strcat(result, "member:"); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + assert(p_type->name == tetengo_json_element_typeName_array()); |
| 130 | + strcat(result, "array:"); |
| 131 | + } |
| 132 | + |
| 133 | + /* Obtains the element type category. */ |
| 134 | + if (p_type->category == tetengo_json_element_typeCategory_primitive()) |
| 135 | + { |
| 136 | + } |
| 137 | + else if (p_type->category == tetengo_json_element_typeCategory_structureOpen()) |
| 138 | + { |
| 139 | + strcat(result, "open:"); |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + assert(p_type->category == tetengo_json_element_typeCategory_structureClose()); |
| 144 | + strcat(result, "close:"); |
| 145 | + } |
| 146 | + |
| 147 | + { |
| 148 | + /* Obtains the element attributes. */ |
| 149 | + const size_t attribute_count = tetengo_json_element_attributeKeys(p_element, NULL); |
| 150 | + if (attribute_count > 0) |
| 151 | + { |
| 152 | + const char** const p_keys = (const char**)malloc(attribute_count * sizeof(const char*)); |
| 153 | + if (!p_keys) |
| 154 | + { |
| 155 | + fprintf(stderr, "Failed to allocate a memory."); |
| 156 | + return result; |
| 157 | + } |
| 158 | + tetengo_json_element_attributeKeys(p_element, p_keys); |
| 159 | + { |
| 160 | + size_t i = 0; |
| 161 | + for (i = 0; i < attribute_count; ++i) |
| 162 | + { |
| 163 | + strcat(result, p_keys[i]); |
| 164 | + strcat(result, "="); |
| 165 | + strcat(result, tetengo_json_element_attributeValueOf(p_element, p_keys[i])); |
| 166 | + strcat(result, ":"); |
| 167 | + } |
| 168 | + } |
| 169 | + free((void*)p_keys); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + /* Obtains the element value. */ |
| 174 | + strcat(result, tetengo_json_element_value(p_element)); |
| 175 | + } |
| 176 | + strcat(result, "\n"); |
| 177 | + return result; |
| 178 | +} |
| 179 | +/* [parsing] */ |
0 commit comments