JSON and XML Parsing Library

更新时间:
复制 MD 格式

The JSON and XML Parsing Library provides built-in functions for reading and writing structured data inside smart contracts on Alibaba Cloud Blockchain as a Service (BaaS). Use these functions to parse JSON or XML input, extract typed node values, insert data, and release the parser handle when done.

Function index

FunctionDescription
property_parseParse a JSON or XML string and return a handler
property_destroyRelease the handler returned by property_parse
property_get_boolRead a bool node value
property_get_intRead an int node value
property_get_uintRead a uint node value
property_get_stringRead a string node value
property_set_boolWrite a bool node value
property_set_intWrite an int node value
property_set_uintWrite a uint node value
property_set_stringWrite a string node value

Return value conventions

All functions share a consistent return value pattern:

Function groupresult typeSuccess valueFailure value
property_parseuintNon-zero handler
property_destroy, property_set_*booltruefalse
property_get_*int10

For property_get_* functions, always check result before using data. A result of 0 means the path was not found or the call failed.

Usage notes

Path format: The path parameter is a dot-separated string that locates a node in the parsed document. For example, to access {"order": {"price": 100}}, use path = "order.price". For XML, use the element tag name hierarchy in the same dot notation.

Handler lifecycle: Always call property_destroy after you finish reading or writing. This releases the memory allocated by property_parse.

property_parse

Parses a JSON or XML string and returns a handler used by all subsequent calls.

Syntax

property_parse(string property_value, int property_type) returns(uint result);

Request parameters

NameRequiredTypeDescription
property_valueYesstringThe data to parse, in JSON or XML format.
property_typeYesintThe format of property_value. 0 = JSON, 1 = XML.

Response parameters

NameRequiredTypeDescription
resultYesuintThe handler for this parse session. Pass this value to all subsequent property_get_*, property_set_*, and property_destroy calls.

property_destroy

Releases the handler returned by property_parse and frees the associated memory.

Syntax

property_destroy(uint handler) returns(bool result);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.

Response parameters

NameRequiredTypeDescription
resultYesbooltrue if the handler was released successfully. false if the call failed.

property_get_bool

Reads a node of type bool from the parsed document.

Syntax

property_get_bool(uint handler, string path) returns(int result, bool data);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.
pathYesstringDot-separated path to the target node (for example, "user.active").

Response parameters

NameRequiredTypeDescription
resultYesint1 if the node was found and read successfully. 0 if the call failed.
dataYesboolThe value of the node at path.

property_get_int

Reads a node of type int from the parsed document.

Syntax

property_get_int(uint handler, string path) returns(int result, int data);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.
pathYesstringDot-separated path to the target node (for example, "order.amount").

Response parameters

NameRequiredTypeDescription
resultYesint1 if the node was found and read successfully. 0 if the call failed.
dataYesintThe value of the node at path.

property_get_uint

Reads a node of type uint from the parsed document.

Syntax

property_get_uint(uint handler, string path) returns(int result, uint data);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.
pathYesstringDot-separated path to the target node (for example, "token.balance").

Response parameters

NameRequiredTypeDescription
resultYesint1 if the node was found and read successfully. 0 if the call failed.
dataYesuintThe value of the node at path.

property_get_string

Reads a node of type string from the parsed document.

Syntax

property_get_string(uint handler, string path) returns(int result, string memory data);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.
pathYesstringDot-separated path to the target node (for example, "product.name").

Response parameters

NameRequiredTypeDescription
resultYesint1 if the node was found and read successfully. 0 if the call failed.
dataYesstring memoryThe value of the node at path.

property_set_bool

Creates data in JSON and XML, and inserts data of the bool type.

Syntax

property_set_bool(uint handler, string path, bool data) returns(bool result);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.
pathYesstringDot-separated path to the target node.
dataYesboolThe value to set.

Response parameters

NameRequiredTypeDescription
resultYesbooltrue if the value was set successfully. false if the call failed.

property_set_int

Creates data in JSON and XML, and inserts data of the int type.

Syntax

property_set_int(uint handler, string path, int data) returns(bool result);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.
pathYesstringDot-separated path to the target node.
dataYesintThe value to set.

Response parameters

NameRequiredTypeDescription
resultYesbooltrue if the value was set successfully. false if the call failed.

property_set_uint

Creates data in JSON and XML, and inserts data of the uint type.

Syntax

property_set_uint(uint handler, string path, uint data) returns(bool result);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.
pathYesstringDot-separated path to the target node.
dataYesuintThe value to set.

Response parameters

NameRequiredTypeDescription
resultYesbooltrue if the value was set successfully. false if the call failed.

property_set_string

Creates data in JSON and XML, and inserts data of the string type.

Syntax

property_set_string(uint handler, string path, val data) returns(bool result);

Request parameters

NameRequiredTypeDescription
handlerYesuintThe handler returned by property_parse.
pathYesstringDot-separated path to the target node.
dataYesvalThe value to set.

Response parameters

NameRequiredTypeDescription
resultYesbooltrue if the value was set successfully. false if the call failed.