Blockchain as a Service:Function Overview

更新时间:
复制 MD 格式

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

FunctionSignatureReturnsDescription
property_parseproperty_parse(string value, int type)uint handlerParses JSON or XML data. Use type = 0 for JSON, type = 1 for XML.
property_destroyproperty_destroy(uint handler)boolReleases 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.

FunctionSignatureReturns
property_get_boolproperty_get_bool(uint handler, string path)(int err, bool value)
property_get_intproperty_get_int(uint handler, string path)(int err, int value)
property_get_uintproperty_get_uint(uint handler, string path)(int err, uint value)
property_get_stringproperty_get_string(uint handler, string path)(int err, string memory value)
property_get_list_countproperty_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.

FunctionSignatureReturns
property_set_boolproperty_set_bool(uint handler, string path, bool value)bool
property_set_intproperty_set_int(uint handler, string path, int value)bool
property_set_uintproperty_set_uint(uint handler, string path, uint value)bool
property_set_stringproperty_set_string(uint handler, string path, string value)bool

Generate output

FunctionSignatureReturnsDescription
property_writeproperty_write(uint handler, int type)string memorySerializes the property tree to JSON (type = 0) or XML (type = 1).

Remove a property

FunctionSignatureReturns
property_removeproperty_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);
    }
}