序列化与反序列化 代码总结

来源:互联网 发布:java script实例 编辑:程序博客网 时间:2024/05/23 16:22


各位亲 有时间可以去看看我的  “金骏家居淘宝店” http://jinjun1688.taobao.com/shop/view_shop.htm?tracelog=twddp 买时说明在我的博客看到有优惠哦 还有意外礼品赠送  真正的程序员淘宝店

#import "ViewController.h"

#import "test.pb.h"

#import "File1.pb.h"

#import "File3.pb.h"

#import "File.pb.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad

{

    [superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    

   Person pp;

    pp.set_id(99);

    pp.set_name("name");

    pp.add_type(0);

//    pp.set_type(0, 0);

//    pp.set_type(1, 1);

    pp.add_type(1);

    

//    Person_PhoneNumber ppn;

//    ppn.set_number("124567");

//    ppn.set_email(Person_temp_START);

//    

//    pp.add_phone();

    

   std::string str;

    pp.SerializeToString(&str);

    

   NSLog(@"str == %s", str.data());

    

    

   Person ppt;

    ppt.ParseFromString(str);

    

   NSLog(@"%d,,%s", ppt.id(), pp.name().data());

    


   for (int i =0 ; i < ppt.type_size(); i++) {

       NSLog(@"%lld", ppt.type(i));

    }

    NSLog(@"----------------");

   Person1 pb;

    pb.set_id_number(01);

    pb.set_name_number("cg");

    pb.add_type_number("ck");

    pb.add_type_number("dk");

    

   std::string ptr;

    pb.SerializeToString(&ptr);

   NSLog(@"ptr=%s",ptr.data());

    

   Person1 pts;

    pts.ParseFromString(ptr);

   NSLog(@"%d,,%s,,%s",pts.id_number(),pts.name_number().data(),pts.type_number(0).data());

    

//    FILE *fp;

//    fp=fopen("/Users/chengen/Documents", "w");

//    if(fp==NULL)

//        printf("打开文件失败\n");

//    fprintf(fp, str.data());

//    fclose(fp);

//    fp=fopen("a.txt", "r");

//    if(fp==NULL)

//    {

//        printf("错误");

//        exit(1);

//    }

//    fclose(fp);

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

    NSLog(@"Get document path:%@",[pathsobjectAtIndex:0]);

   NSString *fileName=[[pathsobjectAtIndex:0]

                        stringByAppendingPathComponent:@"myFiles"];

//    NSString *coutent=@"a";

//    NSData *coutentData=[coutent dataUsingEncoding:NSASCIIStringEncoding];

//    if ([coutentData writeToFile:fileName atomically:YES]) {

//        NSLog(@">>work ok");

//    }

//    NSString *file=[[paths objectAtIndex:0]

//                        stringByAppendingPathComponent:@"myfile"];

//

////    NSString *content=@"hello";

//    coutent = @"aaaa";

//    NSData *contentData=[coutent dataUsingEncoding:NSUTF8StringEncoding];

//    if ([contentData writeToFile:fileName atomically:YES]) {

//        NSLog(@">>write ok");

//    }

    

    constchar* cfilename = [fileNamecStringUsingEncoding:NSUTF8StringEncoding];

   FILE *fp;

    fp =fopen(cfilename,"w");

   fprintf(fp,"%s", ptr.data());

   fflush(fp);

   fclose(fp);

    

      fp =fopen(cfilename,"r");

   fseek(fp,0L,2);

   unsignedlong filesize =ftell(fp);

   NSLog(@"filesize == %d", (int)filesize);

  char tmp[1024];

   memset(tmp,0,1024);

   rewind(fp);

   fread(tmp,1, filesize, fp);

   fclose(fp);


   NSLog(@"%s|%d", tmp, (int)strlen(tmp));

    

   Person1 pts1;

    pts1.ParseFromString(std::string(tmp,strlen(tmp)));

   NSLog(@"%d,,%s,,%s",pts.id_number(),pts.name_number().data(),pts.type_number(0).data());


//    char ch;

//    while ((ch=fgetc(fp))!=EOF) {

//        putchar(ch);

//    }

//    char tmp[1024];

//    unsigned long filesize = ftell(fp);

//    fread(tmp, 1,(int)filesize+1,fp);

//     printf("%s\n", tmp);

//    

//    

   fclose(fp);

    NSLog(@"________________");

   lm::helloworld msg1;

    msg1.set_id(101);

    msg1.set_str("hello");

    msg1.set_opt(102);

    

    

   std::string wyt;

    msg1.SerializeToString(&wyt);

   NSLog(@"wyt == %s",wyt.data());

    

    msg1.ParseFromString(wyt);

   NSLog(@"%d,%s,%d",msg1.id(),msg1.str().data(),msg1.opt());

      NSLog(@"________________");

//    NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

//    NSLog(@"GET document path:%@",[path objectAtIndex:0]);

//    NSString *filename=[[path objectAtIndex:0] stringByAppendingPathComponent:@"myfils"];

    

    

    NSLog(@"-------------------");

   lm1::Person msg2;

    msg2.set_id_str(1);

    //NSLog(@"%d", msg2.id_num_size());

    msg2.add_id_num(10);

//    msg2.add_id_num(2);

//    msg2.add_id_num(3);

    msg2.set_id_num(0,223);

//    NSLog(@"%d", msg2.id_num_size());

//    NSLog(@"%d", msg2.id_num(0));

    msg2.set_id(31);

    

//    int a[2];

//    a[0]=msg2.id_num(0);

//    a[1]=msg2.id_str();

//    a[2]=msg2.id();

//    

//    int ptr_size=sizeof(a);

//    NSLog(@"%d",ptr_size);

    

    

    //提前获取序列化所占空间得大小,从而避免多次分配造成的性能开销

   int length=msg2.ByteSize();

   NSLog(@"lenght==%d",length);

    

   std::string pto;

    msg2.SerializeToString(&pto);

   NSLog(@"pto==%s",pto.data());

    

   int P_size=strlen( (constchar*)pto.data());

   NSLog(@"%d",P_size);

    

//    NSArray *path=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

//    NSLog(@"get Document path",[path objectAtIndex:0]);

//    NSString *filenames=[[path objectAtIndex:0] stringByAppendingPathComponent:@"filenames"];

//    const char* file1=[filenames cStringUsingEncoding:NSUTF8StringEncoding];

    msg2.ParseFromString(pto);

   NSLog(@"pto==%d,,%d,,%d",msg2.id_num(0),msg2.id_str(),msg2.id());

   int a[3];

    a[0]=msg2.id_num(0);

    a[1]=msg2.id_str();

    a[2]=msg2.id();

    

   int ptr_size=sizeof(a);

   NSLog(@"%d",ptr_size);

 //   NSLog(@"%d",msg2.id());

    

   int length1=msg2.ByteSize();

   NSLog(@"pb_size==%d",length1);

    

}


- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end