/
RedResistance
/
JSonMall
Обзор
Документация
Войти
/
RedResistance
/
JSonMall
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
json/reader.cpp
124 строки
2 KB
Domovoi-Kuzma
templates
27 июл 2025, 01:22
27 июл 2025, 01:22
1c9b251
Код
Авторство
О чём код?
#include <cassert> #include "reader.h" #include "any_factory.h" using namespace json; reader::reader(std::ifstream& in, base_factory& fact):factory(fact), fin(in) { parse_field(); } void reader::parse_field() { int ch = nextchar(); fin.unget(); if (ch == '{') { parse_object(); } else if (ch == '[') { parse_array(); } else if (ch >= '0' && ch <= '9') { factory.onInt(parse_int()); } else if (is_letter(ch)) { factory.onString(parse_string()); } else { throw std::string("unexpected character3 ")+char(ch); } } bool reader::is_letter(char ch) { return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_'; } void reader::skip_spaces() { while (fin) { int ch = fin.get();//-1 for eof if (!isspace(ch)) { fin.unget(); break; } } } int reader::parse_int() { int tmp; fin >> tmp; return tmp; } std::string reader::parse_string() { std::string tmp; while (fin) { char ch = fin.get();//-1 for eof if (!is_letter(ch)) { fin.unget(); break; } tmp += ch; } return tmp; } void reader::parse_array() { char ch = nextchar(); assert(ch == '['); ch = nextchar(); if (ch == ']') { return; } fin.unget(); while (fin) { base_factory* element_factory = factory.onArrayItem(); json::reader(fin, *element_factory); ch = nextchar(); if (ch == ']') { return; } if (ch != ',') throw std::string("unexpected character2 ") + ch;//hit skip_spaces(); } } void reader::parse_object() { char ch = nextchar(); assert(ch == '{'); ch = nextchar(); if (ch == '}') { return; } fin.unget(); while (fin) { std::string key = parse_string(); ch = nextchar(); if (ch != ':') throw std::string("unexpected character1 ") + ch; base_factory *field_factory = factory.onField(key), *deleted_factory = nullptr; if (field_factory == nullptr) { deleted_factory = field_factory = new any_factory; } json::reader(fin, *field_factory); delete deleted_factory; ch = nextchar(); if (ch == '}') { return; } if (ch != ',') throw std::string("unexpected character2 ") + ch;//hit skip_spaces(); } throw "unexpected end of object"; } int reader::nextchar() { while (fin) { int ch = fin.get();//-1 for eof if (!isspace(ch)) { return ch; } } return -1; }