智能合约从入门到精通:Lib工具库(二)

简介:上一节,我们介绍智能合约开发中常用的Lib工具库的第一部分。由于内容较长,工具库我们将分两部分介绍,本文将介绍Lib工具库的第二部分:LibJson 、LibStack和LibLog。

LibJson

LibJson主要封装了对JSON格式的字符串一些操作;

支持直接调用、using for *;调用

注意:正如在Lib工具库说明中提到的,LibJson 库的使用稍微有点特殊:

不管是直接调用,还是using for *;方式调用,在合约方法中,如果要使用一次、或者多次LibJson库中的方法,则在第一次使用LibJsos库方法前,需要对被操作的json字符串进行如下操作:

//字符串入栈
LibJson.push(_json);

在最后一个LibJsos库方法之后,要进行如下操作

//出栈

LibJson.pop();

示例:

pragma solidity ^0.4.2;

import "./utillib/LibJson.sol";

contract TestManager {

    using LibJson for *;

    string[] public _arr;

    function test() constant returns(bool _ret) {
        string memory _json = "{\"errno\":27,\"hash\":\"0xf16a2a734ccf0456807e166ad310ec7767f71e8d4003154b73596741683c6433\",\"nodeId\":\"JZNCGP\",\"type\":1}";
       // step01: 字符串入栈
       LibJson.push(_json);
       // step02: 操作字符串,判断自否为一个合法的json格式
       bool isJson = _json.isJson();   
       //或者直接调用 
       //bool isJson = LibJson.isJson(_json);

       _json.jsonRead("nodeId");     // "JZNCGP"
       // step03: 出栈
       LibJson.pop();
    }
}
  • LibJson.push()  LibJson.pop() 一定是成对出现的;
  • 当对字符串的操作结束后务必调用pop将栈中的元素移除掉;

JSON格式判断

描述:判断指定串是否为标准的JSON格式

结构定义

function isJson(string _json) internal constant returns(bool _ret);

示例

string memory _json = "{\"errno\":27,\"hash\":\"0xf16a2a734ccf0456807e166ad310ec7767f71e8d4003154b73596741683c6433\",\"nodeId\":\"JZNCGP\",\"type\":1}";

bool _ret= _json.isJson();        //  _ret = true

读取JSON中key的值

描述:指定key读取JSON串中的值

结构定义

function jsonRead(string _json, string _keyPath) internal constant returns(string _ret);

示例

string memory _json = "{\"errno\":27,\"hash\":\"0xf16a2a734ccf0456807e166ad310ec7767f71e8d4003154b73596741683c6433\",\"nodeId\":\"JZNCGP\",\"type\":1}";

string memory _ret= _json.jsonRead("nodeId");        //  _ret = "JZNCGP"

 JSON的key是否存在

描述:判断JSON中的key是否存在

结构定义

function jsonKeyExists(string _json, string _keyPath) internal constant returns(bool _ret)

示例

string memory _json = "{\"errno\":27,\"hash\":\"0xf16a2a734ccf0456807e166ad310ec7767f71e8d4003154b73596741683c6433\",\"nodeId\":\"JZNCGP\",\"type\":1}";

bool _ret= _json.jsonKeyExists("nodeId");        //  _ret = true

uint[]数组转JSON字符串

描述:将一个uint[]数组转为json格式字符串

结构定义

function toJsonArray(uint[] storage _self) internal constant returns(string _json);

Copy

示例

pragma solidity ^0.4.2;

 

import "LibJson.sol";

 

contract TestManager {

 

    using LibJson for *;

 

    uint[] public _arr;

 

    function test() constant returns(string _ret) {

            _arr.push(1);

            _arr.push(2);

            _ret = _arr.toJsonArray();    // _ret = [1,2]

    }

}

string[]数组转JSON字符串

描述:将一个uint[]数组转为json格式字符串

结构定义

function toJsonArray(string[] storage _self) internal constant returns(string _json);

示例

pragma solidity ^0.4.2;

 

import "LibJson.sol";

 

contract TestManager {

 

    using LibJson for *;

 

    string[] public _arr;

 

    function test() constant returns(string _ret) {

            _arr.push("1");

            _arr.push("2");

            _ret = _arr.toJsonArray();    // _ret = ["1","2"]

    }

}

字符串整形数组转uint[]

描述:将一个字符串的整形数组转为uint[],如:"[1,3,4]"

结构定义

function fromJsonArray(uint[] storage _self, string _json) internal returns(bool succ);

示例

pragma solidity ^0.4.2;

 

import "LibJson.sol";

 

contract TestManager {

 

    using LibJson for *;

 

    uint[] public _arr;

 

    function test() constant returns(string _ret) {

           string memory _json = "[1,2,3]";

           _arr.fromJsonArray(_json);    // _arr = [1,2,3],_arr.length = 3

    }

}

字符串数组转string[]

描述:将一个字符串的整形数组转为string[],如:["1","3","4"]

结构定义

function fromJsonArray(uint[] storage _self, string _json) internal returns(bool succ);

示例

pragma solidity ^0.4.2;

 

import "LibJson.sol";

 

contract TestManager {

 

    using LibJson for *;

 

    string[] public _arr;

 

    function test() constant returns(string _ret) {

           string memory _json = "[\"1\",\"2\",\"3\"]";

           _arr.fromJsonArray(_json);    // _arr = ["1","2","3"],_arr.length = 3

    }

}

元素入栈

描述:当需要对JSON进行操作,如:isJson()、jsonRead()操作时需要先进行push()操作,与此同时当使用后一定使用pop()进行栈数据的移除;

结构定义

 function push(string _json) internal constant returns(uint _len) ;

Copy

示例

pragma solidity ^0.4.2;

 

import "LibJson.sol";

 

contract TestManager {

 

    using LibJson for *;

 

    string[] public _arr;

 

    function test() constant returns(string _ret) {

           string memory _json = "{\"errno\":27,\"hash\":\"0xf16a2a734ccf0456807e166ad310ec7767f71e8d4003154b73596741683c6433\",\"nodeId\":\"JZNCGP\",\"type\":1}";

           LibJson.push(_json);

           _json.isJson();

           _json.jsonRead("type");        // return 1

           LibJson.pop();

    }

}

元素出栈

描述:当需要对JSON进行操作,如:isJson()、jsonRead()操作时需要先进行push()操作,与此同时当使用后一定使用pop()进行栈数据的移除;

结构定义

function pop() internal constant;

示例

pragma solidity ^0.4.2;

 

import "LibJson.sol";

 

contract TestManager {

 

    using LibJson for *;

 

    function test() constant returns(string _ret) {

           string memory _json = "{\"errno\":27,\"hash\":\"0xf16a2a734ccf0456807e166ad310ec7767f71e8d4003154b73596741683c6433\",\"nodeId\":\"JZNCGP\",\"type\":1}";

           LibJson.push(_json);

           _json.isJson();

           _json.jsonRead("type");        // return 1

           LibJson.pop();

    }

}

LibStack

LibStack封装了对栈的使用,合约中通过栈进行字符串拼接操作。

仅支持直接调用

pragma solidity ^0.4.2;

 

import "./utillib/LibStack.sol";

 

contract TestManager {

 

    function test() constant returns(string _ret) {

        len = LibStack.push("{");

        len = LibStack.appendKeyValue("userAddr", "1");

        len = LibStack.appendKeyValue("name", "2");

        len = LibStack.append("}");

        _ret = LibStack.popex(len);

        // _ret = {"userAddr":"1","name":"2"}

    }

}

说明:

  • 栈的使用开始前必须进行LlibStack.push();操作,如果没有固定元素可push一个空串;
  • 每次入栈数据后会返回当前占中元素长度;
  • 出栈时指定要出栈的长度,即可获得栈中的字符串信息

入栈

描述:对一个栈使用前必须要做的事,push()调用就新开了一个栈空间使用,如果没有固定元素可以push一个空字符串:LibStack.push("");

结构定义

function push(string _data) internal constant returns(uint _len);

示例

uint len = LibStack.push("");        // 返回当前栈中元素个数

出栈

描述:当需要获取栈中元素时调用popex()获取栈中数据.

结构定义

function popex() internal constant returns(string _ret);

示例

uint len = LibStack.push("");        // 返回当前栈中元素个数

len = LibStack,append("aaa");    // append 追加单个元素

string memory _ret = LibStack.popex(len);    // _ret = "aaa"

向栈追加单个元素

描述:当栈开辟空间后向栈中追加单个元素

结构定义

function append(string _data) internal constant returns(uint _len);

示例

uint len = LibStack.push("");        // 返回当前栈中元素个数

len = LibStack,append("aaa");    // append 追加单个元素

string memory _ret = LibStack.popex(len);    // _ret = "aaa"

向栈追加k-v键值对

描述:当栈开辟空间后向栈中追加k-v键值对

结构定义

function appendKeyValue(string _key, string _val) internal constant returns (uint _len) ;

function appendKeyValue(string _key, uint _val) internal constant returns (uint _len) ;

 function appendKeyValue(string _key, int _val) internal constant returns (uint _len) ;

 function appendKeyValue(string _key, address _val) internal constant returns (uint _len);

示例

uint len = LibStack.push("{");        // 返回当前栈中元素个数

len = LibStack,appendKeyValue("name","Tom");  

len = LibStack,appendKeyValue("age",1);

len = LibStack,appendKeyValue("creator",0x8affd1952705d8a908e016695c6e454ad39a1c6f);

len = LibStack,append("}");

string memory _ret = LibStack.popex(len);    

// _ret = {"name":"Tom","age":1,"creator":"0x8affd1952705d8a908e016695c6e454ad39a1c6f"}

LibLog

LibLog主要封装了日志打印操作,在合约中的输出日志体现在日志文件中进行输出

仅支持直接调用

日志输出(多元素)

描述:输入一个字符串进行日志输出

结构定义

function log(string _str) internal constant returns(uint _ret);

function log(string _str, string _str2, string _str3) internal constant returns(uint _ret);

function log(string _str, string _str2) internal constant returns(uint _ret);

示例

LibLog.log("hello world");

LibLog.log("hello world","01");

LibLog.log("hello world","01","02");

日志输出(不同类型)

描述:不同类型输入日志输出

结构定义

// 字符串 + 无符号整形

function log(string _str, uint _ui) internal constant returns(uint _ret);
 

// 字符串 + 有符号整形

function log(string _str, int _i) internal constant returns(uint _ret);
 

// 字符串 + 地址

 function log(string _str, address _addr) internal constant returns(uint _ret);

示例

LibLog.log("hello world",111);

LibLog.log("hello world",131);

LibLog.log("hello world",0x8affd1952705d8a908e016695c6e454ad39a1c6f);

参考内容:https://open.juzix.net/doc

智能合约开发教程视频:http://edu.51cto.com/course/13403.html

posted @ 2018-07-09 10:58  区块链技术  阅读(368)  评论(0编辑  收藏  举报