const char * StoreProcessIncomingContact(StoreClient *client, StoreObject *contact, char *path) { UNUSED_PARAMETER_REFACTOR(path); BongoJsonNode *node = NULL; char *name = NULL; char *email = NULL; if (GetJson(client, contact, &node, path) != BONGO_JSON_OK) goto parse_error; if (node->type != BONGO_JSON_OBJECT) goto parse_error; BongoJsonResult result; result = BongoJsonJPathGetString(node, "o:fullName/s", &name); if (result != BONGO_JSON_OK) name = NULL; // FIXME: want to find the preferred address really. result = BongoJsonJPathGetString(node, "o:email/a:0/o:address/s", &email); if (result != BONGO_JSON_OK) email = NULL; if (name) SetDocProp(client, contact, "bongo.contact.name", name); if (email) SetDocProp(client, contact, "bongo.contact.email", email); return NULL; parse_error: if (node) BongoJsonNodeFree(node); return MSG4226BADCONTACT; }
void LuaContext::GetJson(Json::Value& result, int top, bool keepStrings) { if (lua_istable(lua_, top)) { Json::Value tmp = Json::objectValue; bool isArray = true; size_t size = 0; // Code adapted from: http://stackoverflow.com/a/6142700/881731 // Push another reference to the table on top of the stack (so we know // where it is, and this function can work for negative, positive and // pseudo indices lua_pushvalue(lua_, top); // stack now contains: -1 => table lua_pushnil(lua_); // stack now contains: -1 => nil; -2 => table while (lua_next(lua_, -2)) { // stack now contains: -1 => value; -2 => key; -3 => table // copy the key so that lua_tostring does not modify the original lua_pushvalue(lua_, -2); // stack now contains: -1 => key; -2 => value; -3 => key; -4 => table std::string key(lua_tostring(lua_, -1)); Json::Value v; GetJson(v, -2, keepStrings); tmp[key] = v; size += 1; try { if (!OnlyContainsDigits(key) || boost::lexical_cast<size_t>(key) != size) { isArray = false; } } catch (boost::bad_lexical_cast&) { isArray = false; } // pop value + copy of key, leaving original key lua_pop(lua_, 2); // stack now contains: -1 => key; -2 => table } // stack now contains: -1 => table (when lua_next returns 0 it pops the key // but does not push anything.) // Pop table lua_pop(lua_, 1); // Stack is now the same as it was on entry to this function if (isArray) { result = Json::arrayValue; for (size_t i = 0; i < size; i++) { result.append(tmp[boost::lexical_cast<std::string>(i + 1)]); } } else { result = tmp; } } else if (lua_isnil(lua_, top)) { result = Json::nullValue; } else if (!keepStrings && lua_isboolean(lua_, top)) { result = lua_toboolean(lua_, top) ? true : false; } else if (!keepStrings && lua_isnumber(lua_, top)) { // Convert to "int" if truncation does not loose precision double value = static_cast<double>(lua_tonumber(lua_, top)); int truncated = static_cast<int>(value); if (std::abs(value - static_cast<double>(truncated)) <= std::numeric_limits<double>::epsilon()) { result = truncated; } else { result = value; } } else if (lua_isstring(lua_, top)) { // Caution: The "lua_isstring()" case must be the last, since // Lua can convert most types to strings by default. result = std::string(lua_tostring(lua_, top)); } else { LOG(WARNING) << "Unsupported Lua type when returning Json"; result = Json::nullValue; } }
bool FileContent::Save() { string json = GetJson(); string key = "filecontent_" + identifier; RocksdbHandler::Insert(key, json, PATH_DB_FILES); return true; }
bool User::Save() { string json = GetJson(false); RocksdbHandler::Insert("user_" + GetIdentifier(), json, PATH_DB_USERS); return true; }