GitHub

This is a very simple BSON implementation in Lua. It doesn't support all BSON types (yet). In particular, and ironically, it doesn't support the floating point type.

There are no external dependencies, Lua 5.1 or LuaJIT should work.

bson=require'bson'
epoch=os.time({year=1970, month=1, day=1, hour=0})
bsondoc1=bson.encode{username="maroc",
		     info={first="todd",
			     last="coram",
			     age=46,
			     signature1=bson.binary("\000\001\002\003",bson.B_GENERIC),
			     signature2=bson.binary("\000\001\002\003"),
			     saved=true,
			     now=bson.utc_datetime(),
			     past=bson.utc_datetime(epoch * 1000),
			     colors={"Red","Green","Blue"}
		     }}
decoded=bson.decode(bsondoc1)
print("username = "..decoded.username)
for i,v in pairs(decoded) do print(i,v) end
print()
for i,v in pairs(decoded.info) do print(i,v) end
print()
for i,v in ipairs(decoded.info.colors) do print(i,v) end

Read the original on github.com ↗