C++ serializes open source libraries and can be converted in three lines of code

C++ serializes open source libraries and can be converted in three lines of code

2022-09-30 0 1,637
Resource Number 43917 Last Updated 2025-02-24
¥ 0HKD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

In this issue, we recommend an open source C++ serialize library called FdogSerialize.

C++ serializes open source libraries and can be converted in three lines of code插图

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

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free Source Code C++ serializes open source libraries and can be converted in three lines of code https://ictcoder.com/c-serializes-open-source-libraries-and-can-be-converted-in-three-lines-of-code/

Share free open-source source code

Q&A
  • 1. Automatic: After making an online payment, click the (Download) link to download the source code; 2. Manual: Contact the seller or the official to check if the template is consistent. Then, place an order and make payment online. The seller ships the goods, and both parties inspect and confirm that there are no issues. ICTcoder will then settle the payment for the seller. Note: Please ensure to place your order and make payment through ICTcoder. If you do not place your order and make payment through ICTcoder, and the seller sends fake source code or encounters any issues, ICTcoder will not assist in resolving them, nor can we guarantee your funds!
View details
  • 1. Default transaction cycle for source code: The seller manually ships the goods within 1-3 days. The amount paid by the user will be held in escrow by ICTcoder until 7 days after the transaction is completed and both parties confirm that there are no issues. ICTcoder will then settle with the seller. In case of any disputes, ICTcoder will have staff to assist in handling until the dispute is resolved or a refund is made! If the buyer places an order and makes payment not through ICTcoder, any issues and disputes have nothing to do with ICTcoder, and ICTcoder will not be responsible for any liabilities!
View details
  • 1. ICTcoder will permanently archive the transaction process between both parties and snapshots of the traded goods to ensure the authenticity, validity, and security of the transaction! 2. ICTcoder cannot guarantee services such as "permanent package updates" and "permanent technical support" after the merchant's commitment. Buyers are advised to identify these services on their own. If necessary, they can contact ICTcoder for assistance; 3. When both website demonstration and image demonstration exist in the source code, and the text descriptions of the website and images are inconsistent, the text description of the image shall prevail as the basis for dispute resolution (excluding special statements or agreements); 4. If there is no statement such as "no legal basis for refund" or similar content, any indication on the product that "once sold, no refunds will be supported" or other similar declarations shall be deemed invalid; 5. Before the buyer places an order and makes payment, the transaction details agreed upon by both parties via WhatsApp or email can also serve as the basis for dispute resolution (in case of any inconsistency between the agreement and the description of the conflict, the agreement shall prevail); 6. Since chat records and email records can serve as the basis for dispute resolution, both parties should only communicate with each other through the contact information left on the system when contacting each other, in order to prevent the other party from denying their own commitments. 7. Although the probability of disputes is low, it is essential to retain important information such as chat records, text messages, and email records, in case a dispute arises, so that ICTcoder can intervene quickly.
View details
  • 1. As a third-party intermediary platform, ICTcoder solely protects transaction security and the rights and interests of both buyers and sellers based on the transaction contract (product description, agreed content before the transaction); 2. For online trading projects not on the ICTcoder platform, any consequences are unrelated to this platform; regardless of the reason why the seller requests an offline transaction, please contact the administrator to report.
View details

Related Source code

ICTcoder Customer Service

24-hour online professional services