Sans Pareil Technologies, Inc.

Key To Your Business

BSON Static Library



We needed to use BSON for data interchange in an application. To that end, we created a Cocoa Touch Static Library project with the relevant source files from the MongoDB source distribution (version 1.8.1). We needed to make some modifications to some of the sources due to the way in which the MongoDB developers structured the code. Ideally, the entire BSON library should have been contained in the bson namespace (instead of mixing with mongo namespace), and distributed as a separate source tree. However, that is not how the MongoDB developers chose to organise their code, and we had to make some modifications to get the sources to compile and work.

The static library project along with a simple unit test suite and a test application (used to deploy to iPhone 4/4.2 and test) may be downloaded and used under the same Apache Licence as used for the original BSON source distribution from MongoDB. The static library references a local boost header and library path, which will need to be modified to match your local settings. We built boost for iOS using the boost.sh script, which is to be placed at the root of the unarchived boost sources.

Modifications

We do not remember all the modifications that we needed to make, but the following are the major changes that we had to make:

  • Updated include directive syntax to be based on header include path configuration. For instance, replace #include “../bson/oid.h” with #include <bson/oid.h> syntax.
  • bson/util/atomic_int.h did not have an implementation for ARM CPU. We replaced the assertion error with an implementation based on boost::interprocess::detail::atomic_add32
  • Implementation of some of the methods in bsonobj.h was in jsobj.cpp file in the original sources. We have not included jsobj.h (seemed to have more dependencies into other MongoDB sources), so we created a bsonobj.cpp file with the implementation methods copied over from jsobj.cpp.

Test Application


We have included a simple test application that mimics the action of the unit test class and displays the JSON string representation of the serialised BSON object in a UITextView. The static library project has been added to the frameworks group, and also been included as a pre-requisite for the application project. The project settings should serve to illustrate the header path configuration to successfully use the static library.

Xcode4 Note

To ensure that the application links properly for the device, ensure the following settings in both the static library and the application:

  • Symbols Hidden By Default = YES
  • Inline Methods Hidden = NO

Without these settings, you will get a bad codegen, pointer diff error.

Device Note

Using BSONElement.Date() method on the device (iPad/iPhone/iPad Touch) leads to a strange crash. Use BSONElement.date() method instead.