VCOM:Working with JSON: Difference between revisions

From Vectorworks Developer
Jump to navigation Jump to search
No edit summary
 
Line 81: Line 81:
jsonParser->Compact( jsonData, outJson );
jsonParser->Compact( jsonData, outJson );
outJson = outJson;
outJson = outJson;
</code>
Results the following JSON:
<code lang='json'>
{
"colors" : [
{
"category" : "value 1",
"color" : "black"
},
{
"category" : "value 2",
"color" : "white"
}
],
"hasData" : true
}
</code>
</code>

Latest revision as of 20:39, 16 November 2018

.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;

Results the following JSON:

{
	"colors" : [
		{
			"category" : "value 1",
			"color" : "black"
		},
		{
			"category" : "value 2",
			"color" : "white"
		}
	],
	"hasData" : true
}