1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include <frams/vm/classes/collectionobj.h> |
---|
6 | |
---|
7 | void printIndent(int indent) |
---|
8 | { |
---|
9 | for(int i=0;i<indent;i++) |
---|
10 | putchar(' '); |
---|
11 | } |
---|
12 | |
---|
13 | struct Trace |
---|
14 | { |
---|
15 | ExtObject object; |
---|
16 | Trace *previous; |
---|
17 | bool isAlreadyVisited(const ExtObject& o) |
---|
18 | { |
---|
19 | if (object==o) return true; |
---|
20 | if (previous) return previous->isAlreadyVisited(o); |
---|
21 | return false; |
---|
22 | } |
---|
23 | }; |
---|
24 | |
---|
25 | void print(ExtValue* v,int indent=0,Trace *prev_trace=NULL) |
---|
26 | { |
---|
27 | printIndent(indent); |
---|
28 | if (!v) |
---|
29 | {puts("null"); return;} |
---|
30 | |
---|
31 | switch(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 | } |
---|
68 | puts((const char*)v->getString()); |
---|
69 | } |
---|
70 | |
---|
71 | int main(int argc,char*argv[]) |
---|
72 | { |
---|
73 | const 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 | |
---|
78 | printf("DESERIALIZATION TEST\n" |
---|
79 | "input string = %s\n",in); |
---|
80 | ExtValue v; |
---|
81 | const char* ret=v.deserialize(in); |
---|
82 | if (ret==NULL) |
---|
83 | { |
---|
84 | printf("failed!\n"); |
---|
85 | return 1; |
---|
86 | } |
---|
87 | else |
---|
88 | { |
---|
89 | printf("OK, consumed %d of %d characters\n\n",ret-in,strlen(in)); |
---|
90 | print(&v); |
---|
91 | return 0; |
---|
92 | } |
---|
93 | } |
---|