UE4表格工具三部曲之三【ProtocolBuff序列化和反序列化】

syntax = "proto3";
option optimize_for = SPEED;

message TestStruct
{
    map<int32,string> data = 1;
}
#include <iostream>
#include <cstdio>
#include <list>
#include <string>
#include <cstdint>
#include <ctime>
#include "msg.pb.h"
#include "google/protobuf/text_format.h"
using namespace google::protobuf;

int32_t main()
{
    TestStruct tSt1;
    tSt1.mutable_data()->insert({ 1, "str1" });
    tSt1.mutable_data()->insert({ 1, "str11" });
    tSt1.mutable_data()->insert(MapPair<int32_t, std::string>(2, "str2"));
    std::string data;
    tSt1.SerializeToString(&data);

    TestStruct tSt2;
    tSt2.ParseFromString(data);
    for(auto it = tSt2.data().cbegin(); it != tSt2.data().cend(); ++it)
    {
        std::cout << it->first << " " << it->second << std::endl;
    }

    std::string strTest;
    TextFormat::PrintToString(tSt2, &strTest);
    std::cout << strTest << std::endl;

    std::cin.get();
    return 0;
}
message messagetest
{
    ...
    repeated float samples = 6;
    ....
}

for (int j = 0; j < fData.size(); j++)
{
    fMessage.add_samples(fData[j]);    
}
    tnt_deploy::GOODS_INFO infos;
	infos.set_goods_id(123);

	///Serialize to fstream
	std::fstream out("test.dat", std::ios::out | std::ios::binary | std::ios::trunc);
	infos.SerializeToOstream(&out);
	out.close();
	///Serialize to string
	std::string serializeStr;
	infos.SerializePartialToString(&serializeStr);

	///Parse from string
	tnt_deploy::GOODS_INFO infos2;
	infos2.ParseFromString(serializeStr);

	///Parse from string
	tnt_deploy::GOODS_INFO infos3;
	std::fstream in("test.dat", std::ios::in | std::ios::binary);
	if (!infos3.ParseFromIstream(&in))
	{
		UE_LOG(LogTemp, Error, TEXT("Parse error!!!"));
	}
	uint32 g_id = infos3.goods_id();
	UE_LOG(LogTemp, Log, TEXT("From stream goos_id = %s"), *FString::FromInt(g_id));

	uint32 good_id = infos2.goods_id();
	UE_LOG(LogTemp, Log, TEXT("From string goos_id = %s"), *FString::FromInt(good_id));
    Excel_Example data_map;
	auto params = data_map.mutable_fuck_data();
	
	Example item1;
	item1.set_id(1001);
	item1.add_name9(1.0f);
	item1.add_name9(2.0f);
	item1.add_name9(3.0f);
	item1.add_name16("hello");
	item1.add_name16("world");
	item1.add_name16("UE4");
	

	Example item2;
	item2.set_id(1002);
	item2.add_name9(1.0f);
	item2.add_name9(2.0f);
	item2.add_name9(3.0f);
	item2.add_name16("hello");
	item2.add_name16("world");
	item2.add_name16("UE4");
	

	Example item3;
	item3.set_id(1003);
	item3.add_name9(1.0f);
	item3.add_name9(2.0f);
	item3.add_name9(3.0f);
	item3.add_name16("hello");
	item3.add_name16("world");
	item3.add_name16("UE4");
	
	params->insert({ 1, item1 });
	params->insert({ 2, item2 });
	params->insert({ 3, item3 });
	/*(*params)[1001] = item1;
	(*params)[1002] = item2;
	(*params)[1003] = item3;*/

	std::string serializeStr2;
	bool is_serialized = data_map.SerializeToString(&serializeStr2);
	if (!is_serialized)
	{
		UE_LOG(LogTemp, Log, TEXT("Serialized failed"));
		return;
	}
	else
	{
		UE_LOG(LogTemp, Log, TEXT("size ==  %d"), serializeStr2.size());
	}
	Excel_Example parse_map;
	bool is_parsed = parse_map.ParseFromString(serializeStr2);
	if (!is_parsed)
	{
		UE_LOG(LogTemp, Log, TEXT("Parsed failed"));
		return;
	}

	Example exp1 = parse_map.mutable_fuck_data()->at(1);
	int32 id = exp1.id();
	UE_LOG(LogTemp, Log, TEXT("======%d "), id);
	std::string name = exp1.name16(0);
	FString HappyString(name.c_str());
	UE_LOG(LogTemp, Log, TEXT("=====%s"), *HappyString)
	
	Example exp2 = parse_map.mutable_fuck_data()->at(2);
	int32 id2 = exp2.id();
	UE_LOG(LogTemp, Log, TEXT("======%d "), id2);
	std::string name2 = exp2.name16(1);
	FString HappyString2(name2.c_str());
	UE_LOG(LogTemp, Log, TEXT("=====%s"), *HappyString2)

	Example exp3 = parse_map.mutable_fuck_data()->at(3);
	int32 id3 = exp3.id();
	UE_LOG(LogTemp, Log, TEXT("======%d "), id3);
	std::string name3 = exp3.name16(2);
	FString HappyString3(name3.c_str());
	UE_LOG(LogTemp, Log, TEXT("=====%s"), *HappyString3);

UE4表格工具三部曲之一【環境配置】​​​​​​​

UE4表格工具三部曲之二【Protocol Buffer C 源碼編譯】

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章