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

admin2025-04-19  1

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 , (ma) 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?

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 , (ma) 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?

Share Improve this question edited Jul 15, 2015 at 8:37 Shiljo Paulson asked Sep 3, 2014 at 13:00 Shiljo PaulsonShiljo Paulson 6088 silver badges17 bronze badges 5
  • Explain the problem in processing the JSV format.. – Swetha Commented Sep 3, 2014 at 13:12
  • 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 , (ma) and need to loop through it. – Shiljo Paulson Commented Sep 3, 2014 at 13:16
  • 1 Can't you read the docs? (API) – Bergi Commented Sep 3, 2014 at 13:28
  • Thanks for sharing the link but how do I assign the sample JSV format data to JavaScript variable? – Shiljo Paulson Commented Sep 3, 2014 at 13:39
  • 1 If you don't know how to parse it, then don't use it. Is bandwidth really that much of an issue? – user663031 Commented Sep 3, 2014 at 16:05
Add a ment  | 

1 Answer 1

Reset to default 8

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

JSV can be used to send plex types in a GET request, which isn't possible using JSON. Some of these other answers, show the use of JSV to send plex 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 , (ma) 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745041639a281577.html

最新回复(0)