The ASP. 2.1 built-in support for XML,Json,Bson, the MiME type of form-urlencoded, which is highlighted today under Bson. BSON is a data format developed by 10gen, which is currently used primarily in MongoDB and is a data storage format for MongoDB. Bson based on JSON format, the main reason to choose JSON for transformation is the universality of JSON and the schemaless characteristics of JSON.
Bson mainly achieves the following three-point goals:
1. Faster traversal speed
In JSON format, too large a JSON structure can cause data traversal to be very slow. In JSON, to skip a document for data reading, this document needs to be scanned, and a cumbersome data structure should be matched, such as the matching of parentheses, and bson a big improvement to JSON is that it will have the length of each element of the JSON element in the head, This allows you to read directly to the specified point by reading the element length.
2. Easy to operate
For JSON, data storage is untyped, for example, if you want to modify a basic value, from 9 to 10, since a character becomes two, everything behind it may need to be moved backwards. Using Bson, you can specify this column as a number, then whether the number from 9 to 10 or 100, we are only stored in the number of the one on the change, does not cause the total length of the data to become larger. Of course, in MongoDB, if the number is increased from shaping to a long integer, it will cause the total length of the data to grow larger.
3. Added additional data types
JSON is a convenient format for data interchange, but its type is relatively limited. The Bson adds a "byte array" data type based on it. This makes binary storage no longer required to be Base64 converted before being stored as JSON. Greatly reduces the computational overhead and data size. Of course, in some cases, Bson has no spatial advantage over JSON, such as {"Field": 7}, 7 using only one byte on the storage of JSON, and if using Bson, it is at least 4 bytes (32 bits)
?
enabled on the service side BSON??
public static Class Webapiconfig
{
public static void Register (Httpconfiguration config)
{
Config. Formatters.add (New Bsonmediatypeformatter ());
?
Other Web API configuration is not shown ...
}
}
if Client the request is "Application/bson" , Webapi will use BSON Serializer. ? You can add other Media Type also use BSON the line is serialized as follows: ??
Static async Task RunAsync ()
{
using (HttpClient client = new HttpClient ())
{
Client. baseaddress = new Uri ("http://localhost");
?
Set the Accept header for BSON.
Client. DefaultRequestHeaders.Accept.Clear ();
Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (New Mediatypewithqualityheadervalue ("Application/bson"));
?
Send GET request.
result = await client. Getasync ("API/BOOKS/1");
Result. Ensuresuccessstatuscode ();
?
Use BSON formatter to deserialize the result.
mediatypeformatter[] formatters = new mediatypeformatter[] {
New Bsonmediatypeformatter ()
};
?
var book = await result. Content.readasasync<book> (formatters);
}
}
Types of serialized Primitives
? ? The BSON document is a collection of key/value ,and theBSON specification does not define a syntax that returns only a native value, such as returning a int or string the value of the type.
in order to solve this problem Bsonmediatypeformatter special treatment of the native type, before serialization, to convert it to Key/value form, Key is a "Value". as follows:
public class Valuescontroller:apicontroller
{
Public Ihttpactionresult Get ()
{
Return Ok (42);
}
}
return value:
{"Value": 42}
?
When deserializing , the serializer shifts the data to the original value. Of course , if a different BSON serializer is used, theBSON parser needs to handle this if the server returns such a type of data . ?
?
Https://visualstudiomagazine.com/articles/2014/05/01/implementing-binary-json-in-aspnet-web-api-2_1.aspx
ASP. NET Web API 2.1 supports binary JSON (Bson)