简体   繁体   中英

How can I convert a JavaScript object(JSON) into JSV format?

I am looking to use JSV rather than JSON to save bandwidth when sending my ajax requests to my ServiceStack service.

I have the following JSON data:

[{"201":"New York","022":"Chicago"}]

And I would like to convert it to the following JSV format:

[{201:New York,022:Chicago}]

Is there a way to do this simply? I am not sure what is the best way to process it; But as per my knowledge/understanding I need to split the string using : (colon) and , (comma) and need to loop through it. But I am concerned this doesn't account for character escaping. Is there an official JavaScript library that can be used to parse the JSV format - I couldn't find one?

Is there any advantages on JSV over JSON other than few bytes saved in JSV format?

JSV can be used to send complex types in a GET request, which isn't possible using JSON. Some of these other answers , show the use of JSV to send complex types in a GET request.

Do we have any JavaScript library or function to parse JSV?

The official JSV library for JavaScript can be found here . It also includes a JsvServiceClient implemented in JavaScript.

I am not sure what is the best way to process it but as per my knowledge/understanding we need to split the string using : (colon) and , (comma) and need to loop through it.

Use the above library and avoid parsing it yourself.

Deserialize Usage:

JSV.parse("<JSV String>");

Serialize Usage:

var myObject = {Id: 123, Test: [1,2,3]};
JSV.stringify(myObject);

I am planning to use JSV instead of JSON format to save the bandwidth

Unless you are planning on using exceptionally large data sets, then the saving from using JSV may be negated by the requirement to send a JavaScript JSV parsing library to handle the requests. The official JSV library, is 15KB minified or about 8KB minified. Remember that JSON support is already built in to web browsers without overhead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM