Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle integer values a little smarter

git-svn-id: http://luamongo.googlecode.com/svn/trunk@50 17e306f6-9439-11de-a13a-71b910e68cc8
  • Loading branch information...
commit 0900cdeef6fbf296a0a05f5e638642e90e32f3d9 1 parent 6c50993
nrich@ii.net authored
Showing with 16 additions and 4 deletions.
  1. +16 −4 utils.cpp
View
20 utils.cpp
@@ -58,7 +58,7 @@ void lua_push_value(lua_State *L, const BSONElement &elem) {
lua_pushnil(L);
break;
case mongo::NumberInt:
- lua_pushinteger(L, elem.number());
+ lua_pushinteger(L, elem.numberInt());
break;
case mongo::NumberDouble:
lua_pushnumber(L, elem.number());
@@ -186,7 +186,18 @@ static void lua_append_bson(lua_State *L, const char *key, int stackpos, BSONObj
} else if (type == LUA_TNIL) {
builder->appendNull(key);
} else if (type == LUA_TNUMBER) {
- builder->append(key, lua_tonumber(L, stackpos));
+ int intval = lua_tointeger(L, stackpos);
+ double numval = lua_tonumber(L, stackpos);
+
+ if (numval == floor(numval)) {
+ /*
+ * The numeric value looks like an integer, treat it as such.
+ * This is closer to how JSON datatypes behave.
+ */
+ builder->append(key, static_cast<int32_t>(intval));
+ } else {
+ builder->append(key, numval);
+ }
} else if (type == LUA_TBOOLEAN) {
builder->appendBool(key, lua_toboolean(L, stackpos));
} else if (type == LUA_TSTRING) {
@@ -209,8 +220,9 @@ void lua_to_bson(lua_State *L, int stackpos, BSONObj &obj) {
lua_pushnil(L);
while (lua_next(L, stackpos) != 0) {
- const char *k = lua_tostring(L, -2);
- lua_append_bson(L, k, -1, &builder);
+ const char *k = lua_tostring(L, -2);
+ lua_append_bson(L, k, -1, &builder);
+
lua_pop(L, 1);
}
Please sign in to comment.
Something went wrong with that request. Please try again.