JSON 字符串中的二進制數據。 比 Base64 更好的東西 - Binary Data in JSON String. Something better than Base64

問題:

The JSON format natively doesn't support binary data. JSON 格式本身不支持二進制數據。 The binary data has to be escaped so that it can be placed into a string element (ie zero or more Unicode chars in double quotes using backslash escapes) in JSON.必須對二進制數據進行轉義,以便將其放入 JSON 中的字符串元素(即,使用反斜槓轉義的雙引號中的零個或多個 Unicode 字符)。

An obvious method to escape binary data is to use Base64.轉義二進制數據的一個明顯方法是使用 Base64。 However, Base64 has a high processing overhead.但是,Base64 的處理開銷很高。 Also it expands 3 bytes into 4 characters which leads to an increased data size by around 33%.此外,它將 3 個字節擴展爲 4 個字符,這導致數據大小增加了約 33%。

One use case for this is the v0.8 draft of the CDMI cloud storage API specification .一個用例是CDMI 雲存儲 API 規範的 v0.8 草案。 You create data objects via a REST-Webservice using JSON, eg您使用 JSON 通過 REST-Webservice 創建數據對象,例如

PUT /MyContainer/BinaryObject HTTP/1.1
Host: cloud.example.com
Accept: application/vnd.org.snia.cdmi.dataobject+json
Content-Type: application/vnd.org.snia.cdmi.dataobject+json
X-CDMI-Specification-Version: 1.0
{
    "mimetype" : "application/octet-stream",
    "metadata" : [ ],
    "value" :   "TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
    IHNpbmd1bGFyIHBhc3Npb24gZnJvbSBvdGhlciBhbmltYWxzLCB3aGljaCBpcyBhIGx1c3Qgb2Yg
    dGhlIG1pbmQsIHRoYXQgYnkgYSBwZXJzZXZlcmFuY2Ugb2YgZGVsaWdodCBpbiB0aGUgY29udGlu
    dWVkIGFuZCBpbmRlZmF0aWdhYmxlIGdlbmVyYXRpb24gb2Yga25vd2xlZGdlLCBleGNlZWRzIHRo
    ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=",
}

Are there better ways and standard methods to encode binary data into JSON strings?是否有更好的方法和標準方法將二進制數據編碼爲 JSON 字符串?


解決方案:

參考一: https://en.stackoom.com/question/63Qk
參考二: https://stackoom.com/question/63Qk
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章