3

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?

Shiljo Paulson
  • 518
  • 7
  • 17
  • Explain the problem in processing the JSV format.. – Swetha Sep 03 '14 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 , (comma) and need to loop through it. – Shiljo Paulson Sep 03 '14 at 13:16
  • 1
    Can't you read [the docs](https://github.com/ServiceStackV3/mythz_blog/blob/master/pages/176.md)? ([API](http://docs.servicestack.net/text-serializers/jsv-format)) – Bergi Sep 03 '14 at 13:28
  • Thanks for sharing the link but how do I assign the sample JSV format data to JavaScript variable? – Shiljo Paulson Sep 03 '14 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? –  Sep 03 '14 at 16:05

1 Answers1

8

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.

Scott
  • 21,211
  • 8
  • 65
  • 72
  • 1
    Here's the JavaScript implementation of [JSV.js](https://github.com/ServiceStack/ServiceStack/blob/master/lib/js/JSV.js). – mythz Sep 03 '14 at 17:00
  • 1
    @mythz Awesome, clearly I did not know that was there. That certainly makes JSV more approachable. It be good if this library was linked to in the documentation. :) – Scott Sep 03 '14 at 17:23
  • 1
    Yeah I'll remember to do that when I collect the scattered docs about JSV into the SS wiki. But ultimately JSON should still be preferred in Browsers/JavaScript as it can take advantage of browsers native `JSON.parse()` API and JSON introspection tools, which effectively means JSV.js is only really useful for serializing complex objects in QueryString params, and even then when it's too complex it's likely better to be POST'ed as JSON instead. – mythz Sep 03 '14 at 18:20
  • Thanks @mythz for the library link and guidenace – Shiljo Paulson Sep 04 '14 at 10:14
  • link is dead :( – Lomithrani Sep 26 '19 at 17:39
  • @Lomithrani It seems to have been removed from the master branch, but is still [available here](https://github.com/ServiceStack/ServiceStack/blob/v5.4.1/lib/js/JSV.js) – Scott Sep 27 '19 at 08:34