7 static void UnusedFunction()
10 a.OnGetDataFromString(null);
11 a.OnGetString(null, 0, 0);
14 extern int __ecereVMethodID_class_OnGetDataFromString;
15 extern int __ecereVMethodID_class_OnGetString;
16 extern int __ecereVMethodID_class_OnFree;
19 public enum JSONResult { syntaxError, success, typeMismatch, noItem };
21 public enum SetBool : uint
23 unset, false, true /*; // Syntax error! */
25 /*public property bool // NOT WORKING!
27 set { return value ? true : false; }
28 get { return (this == true); }
33 public class JSONParser
41 while(!f.Eof() && (!ch || ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'))
47 JSONResult GetValue(Class type, DataValue value)
49 JSONResult result = syntaxError;
55 result = GetString(&string);
59 if(type && (!strcmp(type.name, "String") || !strcmp(type.dataTypeString, "char *")))
63 else if(type && type.type == enumClass)
65 if(((bool (*)(void *, void *, const char *))(void *)type._vTbl[__ecereVMethodID_class_OnGetDataFromString])(type, &value.i, string))
68 result = typeMismatch;
71 else if(type && (prop = eClass_FindProperty(type, "String", type.module)))
73 // TOFIX: Add more conversion property support... Expecting void * compatible here
74 value.p = ((void *(*)())(void *)prop.Set)(string);
78 else if(type && (type.type == structClass))
80 if(((bool (*)(void *, void *, const char *))(void *)type._vTbl[__ecereVMethodID_class_OnGetDataFromString])(type, value.p, string))
83 result = typeMismatch;
89 result = typeMismatch;
96 result = GetArray(type, &array);
98 if(type && eClass_IsDerived(type, class(Container)))
107 result = typeMismatch;
110 else if(ch == '-' || isdigit(ch))
112 result = GetNumber(type, value);
117 result = GetObject(type, &object);
120 if(type && (type.type == normalClass || type.type == structClass || type.type == noHeadClass || type.type == bitClass))
126 result = typeMismatch;
127 ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, object);
135 while(c < sizeof(buffer)-1 && isalpha(ch))
138 if(!f.Getc(&ch)) break;
145 if(!strcmp(type.name, "bool"))
147 if(!strcmpi(buffer, "false")) value.i = 0;
148 else if(!strcmpi(buffer, "true")) value.i = 1;
150 result = typeMismatch;
152 else if(!strcmp(type.name, "SetBool"))
154 if(!strcmpi(buffer, "false")) value.i = SetBool::false;
155 else if(!strcmpi(buffer, "true")) value.i = SetBool::true;
157 result = typeMismatch;
159 else if(!strcmpi(buffer, "null"))
164 result = typeMismatch;
167 result = typeMismatch;
169 else if(ch == '}' || ch == ']')
174 JSONResult GetArray(Class type, Container * array)
176 JSONResult result = syntaxError;
182 *array = eInstance_New(type);
187 Class arrayType = null;
188 JSONResult itemResult;
190 if(eClass_IsDerived(type, class(Container)))
192 arrayType = type.templateArgs[0].dataTypeClass;
195 itemResult = GetValue(arrayType, value);
196 if(itemResult == success)
198 // TODO: Verify the matching between template type and uint64
200 if(arrayType.type == structClass)
202 t = (uint64) value.p;
204 else if(arrayType == class(double) || !strcmp(arrayType.dataTypeString, "double"))
206 t = *(uint64 *)&value.d;
208 else if(arrayType == class(float) || !strcmp(arrayType.dataTypeString, "float"))
210 t = *(uint *)&value.f;
212 else if(arrayType.typeSize == sizeof(int64) || !strcmp(arrayType.dataTypeString, "int64")
213 !strcmp(arrayType.dataTypeString, "unsigned int64") || !strcmp(arrayType.dataTypeString, "uint64"))
217 else if(arrayType.typeSize == sizeof(int) || !strcmp(arrayType.dataTypeString, "int") ||
218 !strcmp(arrayType.dataTypeString, "unsigned int") || !strcmp(arrayType.dataTypeString, "uint"))
222 else if(arrayType.typeSize == sizeof(short int) || !strcmp(arrayType.dataTypeString, "short") ||
223 !strcmp(arrayType.dataTypeString, "unsigned short") || !strcmp(arrayType.dataTypeString, "uint16") ||
224 !strcmp(arrayType.dataTypeString, "int16"))
228 else if(arrayType.typeSize == sizeof(byte) || !strcmp(arrayType.dataTypeString, "char") ||
229 !strcmp(arrayType.dataTypeString, "unsigned char") || !strcmp(arrayType.dataTypeString, "byte"))
237 ((void *(*)(void *, uint64))(void *)array->Add)(*array, t);
241 if(itemResult == typeMismatch)
244 PrintLn("Warning: Incompatible value for array value, expected ", (String)arrayType.name);
246 else if(itemResult == noItem)
252 if(result != syntaxError)
254 if(ch != ']' && ch != ',')
264 result = syntaxError;
272 JSONResult GetString(String * string)
274 JSONResult result = syntaxError;
275 Array<char> buffer { minAllocSize = 256 };
276 bool escaped = false;
284 if(ch == '\\' && !escaped)
290 if(ch == 'b') ch = '\b';
291 else if(ch == 'f') ch = '\f';
292 else if(ch == 'n') ch = '\n';
293 else if(ch == 'r') ch = '\r';
294 else if(ch == 't') ch = '\t';
311 if(buffer.minAllocSize < buffer.count)
312 buffer.minAllocSize *= 2;
316 *string = CopyString(buffer.array);
324 public JSONResult GetObject(Class objectType, void ** object)
326 JSONResult result = syntaxError;
332 if(objectType && (objectType.type == noHeadClass || objectType.type == normalClass))
334 *object = eInstance_New(objectType);
338 *object = eSystem_New(objectType.typeSize);
345 if(GetString(&string))
348 DataMember member = null;
349 Property prop = null;
354 string[0] = (char)tolower(string[0]);
355 member = eClass_FindDataMember(objectType, string, objectType.module, null, null);
358 type = eSystem_FindClass(__thisModule, member.dataTypeString);
360 type = eSystem_FindClass(__thisModule.application, member.dataTypeString);
364 prop = eClass_FindProperty(objectType, string, objectType.module);
367 type = eSystem_FindClass(__thisModule, prop.dataTypeString);
369 type = eSystem_FindClass(__thisModule.application, prop.dataTypeString);
372 PrintLn("Warning: member ", string, " not found in class ", (String)objectType.name);
375 // Find Member in Object Class
379 if(type && type.type == structClass)
381 value.p = (byte *)*object + member._class.offset + member.offset;
387 JSONResult itemResult = GetValue(type, value);
388 if(itemResult != syntaxError)
394 PrintLn("warning: Unresolved data type ", member ? (String)member.dataTypeString : (String)prop.dataTypeString);
396 else if(itemResult == success)
401 // TOFIX: How to swiftly handle classes with base data type?
402 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
404 *(double *)((byte *)*object + member._class.offset + member.offset) = value.d;
406 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
408 *(float *)((byte *)*object + member._class.offset + member.offset) = value.f;
410 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64")
411 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
413 *(uint64 *)((byte *)*object + member._class.offset + member.offset) = value.ui64;
415 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
416 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
418 int * ptr = (int *)((byte *)*object + member._class.offset + member.offset);
419 // TODO: Fix Me Up 64 bit pointer here
420 if(eClass_IsDerived(type, class(Container)) && *ptr)
422 Container container = (Container)*ptr;
428 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
429 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
430 !strcmp(type.dataTypeString, "int16"))
432 *(short *)((byte *)*object + member._class.offset + member.offset) = value.s;
434 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
435 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
437 *(char *)((byte *)*object + member._class.offset + member.offset) = value.c;
439 else if(type.type != structClass)
441 *(void **)((byte *)*object + member._class.offset + member.offset) = value.p;
444 else if(prop && prop.Set)
446 if(!strcmp(type.dataTypeString, "char *"))
448 ((void (*)(void *, void *))(void *)prop.Set)(*object, value.p);
451 // TOFIX: How to swiftly handle classes with base data type?
452 else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
454 ((void (*)(void *, double))(void *)prop.Set)(*object, value.d);
456 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
458 ((void (*)(void *, float))(void *)prop.Set)(*object, value.f);
460 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64")
461 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
463 ((void (*)(void *, uint64))(void *)prop.Set)(*object, value.ui64);
465 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
466 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
468 ((void (*)(void *, int))(void *)prop.Set)(*object, value.i);
470 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
471 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
472 !strcmp(type.dataTypeString, "int16"))
474 ((void (*)(void *, short))(void *)prop.Set)(*object, value.s);
476 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
477 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
479 ((void (*)(void *, char))(void *)prop.Set)(*object, value.c);
483 ((void (*)(void *, void *))(void *)prop.Set)(*object, value.p);
489 PrintLn("Warning: Incompatible value for ", member ? (String)member.name : (String)prop.name,
490 ", expected ", member ? (String)member.dataTypeString : (String)prop.dataTypeString);
496 result = syntaxError;
500 result = syntaxError;
511 result = syntaxError;
519 JSONResult GetNumber(Class type, DataValue value)
521 JSONResult result = syntaxError;
524 while(c < sizeof(buffer)-1 && (ch == '-' || ch == '.' || tolower(ch) == 'e' || ch == '+' || isdigit(ch)))
527 if(!f.Getc(&ch)) break;
530 //if(strchr(buffer, '.'))
532 // TOFIX: How to swiftly handle classes with base data type?
533 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
535 value.d = strtod(buffer, null);
538 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
540 value.f = (float)strtod(buffer, null);
543 // TOFIX: int64 looks for class long long?
544 //else if(type == class(int64) || !strcmp(type.dataTypeString, "int64"))
545 else if(!strcmp(type.dataTypeString, "int64"))
547 value.i64 = strtol(buffer, null, 10); // TOFIX: 64 bit support
550 else if(type == class(uint64) || !strcmp(type.dataTypeString, "uint64"))
552 value.ui64 = strtol(buffer, null, 10); // TOFIX: 64 bit support
557 value.i = strtol(buffer, null, 10);
564 bool WriteArray(File f, Class type, Container array, int indent)
571 Iterator it { array };
572 Class arrayType = type.templateArgs[0].dataTypeClass;
580 uint64 t = ((uint64(*)(void *, void *))(void *)array.GetData)(array, it.pointer);
587 // TODO: Verify the matching between template type and uint64
588 if(arrayType.type == structClass)
592 else if(arrayType == class(double) || !strcmp(arrayType.dataTypeString, "double"))
594 value.d = *(double *)&t;
596 else if(arrayType == class(float) || !strcmp(arrayType.dataTypeString, "float"))
598 value.f = *(float *)&t;
600 else if(arrayType.typeSize == sizeof(int64) || !strcmp(arrayType.dataTypeString, "int64")
601 !strcmp(arrayType.dataTypeString, "unsigned int64") || !strcmp(arrayType.dataTypeString, "uint64"))
605 else if(arrayType.typeSize == sizeof(int) || !strcmp(arrayType.dataTypeString, "int") ||
606 !strcmp(arrayType.dataTypeString, "unsigned int") || !strcmp(arrayType.dataTypeString, "uint"))
610 else if(arrayType.typeSize == sizeof(short int) || !strcmp(arrayType.dataTypeString, "short") ||
611 !strcmp(arrayType.dataTypeString, "unsigned short") || !strcmp(arrayType.dataTypeString, "uint16") ||
612 !strcmp(arrayType.dataTypeString, "int16"))
616 else if(arrayType.typeSize == sizeof(byte) || !strcmp(arrayType.dataTypeString, "char") ||
617 !strcmp(arrayType.dataTypeString, "unsigned char") || !strcmp(arrayType.dataTypeString, "byte"))
625 for(i = 0; i<indent; i++) f.Puts(" ");
626 WriteValue(f, arrayType, value, indent);
630 for(i = 0; i<indent; i++) f.Puts(" ");
638 bool WriteNumber(File f, Class type, DataValue value, int indent)
641 bool needClass = false;
643 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
644 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.d, buffer, 0, &needClass);
645 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
646 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.f, buffer, null, &needClass);
647 else if(!strcmp(type.dataTypeString, "int64"))
648 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.i64, buffer, null, &needClass);
649 else if(!strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64") || type.typeSize == sizeof(int64))
650 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.ui64, buffer, null, &needClass);
651 else if(!strcmp(type.dataTypeString, "int"))
652 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.i, buffer, null, &needClass);
653 else if(!strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint") || type.typeSize == sizeof(int))
654 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.ui, buffer, null, &needClass);
655 else if(!strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "int16"))
656 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.s, buffer, null, &needClass);
657 else if(!strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") || type.typeSize == sizeof(short int))
658 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.us, buffer, null, &needClass);
659 else if(!strcmp(type.dataTypeString, "char"))
660 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.c, buffer, null, &needClass);
661 else if(!strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte") || type.typeSize == sizeof(byte))
662 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.uc, buffer, null, &needClass);
667 bool WriteValue(File f, Class type, DataValue value, int indent)
669 if(!strcmp(type.name, "String") || !strcmp(type.dataTypeString, "char *"))
672 //if(strchr(value.p, '\"') || strchr(value.p, '\\'))
677 char * string = value.p;
696 else if(b == sizeof(buffer)-2 || !ch)
699 if(ch) buffer[b] = 0;
712 else if(!strcmp(type.name, "bool"))
719 else if(!strcmp(type.name, "SetBool"))
721 if(value.i == SetBool::true)
723 else if(value.i == SetBool::false)
728 else if(type.type == enumClass)
731 WriteNumber(f, type, value, indent);
734 else if(eClass_IsDerived(type, class(Container)))
736 WriteArray(f, type, value.p, indent);
738 else if(type.type == normalClass || type.type == noHeadClass || type.type == structClass || type.type == bitClass)
740 _WriteJSONObject(f, type, value.p, indent);
742 else if(type.type == systemClass)
744 WriteNumber(f, type, value, indent);
749 public bool WriteJSONObject(File f, Class objectType, void * object, int indent)
754 result = _WriteJSONObject(f, objectType, object, indent);
760 static bool _WriteJSONObject(File f, Class objectType, void * object, int indent)
764 char * string = null;
766 if(objectType._vTbl[__ecereVMethodID_class_OnGetString] != objectType.base._vTbl[__ecereVMethodID_class_OnGetString])
770 string = ((char *(*)())(void *)objectType._vTbl[__ecereVMethodID_class_OnGetString])(objectType, object, buffer, null, null);
774 // TOCHECK: ProjectNode.ec why do we add quotes in OnGetString there?
775 if(string[0] == '\"')
793 for(prop = objectType.membersAndProperties.first; prop; prop = prop.next)
795 if(prop.memberAccess != publicAccess || (prop.isProperty && (!prop.Set || !prop.Get))) continue;
798 if(!prop.conversion && (!prop.IsSet || prop.IsSet(object)))
801 Class type = eSystem_FindClass(__thisModule, prop.dataTypeString);
803 type = eSystem_FindClass(__thisModule.application, prop.dataTypeString);
805 // TOFIX: How to swiftly handle classes with base data type?
806 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
808 value.d = ((double (*)(void *))(void *)prop.Get)(object);
810 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
812 value.f = ((float (*)(void *))(void *)prop.Get)(object);
814 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64")
815 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
817 value.ui64 = ((uint64 (*)(void *))(void *)prop.Get)(object);
819 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
820 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
822 value.i = ((int (*)(void *))(void *)prop.Get)(object);
824 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
825 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
826 !strcmp(type.dataTypeString, "int16"))
828 value.s = ((short (*)(void *))(void *)prop.Get)(object);
830 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
831 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
833 value.c = ((char (*)(void *))(void *)prop.Get)(object);
837 value.p = ((void *(*)(void *))(void *)prop.Get)(object);
840 if(!isFirst) f.Puts(",\n");
841 for(c = 0; c<indent; c++) f.Puts(" ");
844 f.Putc((char)toupper(prop.name[0]));
847 WriteValue(f, type, value, indent);
853 DataMember member = (DataMember)prop;
855 Class type = eSystem_FindClass(__thisModule, member.dataTypeString);
857 type = eSystem_FindClass(__thisModule.application, member.dataTypeString);
861 if(type.type == normalClass || type.type == noHeadClass || type.type == structClass || !strcmp(type.name, "String"))
863 if(type.type == structClass)
864 value.p = (void *)((byte *)object + member._class.offset + member.offset);
866 value.p = *(void **)((byte *)object + member._class.offset + member.offset);
870 else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
872 value.d = *(double *)((byte *)object + member._class.offset + member.offset);
874 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
876 value.f = *(float *)((byte *)object + member._class.offset + member.offset);
878 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64")
879 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
881 value.ui64 = *(uint64 *)((byte *)object + member._class.offset + member.offset);
883 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
884 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
886 value.i = *(int *)((byte *)object + member._class.offset + member.offset);
887 if(!strcmp(type.name, "bool") || type.type == enumClass)
891 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
892 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
893 !strcmp(type.dataTypeString, "int16"))
895 value.s = *(short *)((byte *)object + member._class.offset + member.offset);
897 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
898 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
900 value.c = *(char *)((byte *)object + member._class.offset + member.offset);
904 value.i = *(int *)((byte *)object + member._class.offset + member.offset);
907 if(!isFirst) f.Puts(",\n");
908 for(c = 0; c<indent; c++) f.Puts(" ");
911 f.Putc((char)toupper(member.name[0]));
912 f.Puts(member.name+1);
914 WriteValue(f, type, value, indent);
922 for(c = 0; c<indent; c++) f.Puts(" "); f.Puts("}");