BSON数据读写C++的API,uma::bson 1.1 发布

jopen 11年前

uma::bson是一个DOM风格的C++ API用于读写BSON数据。不像MongoDB C++ API,它只公开了一个只读接口和一个单独的接口来创建一个BSON展示。这个API能够读写现有的数据。这个API主要设计用于将BSON数据序列化成数据流(文件,socket连接等)或反过来从数据流反序列化成BSON数据。

    #include <uma/bson/Element.h>      #include <uma/bson/Double.h>      #include <uma/bson/String.h>      #include <uma/bson/Document.h>      #include <uma/bson/Array.h>      #include <uma/bson/Undefined.h>      #include <uma/bson/ObjectId.h>      #include <uma/bson/Boolean.h>      #include <uma/bson/Date.h>      #include <uma/bson/Null.h>      #include <uma/bson/RegularExpression.h>      #include <uma/bson/Integer.h>      #include <uma/bson/Timestamp.h>      #include <uma/bson/Long.h>        #include <Poco/FileStream.h>        using uma::bson::Value;      using uma::bson::SharedElement;      using uma::bson::Element;      using uma::bson::Double;      using uma::bson::String;      using uma::bson::Document;      using uma::bson::Array;      using uma::bson::Undefined;      using uma::bson::ObjectId;      using uma::bson::Boolean;      using uma::bson::Date;      using uma::bson::Null;      using uma::bson::RegularExpression;      using uma::bson::Integer;      using uma::bson::Timestamp;      using uma::bson::Long;        void document()      {        Document emptyDoc( 0 ); // without auto generated _id field        emptyDoc.set( "prop1", "Property One" );        emptyDoc.set( "prop2", "Property Two" );          Array array;        array << 1.0;        array << "string value";        array << Undefined();        array << true;        array << date;        array << Null();        array << RegularExpression( "^abc", "$(1)" );        array << 1;        array << static_cast<int64_t>( 2 );          Document doc();        doc.set( "double", 1.0 );        doc.set( "string", "string value" );        doc.set( "childDoc", emptyDoc );        doc.set( "childArray", array );        doc.set( "undefined", Undefined() );        doc.set( "boolean", true );        doc.set( "date", date );        doc.set( "null", Null() );        doc.set( "regex", RegularExpression( "^abc", "$(1)" ) );        doc.set( "integer", 1 );        doc.set( "long", static_cast<int64_t>( 2 ) );          assert( doc["double"]->getType() == Value::Type::Double );        verify( doc );      }        void bson( const Document& doc )      {        const std::string out( "/tmp/doctest.bson" );        Poco::FileOutputStream fos( out, std::ios::out | std::ios::binary | std::ios::trunc );        doc.toBson( fos );        fos.close();          Document d = Document::fromFile( out );        verify( d );      }        void json( const Document& doc )      {        const std::string out( "/tmp/doctest.json" );        Poco::FileOutputStream fos( out, std::ios::out | std::ios::binary | std::ios::trunc );        doc.toJson( fos, true );        fos.close();          Poco::FileInputStream fis( out );        Document d = Document::fromJson( fis );        verify( d );      }        void verify( const Document& doc )      {        assert( doc.get( "double" )->getType() == Value::Type::Double );        assert( doc.get( "double" )->getValue<Double>().getValue() == 1.0 );          assert( doc.get( "string" )->getType() == Value::Type::String );        assert( doc.get( "string" )->getValue<String>().getValue() == "string value" );          assert( doc.get( "undefined" )->getType() == Value::Type::Undefined );          assert( doc.get( "boolean" )->getType() == Value::Type::Boolean );        assert( doc.get( "boolean" )->getValue<Boolean>().getValue() == true );          assert( doc.get( "date" )->getType() == Value::Type::Date );        assert( doc.get( "date" )->getValue<Date>() == date );          assert( doc.get( "null" )->getType() == Value::Type::Null );          assert( doc.get( "regex" )->getType() == Value::Type::RegEx );        assert( doc.get( "regex" )->getValue<RegularExpression>().getRegex() == "^abc" );        assert( doc.get( "regex" )->getValue<RegularExpression>().getFlags() == "$(1)" );          assert( doc.get( "integer" )->getType() == Value::Type::Integer );        assert( doc.get( "integer" )->getValue<Integer>().getValue() == 1 );          assert( doc.get( "long" )->getType() == Value::Type::Long );        assert( doc.get( "long" )->getValue<Long>().getValue() == 2 );      }