c++ json与protobuf 转换

/*
 *   data:  2018-04-14
 *   author: cc509a
 *   email:
 *   protocol buffer and json convert each other
 *   support array ,enum

*/
#ifndef MYLIB_JSON_2_PB
# define MYLIB_JSON_2_PB

#include "json.h"


#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>

namespace mylib {

class MylibJson2Pb {

public:

    typedef ::google::protobuf::Message         ProtobufMsg;
    typedef ::google::protobuf::Reflection      ProtobufReflection;
    typedef ::google::protobuf::FieldDescriptor ProtobufFieldDescriptor;
    typedef ::google::protobuf::Descriptor      ProtobufDescriptor;

public:

/* 枚举是字符串 enum2str 传true */
static void PbMsg2JsonStr(const ProtobufMsg& src, std::string& dst, bool enum2str = false) 
{
    Json::Value value;
    PbMsg2Json(src, value, enum2str);
    Json::FastWriter writer;
    dst = writer.write(value);
}

static bool JsonStr2PbMsg(const std::string& src, ProtobufMsg& dst, bool str2enum = false) 
{
    Json::Value value;
    Json::Reader reader(Json::Features::strictMode());
    if (!reader.parse(src, value))
    {
        LOG_ERROR("parse json string is fail,str=%s", src.c_str());
        return false;
    }
    if(true != Json2PbMsg(value, dst, str2enum))
    {
        LOG_ERROR("pb convert error");
        return false;
    }
    return true;
}



static bool Json2PbMsg(const Json::Value& src, ProtobufMsg& dst, bool str2enum = false)
{
    const ProtobufDescriptor* descriptor = dst.GetDescriptor();
    const ProtobufReflection* reflection = dst.GetReflection();
    if (NULL == descriptor || NULL == reflection) return false;

    int32_t count = descriptor->field_count();
    for (int32_t i = 0; i < count; ++i) 
    {
        const ProtobufFieldDescriptor* field = descriptor->field(i);
        if (NULL == field) continue;

        if(!src.isMember(field->name()))
        {
            continue;
        }
        const Json::Value& value = src[field->name()];

        if (field->is_repeated()) 
        {
            if (!value.isArray()) 
            {
                LOG_ERROR("pb error");
                return false;
            } 
            else 
            {
                Json2RepeatedMessage(value, dst, field, reflection, str2enum);
                continue;
            }
        }
        switch (field->type()) 
        {
            case ProtobufFieldDescriptor::TYPE_BOOL: 
            {
                if (value.isBool())
                {
                    reflection->SetBool(&dst, field, value.asBool());
                }

                else if (value.isInt())
                {
                    reflection->SetBool(&dst, field, value.isInt());
                }

                else if (value.isString())
                {
                    if (value.asString() == "true")
                        reflection->SetBool(&dst, field, true);
                    else if (value.asString() == "false")
                        reflection->SetBool(&
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值