source: cpp/frams/_demos/serial_test.cpp @ 180

Last change on this file since 180 was 171, checked in by sz, 11 years ago

getObjectTarget is now the recommended way to retrieve object from ExtValue?, can post the standard warning message about missing object

  • Property svn:eol-style set to native
File size: 2.2 KB
Line 
1// This file is a part of the Framsticks GDK.
2// Copyright (C) 2002-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
3// Refer to http://www.framsticks.com/ for further information.
4
5#include <frams/vm/classes/collectionobj.h>
6
7void printIndent(int indent)
8{
9for(int i=0;i<indent;i++)
10        putchar(' ');
11}
12
13struct Trace
14{
15ExtObject object;
16Trace *previous;
17bool isAlreadyVisited(const ExtObject& o)
18        {
19        if (object==o) return true;
20        if (previous) return previous->isAlreadyVisited(o);
21        return false;
22        }
23};
24
25void print(ExtValue* v,int indent=0,Trace *prev_trace=NULL)
26{
27printIndent(indent);
28if (!v)
29        {puts("null"); return;}
30
31switch(v->getType())
32        {
33        case TUnknown: puts("null"); return;
34        case TInt: printf("int:"); break;
35        case TDouble: printf("double:"); break;
36        case TString: printf("string:"); break;
37        case TObj:
38                {
39                Trace next_trace={v->getObject(),prev_trace};
40                printf("%s object:",v->getObject().interfaceName());
41                if (prev_trace && prev_trace->isAlreadyVisited(next_trace.object))
42                        {
43                        printf(" already visited - breaking recursion\n");
44                        return;
45                        }
46                VectorObject *vec=VectorObject::fromObject(v->getObject(),false);
47                if (vec)
48                        {
49                        printf("\n");
50                        for(int i=0;i<vec->data.size();i++)
51                                print((ExtValue*)vec->data(i),indent+3,&next_trace);
52                        return;
53                        }
54                DictionaryObject *dict=DictionaryObject::fromObject(v->getObject(),false);
55                if (dict)
56                        {
57                        printf("\n");
58                        for(HashEntryIterator it(dict->hash);it.isValid();it++)
59                                {
60                                printIndent(indent+3);
61                                printf("key \"%s\"\n",(const char*)it->key);
62                                print((ExtValue*)it->value,indent+6,&next_trace);
63                                }
64                        return;
65                        }
66                }
67        }
68puts((const char*)v->getString());
69}
70
71int main(int argc,char*argv[])
72{
73const char* in= (argc>1) ? argv[1] : "[1,2,3,null,{\"x\":3.4,\"y\":[7,77,777]},4]";
74
75// note: this is not an example of how to read a param field, but only a special case where the input data was
76// previously identified as "serialized" and the "@Serialized:" prefix was already removed
77
78printf("DESERIALIZATION TEST\n"
79       "input string = %s\n",in);
80ExtValue v;
81const char* ret=v.deserialize(in);
82if (ret==NULL)
83        {
84        printf("failed!\n");
85        return 1;
86        }
87else
88        {
89        printf("OK, consumed %d of %d characters\n\n",ret-in,strlen(in));
90        print(&v);
91        return 0;
92        }
93}
Note: See TracBrowser for help on using the repository browser.