JSONObject::JSONObject(JSONObject &object) : JSONAtom(JSON_TYPE_OBJECT) { for (atomMapIter i = object.children.begin(); i != object.children.end(); i++) { char *key = strNewDup(i->first); JSONAtom *value; switch (i->second->getType()) { case JSON_TYPE_NULL: value = new JSONNull(); break; case JSON_TYPE_BOOL: value = new JSONBool(*((JSONBool *)i->second)); break; case JSON_TYPE_INT: value = new JSONInt(*((JSONInt *)i->second)); break; case JSON_TYPE_FLOAT: value = new JSONFloat(*((JSONFloat *)i->second)); break; case JSON_TYPE_STRING: value = new JSONString(strNewDup(*((JSONString *)i->second))); break; case JSON_TYPE_OBJECT: value = new JSONObject(*((JSONObject *)i->second)); break; case JSON_TYPE_ARRAY: value = new JSONArray(*((JSONArray *)i->second)); break; default: throw JSONObjectError(JSON_OBJECT_BAD_KEY); } children[key] = value; } }
JSONAtom &object_t::operator [](const char *const key) const { const auto &node = children.find(key); if (node == children.end()) throw JSONObjectError(JSON_OBJECT_BAD_KEY); return *node->second; }
JSONAtom &JSONObject::operator [](const char *const key) const { atomMapConstIter node = children.find((char *)key); if (node == children.end()) throw JSONObjectError(JSON_OBJECT_BAD_KEY); return *node->second; }
void object_t::clone(const object_t &object) { for (const auto &atom : object) { add(atom.first.get(), [](const JSONAtom &value) -> std::unique_ptr<JSONAtom> { switch (value.getType()) { case JSON_TYPE_NULL: return makeUnique<JSONNull>(); case JSON_TYPE_BOOL: return makeUnique<JSONBool>(value); case JSON_TYPE_INT: return makeUnique<JSONInt>(value); case JSON_TYPE_FLOAT: return makeUnique<JSONFloat>(value); case JSON_TYPE_STRING: return makeUnique<JSONString>(strNewDup(value)); case JSON_TYPE_OBJECT: return makeUnique<JSONObject>(value); case JSON_TYPE_ARRAY: return makeUnique<JSONArray>(value); default: throw JSONObjectError(JSON_OBJECT_BAD_KEY); } }(*atom.second)); } }