JSONB 以二进制格式存储数据,而不是简单的 JSON blob。
BSON record isn’t continuously littler than JSON, but it allows you to effectively skip the records that you’re not fascinated when perusing it, whereas with JSON you’d have to be parsed each byte
BSON powerful indexing and querying features
Traversable, lightweight, fast
+ Field name, field type, document size
Query
Indexes & Skip useless content:
MongoDB uses BSON to offer the industry’s most powerful indexing and querying features on top of the web’s most usable data format.
query and manipulate objects by specific keys inside the JSON/BSON document, even in nested documents many layers deep into a record, and create high performance indexes on those same keys and values.
4. Plaintext JSON is to some degree wasteful for bulk capacity and information transmission, so BSON arrangement can be utilized after you got to send a part of JSON information at tall speed – and don’t need to bargain with compression, which can be expensive. Converting BSON to JSON and vice-versa is large much speedier than compressing JSON employing a general-purpose compression calculation
5. Json:
Transmission:
Web API, Ajax
Through all the content
Diff |
JSON |
BSON |
Note |
Encoding Decoding |
UTF-8 String |
Binary |
|
Readability |
Human and Machine |
Machine Only |
|
Data Support |
String, Boolean, Number, Array |
+ Number (Integer, Float, Long, Decimal128...), + Date, Raw Binary |
Extra data type |
Storage |
Object( key-value) , array(Ordered List) |
+ string length, properties types, extra data type |
Additional info |
Space |
Less |
More |
By extra info |
Speed |
Less fast |
Faster |
quick search by extra info |
Usage |
Transmission |
Storage |
Web API, ajax call … Traversal: Index, Query |
Storage is limited & much encoding and decoding
Vs
Lots of queries & less wait time
BSON record isn’t continuously littler than JSON, but it allows you to effectively skip the records that you’re not fascinated when perusing it, whereas with JSON you’d have to be parsed each byte
BSON powerful indexing and querying features
Traversable, lightweight, fast
+ Field name, field type, document size
Query
Indexes & Skip useless content:
MongoDB uses BSON to offer the industry’s most powerful indexing and querying features on top of the web’s most usable data format.
query and manipulate objects by specific keys inside the JSON/BSON document, even in nested documents many layers deep into a record, and create high performance indexes on those same keys and values.
4. Plaintext JSON is to some degree wasteful for bulk capacity and information transmission, so BSON arrangement can be utilized after you got to send a part of JSON information at tall speed – and don’t need to bargain with compression, which can be expensive. Converting BSON to JSON and vice-versa is large much speedier than compressing JSON employing a general-purpose compression calculation
5. Json:
Transmission:
Web API, Ajax
Through all the content
转发
Compare JSON and BSON
I am comparing JSON and BSON for serializing objects. These objects contain several arrays of a large number of integers. In my test the object I am serializing contains a total number of about 12,000 integers. I am only interested in how the sizes compare of the serialized results. I am using JSON.NET as the library which does the serialization. I am using JSON because I also want to be able to work with it in Javascript.
The size of the JSON string is about 43kb and the size of the BSON result is 161kb. So a difference factor of about 4. This is not what I expected because I looked at BSON because I thought BSON is more efficient in storing data.
So my question is why is BSON not efficient, can it be made more efficient? Or is there another way of serializing data with arrays containing large number of integers, which can be easily handled in Javascript?
Below you find the code to test the JSON/BSON serialization.
// Read file which contain json string
string _jsonString = ReadFile();
object _object = Newtonsoft.Json.JsonConvert.DeserializeObject(_jsonString);
FileStream _fs = File.OpenWrite("BsonFileName");
using (Newtonsoft.Json.Bson.BsonWriter _bsonWriter = new BsonWriter(_fs)
{ CloseOutput = false })
{
Newtonsoft.Json.JsonSerializer _jsonSerializer = new JsonSerializer();
_jsonSerializer.Serialize(_bsonWriter, _object);
_bsonWriter.Flush();
}
Edit:
Here are the resulting files
https://skydrive.live.com/redir?resid=9A6F31F60861DD2C!362&authkey=!AKU-ZZp8C_0gcR0
The efficiency of JSON vs BSON depends on the size of the integers you’re storing. There’s an interesting point where ASCII takes fewer bytes than actually storing integer types. 64-bit integers, which is how it appears your BSON document, take up 8 bytes. Your numbers are all less than 10,000, which means you could store each one in ASCII in 4 bytes (one byte for each character up through 9999). In fact, most of your data look like it’s less than 1000, meaning it can be stored in 3 or fewer bytes. Of course, that deserialization takes time and isn’t cheap, but it saves space. Furthermore, Javascript uses 64-bit values to represent all numbers, so if you wrote it to BSON after converting each integer to a more appropriate dataformat, your BSON file could be much larger.
According to the spec, BSON contains a lot of metadata that JSON doesn’t. This metadata is mostly length prefixes so that you can skip through data you aren’t interested in. For example, take the following data:
["hello there, this is an necessarily long string. It's especially long, but you don't care about it. You're just trying to get to the next element. But I keep going on and on.",
"oh man. here's another string you still don't care about. You really just want the third element in the array. How long are the first two elements? JSON won't tell you",
"data_you_care_about"]
Now, if you’re using JSON, you have to parse the entirety of the first two strings to find out where the third one is. If you use BSON, you’ll get markup more like (but not actually, because I’m making this markup up for the sake of example):
[175 "hello there, this is an necessarily long string. It's especially long, but you don't care about it. You're just trying to get to the next element. But I keep going on and on.",
169 "oh man. here's another string you still don't care about. You really just want the third element in the array. How long are the first two elements? JSON won't tell you",
19 "data_you_care_about"]
So now, you can read ‘175’, know to skip forward 175 bytes, then read ‘169’, skip forward 169 bytes, and then read ’19’ and copy the next 19 bytes to your string. That way you don’t even have to parse the strings for delimiters.
Using one versus the other is very dependent on what your needs are. If you’re going to be storing enormous documents that you’ve got all the time in the world to parse, but your disk space is limited, use JSON because it’s more compact and space efficient.
If you’re going to be storing documents, but reducing wait time (perhaps in a server context) is more important to you than saving some disk space, use BSON.
Another thing to consider in your choice is human readability. If you need to debug a crash report that contains BSON, you’ll probably need a utility to decipher it. You probably don’t just know BSON, but you can just read JSON.
Difference Between JSON and BSON
JavaScript Object Notation (JSON) is a standard file format that uses human type readable text to transmit data with attribute-value pairs and array data types. This is one of the most common data formats which are mainly used for asynchronous browser-server communication. JSON is a language-independent format. BSON, on the other hand, is a computer interchange format that is mainly used for data storage and as a network transfer format in the MongoDB database. It is a simple binary form that is used to represent data structures and associative arrays (often called documents or objects in MongoDB).
Let us study much more about JSON and BSON in detail:
- BSON stands for binary JSON, which consists of a list of ordered elements containing a field name, type, and value. Field name types are typically a string. The fundamental of it includes JavaScript, and there are many programming languages today, which makes use of the code to generate and parse the JSON format related data.
- The BSON type supports the dates and binary data, and because of its nature, this is not in a readable form, whereas normal JSON files consist of a key-value pair. It is not a mandate that the BSON files are always smaller than JSON files, but it surely skips the records which are irrelevant, while in the case of JSON, you need to parse each byte. This is the main reason for using it inside MongoDB.
- The BSON type format is lightweight, highly traversable and fast in nature. BSON implementation is used for supporting embedding objects and arrays within other objects. Inside BSON objects, indexes can be built, and the objects are matched against query expressions on the top level and BSON keys. BSON is a binary encoding JSON document that is used to store documents in collections. Support for data types like binary and date, which aren’t supported in JSON, is added into BSON.
- In practicality, much information about BSON is not needed. Using only the native types of the language and the supplied types such as the ObjectID of the driver is needed, and the mapping will be done on its own to the BSON type.
Head to Head Comparison between JSON and BSON (Infographics)
Below is the top 10 difference between JSON vs BSON:
Key Differences between JSON and BSON
Both are popular choices in the market; let us discuss some of the major difference:
BSON is a serialization format encoding format for JSON mainly used for storing and accessing the documents, whereas JSON is a human-readable standard file format mainly used for transmission of data in the form of key-value attribute pairs.
BSON is designed such that it consumes less space, but it is not extremely efficient as JSON. BSON, in fact, in some cases, uses more space than JSON. The reason for this is traversability which means that BSON adds some additional information to documents like string length and sub-objects, which in turn makes the traversing faster.
BSON is also designed in a way that it has a comparatively faster encoding and decoding technique. For example, all those integers stored as 32-bit integers so that they are not parsed with them to and from the text. Therefore, it uses more space than JSON for smaller integers, but BSON is anyway much faster to parse.
In addition to the above-mentioned points, BSON uses additional data types such as BinData and Date data types unavailable in JSON.BSON extends the JSON type model in order to provide additional data types for more efficient encoding and decoding within different languages, whereas in the case of JSON, no such provision is provided. It facilitates data interchange and along with XML, which is particularly the main format. All the basic data types you can think of are supported by JSON, such as numbers, strings, and other Boolean values. It also supports data types such as hashes and arrays. The BSON utilizes the JSON type format to store the data in the form of documents exactly in the way the data is stored in the form of rows and columns in a relational database.
JSON vs BSON Comparison Table
Below is the topmost Comparison between JSON vs BSON :
The basis Of Comparison |
JSON |
BSON |
Type | Standard file format | Binary file format |
Speed | Comparatively less fast | Faster |
Space | Consumes comparatively less space. | More space is consumed. |
Usage | Transmission of data. | Storage of data. |
Encoding and Decoding technique | No such technique. | Faster encoding and decoding technique. |
Characteristics | Key-value pair only used for transmission of data. | Lightweight, fast and traversable. |
Structure | Language independent format used for asynchronous server browser communication. | Binary JSON which consist of a list of ordered elements containing a field name, type, and value. Field name types are typically a string. |
Traversal | JSON doesn’t skip rather skims through all the content. | BSON, on the other hand, just indexes on the relevant content and skips all the content which does not have to be in use. |
Parse | JSON formats need not be parsed as they are in a human-readable format already. | BSON, on the other hand, needs to be parsed as they are easy for machines to parse and generate. |
Creation type | Broadly JSON consists of an object and an array where the object is a collection of key-value pairs, and the array is an ordered list of values. | The binary encoding technique consists of additional information such as lengths of strings and the object subtypes. Moreover, BinData and Date data types are the data types that are not supported in JSON. |
Conclusion
BSON is not used with every application and mainly extends its usage to NoSQL databases such as MongoDB. Its growing popularity can find its use in many other applications in months to come. On the other hand, many organisations widely use JSON and are among the most popular format being used today in the majority of files. JSON has its own limitations and advantages, and the same is the case for BSON. Choose what suits your organization. Stay tuned to our blogs for more articles like these.
Recommended Articles
This has been a guide to the top difference between JSON vs BSON. Here we also discuss the BSON vs JSON key differences with infographics and comparison table. You may also have a look at the following articles to learn more.
标签:format,type,JSON,VS,BSON,data,types From: https://www.cnblogs.com/panpanwelcome/p/16846649.html