区块链学习笔记前置篇之一:基于boost的json读写

先来定义一下json数据格式,原则上说,一个块上可以有多个交易信息,此处为了简化,每个块上只存放一个交易信息

block = {
'index': 1,
'timestamp': 1506057125.900785,
'transactions': [
{
'sender': "8527147fe1f5426f9dd545de4b27ee00",
'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f",
'amount': 5,
}
],
'proof': 324984774000,
'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"
}

接下来是定义json的数据结构

typedef struct __transactions
{
    std::string sender;
    std::string recipient;
    float amount;
}Transactions;

typedef struct __block
{
    int index;
    time_t timestamp;
    std::list<Transactions> lst_ts;
    long int proof;
    std::string previous_hash;
} Block;

下面是基于boost的json读写函数

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

std::string GetJsonFromBlock(Block &block)
{
    boost::property_tree::ptree item;

    boost::property_tree::ptree lstts;
    {
        std::list<Transactions>::iterator it;
        for (it = block.lst_ts.begin(); it != block.lst_ts.end(); ++it)
        {
            boost::property_tree::ptree ts;
            ts.put("sender", it->sender);
            ts.put("recipient", it->recipient);
            ts.put("amount", it->amount);
            lstts.push_back(make_pair("", ts));
        }
    }

    item.put("index", block.index);
    item.put("timestamp", block.timestamp);
    item.put_child("transactions", lstts);
    item.put("proof", block.proof);
    item.put("previous_hash", block.previous_hash);

    std::stringstream is;
    boost::property_tree::write_json(is, item);
    return is.str();
}

Block GetBlockFromJson(const std::string &json)
{
    Block block;
    std::stringstream ss(json);
    boost::property_tree::ptree pt;
    boost::property_tree::ptree array;
    boost::property_tree::read_json(ss, pt);
    block.index = pt.get<int>("index");
    block.previous_hash = pt.get<std::string>("previous_hash");
    block.proof = pt.get<long int>("proof");
    block.timestamp = pt.get<time_t>("timestamp");
    array = pt.get_child("transactions");

    for (auto v : array)
    {
        Transactions ts;
        ts.sender = v.second.get<std::string>("sender");
        ts.recipient = v.second.get<std::string>("recipient");
        ts.amount = v.second.get<float>("amount");
        block.lst_ts.push_back(ts);
    }

    return block;
}

std::string GetJsonFromBlockList(std::list<Block> &lst_block)
{
    int i = 0;

    boost::property_tree::ptree item;

    boost::property_tree::ptree pblock;
    {
        std::list<Block>::iterator bit;
        for (bit = lst_block.begin(); bit != lst_block.end(); ++bit)
        {
            boost::property_tree::ptree b;
            boost::property_tree::ptree pts;
            {
                std::list<Transactions>::iterator tit;
                for (tit = bit->lst_ts.begin(); tit != bit->lst_ts.end(); ++tit)
                {
                    boost::property_tree::ptree t;
                    t.put("sender", tit->sender);
                    t.put("recipient", tit->recipient);
                    t.put("amount", tit->amount);
                    pts.push_back(make_pair("", t));
                }
            }

            b.put("index", bit->index);
            b.put("timestamp", bit->timestamp);
            b.put_child("transactions", pts);
            b.put("proof", bit->proof);
            b.put("previous_hash", bit->previous_hash);
            pblock.push_back(make_pair("", b));

            ++i;
        }
    }

    item.put_child("chain", pblock);
    item.put("length", i);

    std::stringstream is;
    boost::property_tree::write_json(is, item);
    return is.str();
}

std::list<Block> GetBlockListFromJson(const std::string &json)
{
    std::list<Block> lstBlock;
    std::stringstream ss(json);
    boost::property_tree::ptree pt;
    boost::property_tree::ptree barray;
    boost::property_tree::read_json(ss, pt);
    barray = pt.get_child("chain");

    for (auto bv : barray)
    {
        Block block;
        boost::property_tree::ptree tarray;

        block.index = bv.second.get<int>("index");
        block.previous_hash = bv.second.get<std::string>("previous_hash");
        block.proof = bv.second.get<long int>("proof");
        block.timestamp = bv.second.get<time_t>("timestamp");
        tarray = bv.second.get_child("transactions");

        for (auto tv : tarray)
        {
            Transactions ts;
            ts.sender = tv.second.get<std::string>("sender");
            ts.recipient = tv.second.get<std::string>("recipient");
            ts.amount = tv.second.get<float>("amount");
            block.lst_ts.push_back(ts);
        }
        lstBlock.push_back(block);
    }

    return lstBlock;
}

猜你喜欢

转载自blog.csdn.net/mumufan05/article/details/81985568