C BSON使用

    与普通的 JSON 不同,BSON 提供更多的数据类型,以满足 C/C++ 语言多种多样的需求。SequoiaDB 提供了包括8字节浮点数(DOUBLE),字符串,嵌套对象,嵌套数组,对象 ID(数据库中每个集合中每条记录都有一个唯一 ID),布尔值,日期,NULL,正则表达式,4字节整数(INT),时间戳,以及8字节整数等数据类型。这些类型的定义可以在 bson.h 中的 bson_type 找到。详情请查看 。

    注意:使用 C BSON API 函数构建 BSON 出错时,API 将返回错误码表示构建失败。用户应当适当检测函数返回值。

    总的来说,一个 BSON 对象的创建主要分为三大步操作:

    1)创建对象(bson_create ; bson_init)

    3)清除对象(bson_dispose(与bson_create配对使用) ; bson_destroy(与bson_init配对使用))

    • 创建一个简单的 BSON 对象{age:20}。
    • 创建一个复杂的 BSON 对象
    1. /* 创建一个包含{name:"tom",colors:["red","blue","green"], address: {city:"Toronto", province: "Ontario"}}的对象 */
    2. bson *newobj = bson_create ();
    3. bson_append_string ( newobj, "name", "tom" );
    4. bson_append_start_array( newobj, "colors" );
    5. bson_append_string( newobj, "0", "red" );
    6. bson_append_string( newobj, "1", "blue" );
    7. bson_append_string( newobj, "2", "green" );
    8. bson_append_finish_array( newobj );
    9. bson_append_start_object ( newobj, "address" );
    10. bson_append_string ( newobj, "city", "Toronto" );
    11. bson_append_string ( newobj, "province", "Ontario" );
    12. bson_append_finish_object ( newobj );
    13. if( BSON_OK != bson_finish ( newobj ) )
    14. {
    15. printf( "Error." );
    16. }
    17. else
    18. {
    19. bson_print( newobj );
    20. }
    21. // never use "bson_destroy" here,
    22. // for "bson_dispose" is used with
    23. // "bson_create"
    24. bson_dispose( newobj );

    读取对象

    • 可以使用 bson_print 方法来打印 BSON 内容。也可以使用 bson_iterator 来遍历 BSON 的所有字段内容。要遍历 BSON,首先要初始化 bson_iterator,然后使用 bson_iterator_next 遍历 BSON 每一个元素。
    1. bson newobj;
    2. bson_iterator i;
    3. bson_type type;
    4.  
    5. // build a bson
    6. bson_init( &newobj );
    7. bson_append_string( &newobj, "a", "hello" );
    8. bson_finish( &newobj );
    9.  
    10. // init bson iterator
    11. bson_iterator_init( &i, &newobj );
    12.  
    13. // get the type of the value
    14. type = bson_iterator_type( &i );
    15.  
    16. // display the value
    17. if ( BSON_STRING == type )
    18. {
    19. printf( "Value: %s\n", bson_iterator_string( &i ) );
    20. }
    21.  
    22. // release resource
    23. bson_destroy( &newobj );
    • 遍历每个连续的 BSON 对象元素,可以使用 bson_find 函数直接跳转得到元素的名称。如果该元素不存在于 bson 之内,则 bson_find 函数返回 BSON_EOO。

    例如想得到 name 元素名可以这样使用:

    • 读取数组元素或嵌套对象,因为“address”是一个嵌套对象,需要特殊遍历。首先得到 address 值,再初始化一个新的 BSON 迭代器:
    1. bson newobj;
    2. bson_iterator i;
    3. bson_iterator sub;
    4. bson_type type;
    5. const CHAR *value = NULL;
    6.  
    7. // build a bson
    8. bson_init( &newobj );
    9. bson_append_start_object( &newobj, "address" );
    10. bson_append_string( &newobj, "Home", "guangzhou" );
    11. bson_append_string( &newobj, "WorkPlace", "shenzhen" );
    12. bson_append_finish_object( &newobj );
    13. bson_finish( &newobj );
    14.  
    15. // init bson iterator and display contents in the sub object
    16. type = bson_find( &i, &newobj, "address" );
    17. if ( BSON_EOO != type )
    18. {
    19. bson_iterator_subiterator( &i, &sub );
    20. while ( bson_iterator_more( &sub ) )
    21. {
    22. type = bson_iterator_next( &sub );
    23. key = bson_iterator_key( &sub );
    24. value = bson_iterator_string( &sub ) ;
    25. if ( BSON_STRING == type )
    26. {
    27. printf( "Type: %d, Key: %s, value: %s\n", type, key, value );
    28. }
    29. }
    30. }
    31.  
    32. // release resource
    33. bson_destroy( &newobj );