In this issue, we recommend an open source C++ serialize library called FdogSerialize.
FStruct is an open source library for serialization in C++. It uses a non-invasive way without modifying the original structure. Currently, it supports serialization of base types, base type arrays, structures, and data types such as vector, list, map, etc. Supports JSON and XML data formats, aliases, ignores fields, and can be converted in at least three lines of code.
class=”pgc-h-arrow-right” data-track=”188″>
first stage
- supports conversion between base types and Json
- Support array of base types and json conversion
- supports composition of base types into structure types and Json conversion
- supports Json conversion from struct to struct type
- Support vector type and json conversion
- Support list type and json conversion
- Support map type and json conversion
- Support set type and json conversion
Stage 2
- Support STL and custom type multi-layer nesting
- Optimize the interface used in the first phase, integrate multiple interfaces into one, and facilitate the invocation of
Stage 3
- Supports correct formatting of json strings (under development)
- Support to get whether a field exists (under development)
- supports getting the value of a field without serializing it first (in development)
Stage 4
- supports required and optional fields. When the required field has no value, error will be reported (defined as pointer type, it is optional field) (under development)
- Support XML data format conversion (under development)
Miscellaneous support
-
- aliases
are supported
- Support field ignore
- supports case ignoring
- If the supported field is empty, no serialization will be performed (under development)
- Support fuzzy conversion (in development)
class=”pgc-h-arrow-right”>
Based on C++ base types, FdogSerialize supports 16 types, as long as the most basic type in your structure falls within these 16 ranges, you can complete the conversion.
|
Base type |
|
|
Base type |
|
1 |
bool |
Boolean type |
9 |
unsigned int |
无符号整型 |
2 |
char |
Unsigned character |
10 |
long |
long integer |
3 |
unsigned char |
Unsigned character |
11 |
unsigned long |
unsigned long integer |
4 |
**char *** |
character pointer (string type) |
12 |
long long |
|
5 |
short |
Short integer |
13 |
unsigned long long |
unsigned long integer |
6 |
unsigned short |
Unsigned short integer |
14 |
float |
Single-precision floating-point type |
7 |
int |
Integer |
15 |
double |
double-schedule floating-point type |
8 |
unsigned int |
Unsigned integer |
16 |
long double |
Long dual-schedule floating-point type |
Although the school structure contains one basic type and one custom type, the bottom layer of the custom type is essentially the basic type. Therefore, the school structure can be parsed normally, in fact any type can be parsed.
struct headmaster{
char * name;
int age;
};
struct school{
char * schoolName;
headmaster info;
};
class=”pgc-h-arrow-right”>
Serialization and deserialization each provide three functions:
call function |
|
FSerialize(string & json_, T & object_, string name = “”) |
Base type and structure type call |
FSerializeA(string & json_, T & object_, string name = “”) |
array, vector, list call |
FSerializeS(string & json_, T & object_, string name = “”) |
set, map type call |
deserialize call function |
|
FSerialize( T & object_, string & json_, string name = “”) |
Base type and structure type call |
FSerializeA( T & object_, string & json_, string name = “”) |
Array, vector, list type call |
FSerializeS( T & object_, string & json_, string name = “”) |
set, map type call |
1 Base type serialization
#include "fdogserialize.h" // Add header file required for serialization
int main()
{
int value = 10;
string json_;
// Convert value to json format data, the second type is recommended
FdogSerialize::Instance()->FSerialize(json_, value); //json value is "{10}"
FdogSerialize::Instance()->FSerialize(json_, value, "value"); //json value is "{"value":10}"
// To convert JSON-formatted data to value, make sure that json_ is a correctly formatted string
FdogSerialize::Instance()->FDesSerialize(value, json_);
return 0;
}
2 Base type array serialization
#include "fdogserialize.h" // Adds the header file needed for serialization
int main()
{
int valueArray[5] = {1,2,3,4,5};
string json_;
// Convert valueArray to json format. The second type is recommended
FdogSerialize::Instance()->FSerialize(json_, value); //json value is "{[1,2,3,4,5]}"
FdogSerialize::Instance()->FSerialize(json_, value, "value"); //json The value is "{"valueArray":[1,2,3,4,5]}"
// To convert json data to value, ensure that json_ is a string in the correct format
FdogSerialize::Instance()->FDesSerialize(value, json_);
}
3 Serialization of the structure consisting of the underlying type
#include "fdogserialize.h" // Adds the header file needed for serialization
// Customize the base type structure
struct student{
char * name;
int age;
};
int main()
{
REGISTEREDMEMBER(student, name, age); // You need to register a custom type. The first parameter is the name of the custom structure, followed by the member name
struct stu;
stu.name = "pieddog Fdog.";
stu.age = 22;
string json_;
// Converts the value to json format data
FdogSerialize::Instance()->FSerialize(json_, value, "stu"); //json value is"{"stu":{"name":"花狗Fdog","age":22}}"
// To convert json data to value, ensure that json_ is a string in the correct format
FdogSerialize::Instance()->FDesSerialize(value, json_);
}
4 vector serialization
#include "fdogserialize.h" //Add the header file needed for serialization
//Custom base type structs
struct student{
char * name;
int age;
};
int main()
{
REGISTEREDMEMBER(student, name, age); //You need to register a custom type. The first parameter is the name of the custom structure, followed by the member name
vector<student> stu;
struct stu_1;
stu_1.name = "pieddog Fdog";
stu_1.age = 22;
struct stu_2;
stu_2.name = "Black Dog Fdog";
stu_2.age = 23;
stu.push_back(stu_1);
stu.push_back(stu_2);
string json_;
//Convert the value to json format data
FdogSerialize::Instance()->FSerializeA(json_, stu, "stu");
//json值为"{"stu":[{"name":"pieddog Fdog","age":22},{"name":"Black Dog Fdog","age":23}]}"
//To convert json format data to value, ensure that json_ is a correctly formatted string
FdogSerialize::Instance()->FDesSerializeA(value, json_);
}
5 list Serialization of type
#include "fdogserialize.h" //Add the header file needed for serialization
//Custom base type structs
struct student{
char * name;
int age;
};
int main()
{
REGISTEREDMEMBER(student, name, age); //You need to register a custom type. The first parameter is the name of the custom structure, followed by the member name
list<student> stu;
struct stu_1;
stu_1.name = "pieddog Fdog";
stu_1.age = 22;
struct stu_2;
stu_2.name = "Black Dog Fdog";
stu_2.age = 23;
stu.push_back(stu_1);
stu.push_back(stu_2);
string json_;
//Convert the value to json format data
FdogSerialize::Instance()->FSerializeA(json_, stu, "stu");
//json value is"{"stu":[{"name":"pieddog Fdog","age":22},{"name":"Black Dog Fdog","age":23}]}"
//To convert json format data to value, ensure that json_ is a correctly formatted string
FdogSerialize::Instance()->FDesSerializeA(value, json_);
}
6 map Serialization of type
#include "fdogserialize.h" //Add the header file needed for serialization
//Custom base type structs
struct student{
char * name;
int age;
};
int main()
{
REGISTEREDMEMBER(student, name, age); //You need to register a custom type. The first parameter is the name of the custom structure, followed by the member name
vector<student> stu;
struct stu_1;
stu_1.name = "pieddog Fdog";
stu_1.age = 22;
struct stu_2;
stu_2.name = "Black Dog Fdog";
stu_2.age = 23;
stu.push_back(stu_1);
stu.push_back(stu_2);
string json_;
//Convert the value to json format data
FdogSerialize::Instance()->FSerializeA(json_, stu, "stu");
//json value is"{"stu":[{"name":"pieddog Fdog","age":22},{"name":"Black Dog Fdog","age":23}]}"
//To convert json format data to value, ensure that json_ is a correctly formatted string
FdogSerialize::Instance()->FDesSerializeA(value, json_);
}
7 Necessary description
//You need to add a nested structure under the macro Serialize_type_judgment_all definition
#define Serialize_type_judgment_all\
Serialize_type_judgment(student)
//Add in turn
// Needs to add nested structures under the macro Serialize_type_judgment_all definition
#define DesSerialize_type_judgment_all\
DesSerialize_type_judgment(student)
//Add in turn
//Two macro definition guidelines: Use this custom type as a member of another custom type
Miscellaneous support
Miscellaneous function support |
Description |
setAliasName(string Type, string memberName, string AliasName) |
Using aliases |
setIgnoreField(string Type, string memberName) |
Ignore field |
setIgnoreLU(string Type, string memberName) |
Ignore case |
setFuzzy(string Type) |
Fuzzy conversion |
1 Support aliases
#include "fdogserialize.h" // Add header files for serialization
// Custom base type struct
struct student{
char * name;
int age;
};
int main()
{
REGISTEREDMEMBER(student, name, age); // To register a custom type, the first parameter is the name of the custom structure, followed by the member name
FdogSerialize::Instance()-> setAliasName("student", "name", "Aliasname");
// The first parameter is the type, the second parameter is the original name, and the third parameter is an alias
// In addition, you can also use setAliasNameAll to set aliases for multiple parameters
struct stu;
stu.name = "pieddog Fdog";
stu.age = 22;
string json_;
FdogSerialize::Instance()-> FSerialize(json_, value); < span class = "HLJS - the comment" > / / json values for "{{" Aliasname" : "flower dog Fdog", "age" : 22}} "< / span >
}
2 Support fields ignore
#include "fdogserialize.h" // Add header files for serialization
// Custom base type struct
struct student{
char * name;
int age;
};
int main()
{
REGISTEREDMEMBER(student, name, age); // To register a custom type, the first parameter is the name of the custom structure, followed by the member name
FdogSerialize::Instance()-> setIgnoreField("student", "name");
// The first parameter is the type, the second parameter is the field to be ignored
// Alternatively, you can also use setIgnoreFieldAll to set multiple ignored fields
struct stu;
stu.name = "pieddog Fdog";
stu.age = 22;
string json_;
FdogSerialize::Instance()-> FSerialize(json_, value); < span class = "HLJS - the comment" > / / json values for "{{" age" : 22}} "< / span >
}
3 Case ignoring
When converting json to an object, if the case of the key value in json is different from that of the member name in the object, you can set the case to be ignored.
#include "fdogserialize.h" // Add header files needed for serialization
// Custom base type struct
struct student{
char * name;
int age;
};
int main()
{
REGISTEREDMEMBER(student, name, age);
struct stu;
FdogSerialize::Instance()->setIgnoreLU("student", "name");
string stu_json = "{\"Name\":\"yujing\", \"AGE\":21}";
FdogSerialize::Instance()->FDesSerialize(json_, value);
}
4 Support fuzzy conversion
If the json and the key value in the object are different, it will be matched according to the blur level after being enabled.
—END—
Open Source protocol: Apache2.0