Difference between revisions of "VCOM:Working with JSON"

From Vectorworks Developer
Jump to: navigation, search
(Created page with "{{LocationMain|category=LocationVCOMSpecial|specific=}} <div class="rightmenu"> __TOC__ </div> == Enumerating Folder Contents == <code lang="cpp"> IJSONParserPtr jsonParser( ...")
 
Line 4: Line 4:
 
</div>
 
</div>
 
== Enumerating Folder Contents ==
 
== Enumerating Folder Contents ==
 +
 +
The input JSON is:
 +
<code lang="cpp">
 +
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\" "
 +
"   } "
 +
" } "
 +
"  ] "
 +
"} "
 +
;
 +
</code>
 +
 +
Sample code to read parts of it:
  
 
<code lang="cpp">
 
<code lang="cpp">
Line 22: Line 52:
 
colorName = colorName; // -> "white"
 
colorName = colorName; // -> "white"
 
}
 
}
 +
</code>
 +
 +
 +
== Writing a JSON ==
 +
 +
<code lang="cpp">
 +
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;
 
</code>
 
</code>

Revision as of 16:37, 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;
Personal tools
NamespacesFFFF

Variants
Actions
Advanced Search
See Also
Navigation
Toolbox