EOSIO source code analysis - add json parsing library for EOSIO contract development

Why add json library

When we develop applications, it is inevitable to communicate with the server. Similarly, when we write EOSIO contracts, we also need to transfer data. Although EOSIO provides me with the json_to_bin method to package data, it is too troublesome to use. If we want to use other languages ​​(such as python, json, etc.) It will be very troublesome.
But if we use json as the parameter of the transaction action, then all the problems will be solved. As long as the client implements a pack string method, it does not depend on the json_to_bin API, and at the same time, the transfer of large data objects in the transaction can also be solved. , so with the purpose of solving the problem, we should also add a json parsing library to the EOSIO contract development library to reduce the difficulty of contract development.
The benefits of adding a json parsing library are as follows:

  1. The json syntax is easy to use and has wide compatibility, making it the best tool for sharing data
  2. EOSIO transaction data packaging is too complicated and troublesome, and the development efficiency is not high
  3. It is inconvenient to use EOSIO contract parameters to transfer large data

What should be paid attention to when increasing

According to our previous analysis of the EOSIO contract development library, we need to pay attention to the following aspects in order to add our own development library to the contract development library

  1. The code base should be written as concisely as possible, otherwise the compiled contract will become larger
  2. There can be no system operations, file operations, etc. in the code, and they are all related to memory data operations.
  3. There cannot be macros defined according to the platform in the code, which may cause compilation failure and increase the code size
  4. There cannot be exception handling in the code. If you really need to throw a logical exception, please use the check function provided by the development library to assert that the exception is thrown

Example of use

There are not many json libraries that can be directly added to the EOSIO contract development library, and most of them need to be modified. Here is a simple and practical explanation of the modified picojson library. Note: the picojson library has only one header file, and you can use the direct copy
to enter
the original picojson address: https://github.com/kazuho/picojson
EOSIO version picojson address: https://download.csdn.net/download/whg1016/86506368
The following only explains the usage in the contract

parse json string

std::string json_str = "{\"a\":1}";

picojson::value v;
std::string err;
err = picojson::parse(v, json_str);

The result object is stored in the object v, and then the corresponding data can be obtained by using v

Judging that a field exists

check( v.contains("account"), "invalid params(account)");

Determine whether the field account exists, and if not, use the check assertion to throw an exception

get value field

// 获取字符串
auto account = v.get("account").get<std::string>();

// 获取数值
auto balance = v.get("balance").get<int64_t>();

For details, please continue to refer to the development documentation of picojson

string json serialization

picojson::value json;		
json.set<picojson::object>(picojson::object());
json.get<picojson::object>()["from"] = picojson::value(from.to_string());
json.get<picojson::object>()["to"] = picojson::value(to.to_string());
json.get<picojson::object>()["quantity"] = picojson::value(quantity.to_string());
json.get<picojson::object>()["memo"] = picojson::value(std::string("transfer memo"));

action act( { {get_self(), name(active_permission)},{from, name(active_permission)}},
    name("eosio.token"), name("transfer"), json.serialize());
act.send(); 

Summarize

  • Adding a development library to EOSIO is relatively simple, as long as it complies with the rules
  • The writing of the picojson library is not concise enough. The advantage is that it is smaller in size compared to other json libraries.

follow-up plan

Friends who have read the EOSIO source code must appreciate the use of the fc library in EOSIO, which has a set of self-implemented reflection mechanisms. The follow-up plan will try to transplant the reflection-related code in the fc library, which can greatly increase the development efficiency of the contract. However, The resulting contract compilation volume will also increase

Guess you like

Origin blog.csdn.net/whg1016/article/details/126647675