CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Format/JSONReader.cpp
Views: 1401
#include "Common/Data/Format/JSONReader.h"1#include "Common/File/VFS/VFS.h"2#include "Common/File/Path.h"3#include "Common/File/FileUtil.h"4#include "Common/Log.h"56namespace json {78JsonReader::JsonReader(const std::string &filename) {9size_t buf_size;10buffer_ = (char *)g_VFS.ReadFile(filename.c_str(), &buf_size);11if (buffer_) {12parse();13} else {14// Okay, try to read on the local file system15buffer_ = (char *)File::ReadLocalFile(Path(filename), &buf_size);16if (buffer_) {17parse();18} else {19ERROR_LOG(Log::IO, "Failed to read json file '%s'", filename.c_str());20}21}22}2324bool JsonReader::parse() {25char *error_pos;26int status = jsonParse(buffer_, &error_pos, &root_, alloc_);27if (status != JSON_OK) {28ERROR_LOG(Log::IO, "Error at (%i): %s\n%s\n\n", (int)(error_pos - buffer_), jsonStrError(status), error_pos);29return false;30}31ok_ = true;32return true;33}3435int JsonGet::numChildren() const {36int count = 0;37if (value_.getTag() == JSON_OBJECT || value_.getTag() == JSON_ARRAY) {38for (auto it : value_) {39(void)it;40count++;41}42}43return count;44}4546const JsonNode *JsonGet::get(const char *child_name) const {47if (!child_name) {48ERROR_LOG(Log::IO, "JSON: Cannot get from null child name");49return nullptr;50}51if (value_.getTag() != JSON_OBJECT) {52return nullptr;53}54for (auto it : value_) {55if (!strcmp(it->key, child_name)) {56return it;57}58}59return nullptr;60}6162const JsonNode *JsonGet::get(const char *child_name, JsonTag type) const {63const JsonNode *v = get(child_name);64if (v && type == v->value.getTag())65return v;66return nullptr;67}6869const char *JsonGet::getStringOrNull(const char *child_name) const {70const JsonNode *val = get(child_name, JSON_STRING);71if (val)72return val->value.toString();73ERROR_LOG(Log::IO, "String '%s' missing from node", child_name);74return nullptr;75}7677bool JsonGet::getString(const char *child_name, std::string *output) const {78const JsonNode *val = get(child_name, JSON_STRING);79if (!val) {80return false;81}82*output = val->value.toString();83return true;84}8586const char *JsonGet::getStringOr(const char *child_name, const char *default_value) const {87const JsonNode *val = get(child_name, JSON_STRING);88if (!val)89return default_value;90return val->value.toString();91}9293bool JsonGet::getStringVector(std::vector<std::string> *vec) const {94vec->clear();95if (value_.getTag() == JSON_ARRAY) {96for (auto it : value_) {97if (it->value.getTag() == JSON_STRING) {98vec->push_back(it->value.toString());99}100}101return true;102} else {103return false;104}105}106107double JsonGet::getFloat(const char *child_name) const {108return get(child_name, JSON_NUMBER)->value.toNumber();109}110111double JsonGet::getFloat(const char *child_name, double default_value) const {112const JsonNode *val = get(child_name, JSON_NUMBER);113if (!val)114return default_value;115return val->value.toNumber();116}117118int JsonGet::getInt(const char *child_name) const {119return (int)get(child_name, JSON_NUMBER)->value.toNumber();120}121122int JsonGet::getInt(const char *child_name, int default_value) const {123const JsonNode *val = get(child_name, JSON_NUMBER);124if (!val)125return default_value;126return (int)val->value.toNumber();127}128129bool JsonGet::getBool(const char *child_name) const {130return get(child_name)->value.getTag() == JSON_TRUE;131}132133bool JsonGet::getBool(const char *child_name, bool default_value) const {134const JsonNode *val = get(child_name);135if (val) {136JsonTag tag = val->value.getTag();137if (tag == JSON_TRUE)138return true;139if (tag == JSON_FALSE)140return false;141}142return default_value;143}144145} // namespace json146147148