One way to utilize the extended JSON syntax is as a means of transferring data between different systems. For example, if you are sending JSON output to a remote client, using extended syntax allows for the preservation of important type information such as ObjectId, Date, Binary, and more.
Currently, the implementation of this feature can be found in the C# driver, which includes a useful json parser utility method. This method can take JSON data with extended syntax fields and convert them into objects of the necessary type.
If you want to achieve similar functionality in your own code, you can create a custom parsing routine to identify key values that indicate specific object types. For instance, consider the following fragment:
{ "_id": { "$oid": "51b6eab8cd794eb62bb3e131" } }
Here's a simplified approach to converting these values without delving into deep recursive checks:
data = JSON.parse( json );
for ( k in data ) {
if ( data[k].hasOwnProperty("$oid") )
data[k] = new ObjectId( data[k]["$oid"] );
// additional cases
}
While JavaScript may not inherently recognize the extended syntax, you have the flexibility to post-process parsed JSON data to adhere to the desired object notation required by the language or query interface.
Furthermore, certain drivers perform similar casting operations when dealing with string values assigned to an `_id` field, ensuring alignment with the BSON wire protocol's object type requirements.