Property definitions

mongodb $ BSON :: defaultinit
# Everything inside MongoDB is manipulated as BSON Objects.
#
# See:
# * [Binary JSON spec](http://bsonspec.org/)
# * [Libbson](http://api.mongodb.org/libbson/1.1.4/)#
private class BSON
	super FinalizableOnce

	# Native instance pointer.
	var native: NativeBSON

	# Returns a new BSON object initialized from the content of `json`.
	#
	# ~~~
	# intrude import mongodb
	# var obj = new JsonObject
	# obj["age"] = 10
	# obj["name"] = "Rick"
	# obj["ELS"] = new JsonArray
	# var bson = new BSON.from_json(obj)
	# assert bson.to_s == """{ "age" : 10, "name" : "Rick", "ELS" : [  ] }"""
	# ~~~
	init from_json(json: JsonObject) do
		init(new NativeBSON.from_json_string(json.to_json.to_cstring))
	end

	# Returns a new BSON object parsed from `json_string`.
	#
	# If `json_string` is not a valid JSON string, this initializer returns NULL.
	#
	# ~~~
	# intrude import mongodb
	# var str = """{ "age" : 10, "name" : "Rick", "ELS" : [  ] }"""
	# var bson = new BSON.from_json_string(str)
	# assert bson.to_s == str
	# ~~~
	init from_json_string(json_string: String) do
		init(new NativeBSON.from_json_string(json_string.to_cstring))
	end

	redef fun to_s do
		var ns = native.to_c_string
		var res = ns.to_s
		ns.free # manual free of gc allocated CString
		return res
	end

	# Returns a new JsonObject from `self`.
	#
	# ~~~
	# intrude import mongodb
	# var str = """{ "age" : 10, "name" : "Rick", "ELS" : [  ] }"""
	# var bson = new BSON.from_json_string(str)
	# var json = bson.to_json
	# assert json["age"] == 10
	# assert json["name"] == "Rick"
	# assert json["ELS"].as(JsonArray).is_empty
	# ~~~
	fun to_json: JsonObject do
		var json = to_s.parse_json
		if json isa JsonParseError then
			print json.message
			sys.exit 1
		end
		return json.as(JsonObject)
	end

	redef fun finalize_once do native.destroy
end
lib/mongodb/mongodb.nit:58,1--126,3