MCPcopy Create free account
hub / github.com/0xShug0/audio.cpp / json_parse

Function json_parse

external/ggml/examples/common.cpp:155–244  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

153}
154
155std::map<std::string, int32_t> json_parse(const std::string & fname) {
156 std::map<std::string, int32_t> result;
157
158 // read file into string
159 std::string json;
160 {
161 std::ifstream ifs(fname);
162 if (!ifs) {
163 fprintf(stderr, "Failed to open %s\n", fname.c_str());
164 exit(1);
165 }
166
167 json = std::string((std::istreambuf_iterator<char>(ifs)),
168 (std::istreambuf_iterator<char>()));
169 }
170
171 if (json[0] != '{') {
172 return result;
173 }
174
175 // parse json
176 {
177 bool has_key = false;
178 bool in_token = false;
179
180 std::string str_key = "";
181 std::string str_val = "";
182
183 int n = json.size();
184 for (int i = 1; i < n; ++i) {
185 if (!in_token) {
186 if (json[i] == ' ') continue;
187 if (json[i] == '"') {
188 in_token = true;
189 continue;
190 }
191 } else {
192 if (json[i] == '\\' && i+1 < n) {
193 if (has_key == false) {
194 str_key += json[i];
195 } else {
196 str_val += json[i];
197 }
198 ++i;
199 } else if (json[i] == '"') {
200 if (has_key == false) {
201 has_key = true;
202 ++i;
203 while (json[i] == ' ') ++i;
204 ++i; // :
205 while (json[i] == ' ') ++i;
206 if (json[i] != '\"') {
207 while (json[i] != ',' && json[i] != '}') {
208 str_val += json[i++];
209 }
210 has_key = false;
211 } else {
212 in_token = true;

Callers 1

gpt_vocab_initFunction · 0.85

Calls 3

replaceFunction · 0.85
stringFunction · 0.50
sizeMethod · 0.45

Tested by

no test coverage detected