Aligning cJobject serialization with javaScript function names

zIf you are not yet familiar with the cJobject, take a look at How to use cJobject , using javaScript like objects in VBA and jSon. If you have already been using cJobject for working with JSON and VBA, then you’ll be used to doing this.excel to json and backIf you are not yet familiar with the cJobject, take a look at How to use cJobject , using javaScript like objects in VBA and jSon. If you have already been using cJobject for working with JSON and VBA, then you’ll be used to doing this.

Create a JSON string from a cJobject

Create an object from a JSON string

set j = new cjobject
set j = j.init ( Nothing).deSerialize(s)

The result of appending the deserialized object to an existing (empty) cJobject,  also has the annoying side effect of creating an additional object level for j as below

{"_deserialization":{"key":100,"details":{"name":"fred","age":20},"list":["engineer","mathematician","cook" ]}}

Standardization

Whereas different javaScript implementations used to have different ways of serializing JSON, they have now pretty much settled on JSON.parse for deSerialize and JSON.stringify () for serialize.  So I’ve added a couple of things to cJobject to both simplify it and bring it into line with javaScript.

To make JSON from a VBA cJobject 

you can now use either

s=j.stringify()

or

s=JSONStringify(j)

To make a VBA cJobect from jSon

set j = JSONParse (s)

Not only does this bring cJobject it more into line with javaScript , it also gets rid of the additional object level introduced by j.deserialize()

The old methods will continue to work as before, so there is no need to change any existing code.

See Excel, VBA , javaScript and Google for more topics

Return to Excel to JSON and back for me topics