The Ant Blockchain contract platform extends Solidity with precompiled function libraries for parsing and constructing JSON and XML data. These functions are available in your smart contracts without any imports — the platform incorporates them at compile time.
Function reference
All functions follow a common pattern: call property_parse to get a handler, use getter and setter functions to read or write properties, then call property_destroy to release the handler.
Always call property_destroy after you finish using a handler. Failing to release handlers causes resource leaks in your contract.Parse and release
| Function | Signature | Returns | Description |
|---|---|---|---|
property_parse | property_parse(string value, int type) | uint handler | Parses JSON or XML data. Use type = 0 for JSON, type = 1 for XML. |
property_destroy | property_destroy(uint handler) | bool | Releases the handler and frees associated resources. |
Read properties
Most getter functions take a handler and a dot-notation path string and return an error code (int err) and the retrieved value. The exception is property_get_list_count, which returns a uint count directly.
| Function | Signature | Returns |
|---|---|---|
property_get_bool | property_get_bool(uint handler, string path) | (int err, bool value) |
property_get_int | property_get_int(uint handler, string path) | (int err, int value) |
property_get_uint | property_get_uint(uint handler, string path) | (int err, uint value) |
property_get_string | property_get_string(uint handler, string path) | (int err, string memory value) |
property_get_list_count | property_get_list_count(uint handler, string path) | uint count |
Path notation:
Nested properties:
"node_config.node.port"Array element:
"node_config.user_keys[0].key_name"XML CDATA:
"node_config.<xmlcdata>"XML attribute:
"node_config.<xmlattr>.key_path"
Write properties
All setter functions take a handler, a dot-notation path, and the value to set. They return true on success.
| Function | Signature | Returns |
|---|---|---|
property_set_bool | property_set_bool(uint handler, string path, bool value) | bool |
property_set_int | property_set_int(uint handler, string path, int value) | bool |
property_set_uint | property_set_uint(uint handler, string path, uint value) | bool |
property_set_string | property_set_string(uint handler, string path, string value) | bool |
Generate output
| Function | Signature | Returns | Description |
|---|---|---|---|
property_write | property_write(uint handler, int type) | string memory | Serializes the property tree to JSON (type = 0) or XML (type = 1). |
Remove a property
| Function | Signature | Returns |
|---|---|---|
property_remove | property_remove(uint handler, string path) | bool |
Examples
Parse JSON data
The following contract parses a nested JSON string and reads individual property values using dot-notation paths.
pragma solidity ^0.4.23;
contract test_parse {
// test json interface
function test_property_parse_json() public returns (uint)
{
string memory property_value;
int property_type;
property_value = "{\"node_config\" : {\"identity\" : \"true\",\"key_path\" : \"../../user\",\"user_keys\" : [{\"key_name\" : \"Tester001.key\",\"key_passwd\" : \"123456a\"},{\"key_name\" : \"Tester002.key\",\"key_passwd\" : \"123456a\"}],\"node\" : {\"ip\" : \"127.0.0.1\",\"port\" : 18130}}}";
property_type = 0;
// Call property_parse.
uint handler = property_parse(property_value, property_type);
// Obtain JSON-formatted data properties.
test_property_get_bool(handler);
test_property_get_int(handler);
test_property_get_uint(handler);
test_property_get_string(handler);
test_property_get_array(handler);
test_property_get_list_count(handler);
// Release resources.
test_property_destroy(handler);
return handler;
}
// test xml interface
function test_property_parse_xml() public returns (uint)
{
string memory property_value;
int property_type;
property_value = "<?xml version=\"1.0\" encoding=\"utf-8\"?><node_config key_path=\"../../user\"> <![CDATA[cdata]]><identity>true</identity><user_keys> <key_name>Tester001.key</key_name> <key_passwd>123456a</key_passwd> </user_keys> <user_keys> <key_name>Tester002.key</key_name> <key_passwd>123456a</key_passwd> </user_keys> <node> <ip>127.0.0.1</ip> <port>18130</port> </node> </node_config>";
property_type = 1;
// Call property_parse.
uint handler = property_parse(property_value, property_type);
// Obtain XML-formatted data properties.
test_property_get_bool(handler);
test_property_get_int(handler);
test_property_get_uint(handler);
test_property_get_string(handler);
test_property_get_array(handler);
test_property_get_cdata(handler);
test_property_get_attr(handler);
test_property_get_list_count(handler);
// Release resources.
test_property_destroy(handler);
return handler;
}
function test_property_destroy(uint hanlder) public returns (bool)
{
return property_destroy(hanlder);
}
function test_property_get_bool(uint hanlder) public returns (int, bool)
{
string memory path;
path = "node_config.identity";
int err;
bool ret;
(err, ret) = property_get_bool(hanlder, path);
return (err, ret);
}
function test_property_get_int(uint hanlder) public returns (int, int)
{
string memory path;
path = "node_config.node.port";
int err;
int ret;
(err, ret) = property_get_int(hanlder, path);
return (err, ret);
}
function test_property_get_uint(uint hanlder) public returns (int, uint)
{
string memory path;
path = "node_config.node.port";
int err;
uint ret;
(err, ret) = property_get_uint(hanlder, path);
return (err, ret);
}
function test_property_get_string(uint hanlder) public returns (int, string memory)
{
string memory path;
path = "node_config.node.ip";
int err;
string memory ret;
(err, ret) = property_get_string(hanlder, path);
return (err, ret);
}
function test_property_get_array(uint hanlder) public returns (int, string memory)
{
// string memory path;
// path = "node_config.user_keys[0].key_name";
int err;
string memory ret;
(err, ret) = property_get_string(hanlder, "node_config.user_keys[0].key_name");
return (err, ret);
}
function test_property_get_list_count(uint hanlder) public returns (uint) {
return property_get_list_count(hanlder, "node_config.user_keys");
}
function test_property_get_cdata(uint hanlder) public returns (int, string memory)
{
string memory path;
path = "node_config.<xmlcdata>";
int err;
string memory ret;
(err, ret) = property_get_string(hanlder, path);
return (err, ret);
}
function test_property_get_attr(uint hanlder) public returns (int, string memory)
{
string memory path;
path = "node_config.<xmlattr>.key_path";
int err;
string memory ret;
(err, ret) = property_get_string(hanlder, path);
return (err, ret);
}
}Construct and serialize data
The following contract builds a property tree by setting individual values, then serializes it to JSON or XML using property_write.
pragma solidity ^0.4.23;
contract test_write {
///////test this interface only
function test_property_write() public returns (string memory)
{
string memory property_value;
int property_type = 0;
// Call property_parse.
uint handler = property_parse(property_value, property_type);
// Set properties.
test_property_set_bool(handler);
test_property_set_int(handler);
test_property_set_uint(handler);
test_property_set_string(handler);
property_type = 1;
// Generate JSON- or XML-formatted data.
string memory ret = property_write(handler, property_type);
property_destroy(handler);
return ret;
}
function test_property_set_bool(uint handler) public returns (bool)
{
string memory path;
path = "a.b.c";
return property_set_bool(handler, path, true);
}
function test_property_set_int(uint handler) public returns (bool)
{
string memory path;
path = "e.f.g.h";
return property_set_int(handler, path, 0x12);
}
function test_property_set_uint(uint handler) public returns (bool)
{
string memory path;
path = "h.m.l";
return property_set_uint(handler, path, 1);
}
function test_property_set_string(uint handler) public returns (bool)
{
string memory path;
path = "a.b.g";
string memory val;
val = "testhahahaha";
return property_set_string(handler, path, val);
}
function test_property_set_cdata(uint handler) public returns (bool)
{
string memory path;
path = "zz.<xmlcdata>";
string memory val;
val = "cdata";
return property_set_string(handler, path, val);
}
function test_property_set_attr(uint handler) public returns (bool)
{
string memory path;
path = "zz.<xmlattr>.attr";
string memory val;
val = "attr";
return property_set_string(handler, path, val);
}
function test_property_remove(uint handler) public returns (bool)
{
string memory path;
path = "a.b.g";
return property_remove(handler, path);
}
}