VCOM:Working with JSON

From Vectorworks Developer
Revision as of 16:37, 16 November 2018 by Root (Talk | contribs)

Jump to: navigation, search

.SDK|SDK ..SDK:Types|SDK Types ..VCOM:VCOM (Vectorworks Component Object Model)|VCOM Basics ..VCOM:Class Reference|VCOM Class Reference

Enumerating Folder Contents

The input JSON is:

TXString json = 
		"{						"
		"  \"hasData\": true,				"
		"  \"colors\": [				"
		"	{					"
		"	  \"color\": \"black\",			"
		"	  \"category\": \"hue\",		"
		"	  \"type\": \"primary\",		"
		"	  \"code\": {				"
		"		\"rgba\": [255,255,255,1],	"
		"		\"hex\": \"#000\"		"
		"	  }					"
		"	},					"
		"	{					"
		"	  \"color\": \"white\",			"
		"	  \"category\": \"value\",		"
		"	  \"code\": {				"
		"		\"rgba\": [0,0,0,1],		"
		"		\"hex\": \"#FFF\"		"
		"	  }					"
		"	}					"
		"  ]						"
		"}						"
		;

Sample code to read parts of it:

IJSONParserPtr jsonParser( IID_JSONParser );

TJSONMap	jsonData;
VCOMError err = jsonParser->Extract( json, jsonData );
if ( VCOM_SUCCEEDED( err ) )
{
	if ( jsonData.find("hasData") != jsonData.end() )
	{
		bool hasData = jsonData["hasData"]->Bool( false );
		hasData = hasData; // -> true
	}

	TJSONArray arrColors = jsonData["colors"]->Array();
	TXString colorName = arrColors[1]->Map()["color"]->String();
	colorName = colorName; // -> "white"
}


Writing a JSON

IJSONParserPtr jsonParser( IID_JSONParser );

TJSONMap	jsonData;
jsonData["hasData"] = jsonParser->CompactBool( true );

TJSONMap	color1;
color1["color"] = jsonParser->CompactString("black");
color1["category"] = jsonParser->CompactString("value 1");

TJSONMap	color2;
color2["color"] = jsonParser->CompactString("white");
color2["category"] = jsonParser->CompactString("value 2");

TJSONArray arrColors;
arrColors.push_back( jsonParser->CompactMap( color1 ) );
arrColors.push_back( jsonParser->CompactMap( color2 ) );

jsonData["colors"] = jsonParser->CompactArray( arrColors );

// write out the json
TXString outJson;
jsonParser->Compact( jsonData, outJson );
outJson = outJson;
Personal tools
NamespacesFFFF

Variants
Actions
Advanced Search
See Also
Navigation
Toolbox