Protobuf common serialization/deserialization API Goto

Source: Internet
Author: User

http://blog.csdn.net/sealyao/article/details/6940245

1. Serialization and deserialization API for C arrays

[CPP]View Plaincopy
  1. Serialization and serialization API for C arrays
  2. BOOL Parsefromarray (const void* data, int size);
  3. BOOL Serializetoarray (void* data, int size) const;
  4. Use
  5. void Set_people ()
  6. {
  7. Wp.set_name ("Sealyao");
  8. WP.SET_ID (123456);
  9. Wp.set_email ("[email protected]");
  10. Wp. Serializetoarray (parray,256);
  11. }
  12. void Get_people ()
  13. {
  14. Rap. Parsefromarray (parray,256);
  15. cout << "Get people from Array:" << Endl;
  16. cout << "\ t Name:" <<rap.name () << Endl;
  17. cout << "\ t Id:" << rap.id () << Endl;
  18. cout << "\ t email:" << rap.email () << Endl;
  19. }

2. Serialization and deserialization API for C + + string

[CPP]View Plaincopy
  1. C++string serialization and serialization API
  2. BOOL Serializetostring (string* output) const;
  3. BOOL Parsefromstring (const string& data);
  4. Use:
  5. void Set_people ()
  6. {
  7. Wp.set_name ("Sealyao");
  8. WP.SET_ID (123456);
  9. Wp.set_email ("[email protected]");
  10. Wp. Serializetostring (&pstring);
  11. }
  12. void Get_people ()
  13. {
  14. Rsp. Parsefromstring (pstring);
  15. cout << "Get people from String:" << Endl;
  16. cout << "\ t Name:" <<rsp.name () << Endl;
  17. cout << "\ t Id:" << rsp.id () << Endl;
  18. cout << "\ t email:" << rsp.email () << Endl;
  19. }

3. File descriptor serialization and deserialization API

[CPP]View Plaincopy
  1. //File descriptor serialization and serialization API
  2. bool Serializetofiledescriptor (int file_descriptor) const;
  3. bool Parsefromfiledescriptor (int file_descriptor);
  4. //use:
  5. void Set_people ()
  6. {
  7. FD = open (path,o_creat| o_trunc| o_rdwr,0644);
  8. if (fd <= 0) {
  9. Perror ("open");
  10. Exit (0);
  11. }
  12. Wp.set_name ("Sealyaog");
  13. WP.SET_ID (123456);
  14. Wp.set_email ("[email protected]");
  15. Wp. Serializetofiledescriptor (FD);
  16. Close (FD);
  17. }
  18. void Get_people ()
  19. {
  20. FD = open (path,o_rdonly);
  21. if (fd <= 0) {
  22. Perror ("open");
  23. Exit (0);
  24. }
  25. Rp. Parsefromfiledescriptor (FD);
  26. Std::cout << "Get people from FD:" << Endl;
  27. Std::cout << "\ t Name:" <<rp.name () << Endl;
  28. Std::cout << "\ t Id:" << rp.id () << Endl;
  29. Std::cout << "\ t email:" << rp.email () << Endl;
  30. Close (FD);
  31. }

4. C + + stream serialization and deserialization API

[CPP]View Plaincopy
    1. C + + stream serialization/deserialization API
    2. BOOL Serializetoostream (ostream* output) const;
    3. BOOL Parsefromistream (istream* input);
    4. Use:
    5. void Set_people ()
    6. {
    7. FStream FS (path,ios::out|ios::trunc|ios::binary);
    8. Wp.set_name ("Sealyaog");
    9. WP.SET_ID (123456);
    10. Wp.set_email ("[email protected]");
    11. Wp. Serializetoostream (&FS);
    12. Fs.close ();
    13. Fs.clear ();
    14. }
    15. void Get_people ()
    16. {
    17. FStream FS (path,ios::in|ios::binary);
    18. Rp. Parsefromistream (&FS);
    19. Std::cout << "\ t Name:" <<rp.name () << Endl;
    20. Std::cout << "\ t Id:" << rp.id () << Endl;
    21. Std::cout << "\ t email:" << rp.email () << Endl;
    22. Fs.close ();
    23. Fs.clear ();
    24. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.