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

I want to use JSV and not 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 make it simple? I'm not sure if this is the best way to handle it; But according to my knowledge / understanding, I need to split the string using :

(colon) and ,

(comma) and you need to step through it. But I'm concerned that this doesn't explain the character's departure. Is there an official JavaScript library that can be used to parse the JSV format - I couldn't find it?

+3


source to share


1 answer


  Are there any advantages to JSV over JSON other than a few bytes stored in JSV format?

JSV can be used to send complex types in a GET request, which is not possible with JSON. Some of these 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 JsvServiceClient

JavaScript.

I'm not sure if this is the best way to handle it, but as far as I know we need to split the string using: (colon) and, (comma) and need to go through it.

Use the above library and don't parse it yourself.



Deserializing Usage:

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

      

Serializing Usage:

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

      

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

Unless you plan on using exceptionally large datasets, saving from using JSV can be negated by the requirement to send a JavaScript JSV library to handle requests. Official JSV library, downsized to 15KB or about 8KB. Remember, JSON support is already built into web browsers at no extra cost.

+8


source







All Articles