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(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(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 type._vTbl[__ecereVMethodID_class_OnFree](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)
243 PrintLn("Warning: Incompatible value for array value, expected ", (String)arrayType.name);
245 else if(itemResult == noItem)
251 if(result != syntaxError)
253 if(ch != ']' && ch != ',')
263 result = syntaxError;
271 JSONResult GetString(String * string)
273 JSONResult result = syntaxError;
274 Array<char> buffer { minAllocSize = 256 };
275 bool escaped = false;
283 if(ch == '\\' && !escaped)
289 if(ch == 'b') ch = '\b';
290 else if(ch == 'f') ch = '\f';
291 else if(ch == 'n') ch = '\n';
292 else if(ch == 'r') ch = '\r';
293 else if(ch == 't') ch = '\t';
310 if(buffer.minAllocSize < buffer.count)
311 buffer.minAllocSize *= 2;
315 *string = CopyString(buffer.array);
323 public JSONResult GetObject(Class objectType, void ** object)
325 JSONResult result = syntaxError;
331 if(objectType && objectType.type == noHeadClass || objectType.type == normalClass)
333 *object = eInstance_New(objectType);
337 *object = eSystem_New(objectType.typeSize);
344 if(GetString(&string))
347 DataMember member = null;
348 Property prop = null;
353 string[0] = (char)tolower(string[0]);
354 member = eClass_FindDataMember(objectType, string, objectType.module, null, null);
357 type = eSystem_FindClass(__thisModule, member.dataTypeString);
359 type = eSystem_FindClass(__thisModule.application, member.dataTypeString);
363 prop = eClass_FindProperty(objectType, string, objectType.module);
366 type = eSystem_FindClass(__thisModule, prop.dataTypeString);
368 type = eSystem_FindClass(__thisModule.application, prop.dataTypeString);
371 PrintLn("Warning: member ", string, " not found in class ", (String)objectType.name);
374 // Find Member in Object Class
378 if(type && type.type == structClass)
380 value.p = (byte *)*object + member._class.offset + member.offset;
386 JSONResult itemResult = GetValue(type, value);
387 if(itemResult != syntaxError)
393 PrintLn("warning: Unresolved data type ", member ? (String)member.dataTypeString : (String)prop.dataTypeString);
395 else if(itemResult == success)
400 // TOFIX: How to swiftly handle classes with base data type?
401 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
403 *(double *)((byte *)*object + member._class.offset + member.offset) = value.d;
405 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
407 *(float *)((byte *)*object + member._class.offset + member.offset) = value.f;
409 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64")
410 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
412 *(uint64 *)((byte *)*object + member._class.offset + member.offset) = value.ui64;
414 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
415 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
417 int * ptr = (int *)((byte *)*object + member._class.offset + member.offset);
418 // TODO: Fix Me Up 64 bit pointer here
419 if(eClass_IsDerived(type, class(Container)) && *ptr)
421 Container container = (Container)*ptr;
427 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
428 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
429 !strcmp(type.dataTypeString, "int16"))
431 *(short *)((byte *)*object + member._class.offset + member.offset) = value.s;
433 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
434 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
436 *(char *)((byte *)*object + member._class.offset + member.offset) = value.c;
438 else if(type.type != structClass)
440 *(void **)((byte *)*object + member._class.offset + member.offset) = value.p;
443 else if(prop && prop.Set)
445 if(!strcmp(type.dataTypeString, "char *"))
447 ((void (*)(void *, int))(void *)prop.Set)(*object, value.i);
450 // TOFIX: How to swiftly handle classes with base data type?
451 else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
453 ((void (*)(void *, double))(void *)prop.Set)(*object, value.d);
455 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
457 ((void (*)(void *, float))(void *)prop.Set)(*object, value.f);
459 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64")
460 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
462 ((void (*)(void *, uint64))(void *)prop.Set)(*object, value.ui64);
464 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
465 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
467 ((void (*)(void *, int))(void *)prop.Set)(*object, value.i);
469 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
470 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
471 !strcmp(type.dataTypeString, "int16"))
473 ((void (*)(void *, short))(void *)prop.Set)(*object, value.s);
475 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
476 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
478 ((void (*)(void *, char))(void *)prop.Set)(*object, value.c);
482 ((void (*)(void *, void *))(void *)prop.Set)(*object, value.p);
488 PrintLn("Warning: Incompatible value for ", member ? (String)member.name : (String)prop.name,
489 ", expected ", member ? (String)member.dataTypeString : (String)prop.dataTypeString);
495 result = syntaxError;
499 result = syntaxError;
510 result = syntaxError;
518 JSONResult GetNumber(Class type, DataValue value)
520 JSONResult result = syntaxError;
523 while(c < sizeof(buffer)-1 && (ch == '-' || ch == '.' || tolower(ch) == 'e' || ch == '+' || isdigit(ch)))
526 if(!f.Getc(&ch)) break;
529 //if(strchr(buffer, '.'))
531 // TOFIX: How to swiftly handle classes with base data type?
532 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
534 value.d = strtod(buffer, null);
537 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
539 value.f = (float)strtod(buffer, null);
542 // TOFIX: int64 looks for class long long?
543 //else if(type == class(int64) || !strcmp(type.dataTypeString, "int64"))
544 else if(!strcmp(type.dataTypeString, "int64"))
546 value.i64 = strtol(buffer, null, 10); // TOFIX: 64 bit support
549 else if(type == class(uint64) || !strcmp(type.dataTypeString, "uint64"))
551 value.ui64 = strtol(buffer, null, 10); // TOFIX: 64 bit support
556 value.i = strtol(buffer, null, 10);
563 bool WriteArray(File f, Class type, Container array, int indent)
570 Iterator it { array };
571 Class arrayType = type.templateArgs[0].dataTypeClass;
579 uint64 t = ((uint64(*)(void *, void *))(void *)array.GetData)(array, it.pointer);
586 // TODO: Verify the matching between template type and uint64
587 if(arrayType.type == structClass)
591 else if(arrayType == class(double) || !strcmp(arrayType.dataTypeString, "double"))
593 value.d = *(double *)&t;
595 else if(arrayType == class(float) || !strcmp(arrayType.dataTypeString, "float"))
597 value.f = *(float *)&t;
599 else if(arrayType.typeSize == sizeof(int64) || !strcmp(arrayType.dataTypeString, "int64")
600 !strcmp(arrayType.dataTypeString, "unsigned int64") || !strcmp(arrayType.dataTypeString, "uint64"))
604 else if(arrayType.typeSize == sizeof(int) || !strcmp(arrayType.dataTypeString, "int") ||
605 !strcmp(arrayType.dataTypeString, "unsigned int") || !strcmp(arrayType.dataTypeString, "uint"))
609 else if(arrayType.typeSize == sizeof(short int) || !strcmp(arrayType.dataTypeString, "short") ||
610 !strcmp(arrayType.dataTypeString, "unsigned short") || !strcmp(arrayType.dataTypeString, "uint16") ||
611 !strcmp(arrayType.dataTypeString, "int16"))
615 else if(arrayType.typeSize == sizeof(byte) || !strcmp(arrayType.dataTypeString, "char") ||
616 !strcmp(arrayType.dataTypeString, "unsigned char") || !strcmp(arrayType.dataTypeString, "byte"))
624 for(i = 0; i<indent; i++) f.Puts(" ");
625 WriteValue(f, arrayType, value, indent);
629 for(i = 0; i<indent; i++) f.Puts(" ");
637 bool WriteNumber(File f, Class type, DataValue value, int indent)
640 bool needClass = false;
642 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
643 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.d, buffer, 0, &needClass);
644 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
645 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.f, buffer, null, &needClass);
646 else if(!strcmp(type.dataTypeString, "int64"))
647 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.i64, buffer, null, &needClass);
648 else if(!strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64") || type.typeSize == sizeof(int64))
649 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.ui64, buffer, null, &needClass);
650 else if(!strcmp(type.dataTypeString, "int"))
651 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.i, buffer, null, &needClass);
652 else if(!strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint") || type.typeSize == sizeof(int))
653 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.ui, buffer, null, &needClass);
654 else if(!strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "int16"))
655 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.s, buffer, null, &needClass);
656 else if(!strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") || type.typeSize == sizeof(short int))
657 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.us, buffer, null, &needClass);
658 else if(!strcmp(type.dataTypeString, "char"))
659 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.c, buffer, null, &needClass);
660 else if(!strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte") || type.typeSize == sizeof(byte))
661 type._vTbl[__ecereVMethodID_class_OnGetString](type, &value.uc, buffer, null, &needClass);
666 bool WriteValue(File f, Class type, DataValue value, int indent)
668 if(!strcmp(type.name, "String") || !strcmp(type.dataTypeString, "char *"))
671 //if(strchr(value.p, '\"') || strchr(value.p, '\\'))
676 char * string = value.p;
695 else if(b == sizeof(buffer)-2 || !ch)
698 if(ch) buffer[b] = 0;
711 else if(!strcmp(type.name, "bool"))
718 else if(!strcmp(type.name, "SetBool"))
720 if(value.i == SetBool::true)
722 else if(value.i == SetBool::false)
727 else if(type.type == enumClass)
730 WriteNumber(f, type, value, indent);
733 else if(eClass_IsDerived(type, class(Container)))
735 WriteArray(f, type, value.p, indent);
737 else if(type.type == normalClass || type.type == noHeadClass || type.type == structClass || type.type == bitClass)
739 WriteJSONObject(f, type, value.p, indent);
741 else if(type.type == systemClass)
743 WriteNumber(f, type, value, indent);
748 public bool WriteJSONObject(File f, Class objectType, void * object, int indent)
752 char * string = null;
754 if(objectType._vTbl[__ecereVMethodID_class_OnGetString] != objectType.base._vTbl[__ecereVMethodID_class_OnGetString])
758 string = ((char *(*)())(void *)objectType._vTbl[__ecereVMethodID_class_OnGetString])(objectType, object, buffer, null, null);
762 // TOCHECK: ProjectNode.ec why do we add quotes in OnGetString there?
763 if(string[0] == '\"')
781 for(prop = objectType.membersAndProperties.first; prop; prop = prop.next)
783 if(prop.memberAccess != publicAccess || (prop.isProperty && (!prop.Set || !prop.Get))) continue;
786 if(!prop.conversion && (!prop.IsSet || prop.IsSet(object)))
789 Class type = eSystem_FindClass(__thisModule, prop.dataTypeString);
791 type = eSystem_FindClass(__thisModule.application, prop.dataTypeString);
793 // TOFIX: How to swiftly handle classes with base data type?
794 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
796 value.d = ((double (*)(void *))(void *)prop.Get)(object);
798 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
800 value.f = ((float (*)(void *))(void *)prop.Get)(object);
802 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64")
803 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
805 value.ui64 = ((uint64 (*)(void *))(void *)prop.Get)(object);
807 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
808 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
810 value.i = ((int (*)(void *))(void *)prop.Get)(object);
812 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
813 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
814 !strcmp(type.dataTypeString, "int16"))
816 value.s = ((short (*)(void *))(void *)prop.Get)(object);
818 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
819 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
821 value.c = ((char (*)(void *))(void *)prop.Get)(object);
825 value.p = ((void *(*)(void *))(void *)prop.Get)(object);
828 if(!isFirst) f.Puts(",\n");
829 for(c = 0; c<indent; c++) f.Puts(" ");
832 f.Putc((char)toupper(prop.name[0]));
835 WriteValue(f, type, value, indent);
841 DataMember member = (DataMember)prop;
843 Class type = eSystem_FindClass(__thisModule, member.dataTypeString);
845 type = eSystem_FindClass(__thisModule.application, member.dataTypeString);
849 if(type.type == normalClass || type.type == noHeadClass || type.type == structClass || !strcmp(type.name, "String"))
851 if(type.type == structClass)
852 value.p = (void *)((byte *)object + member._class.offset + member.offset);
854 value.p = *(void **)((byte *)object + member._class.offset + member.offset);
858 else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
860 value.d = *(double *)((byte *)object + member._class.offset + member.offset);
862 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
864 value.f = *(float *)((byte *)object + member._class.offset + member.offset);
866 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64")
867 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
869 value.ui64 = *(uint64 *)((byte *)object + member._class.offset + member.offset);
871 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
872 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
874 value.i = *(int *)((byte *)object + member._class.offset + member.offset);
875 if(!strcmp(type.name, "bool") || type.type == enumClass)
879 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
880 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
881 !strcmp(type.dataTypeString, "int16"))
883 value.s = *(short *)((byte *)object + member._class.offset + member.offset);
885 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
886 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
888 value.c = *(char *)((byte *)object + member._class.offset + member.offset);
892 value.i = *(int *)((byte *)object + member._class.offset + member.offset);
895 if(!isFirst) f.Puts(",\n");
896 for(c = 0; c<indent; c++) f.Puts(" ");
899 f.Putc((char)toupper(member.name[0]));
900 f.Puts(member.name+1);
902 WriteValue(f, type, value, indent);
910 for(c = 0; c<indent; c++) f.Puts(" "); f.Puts("}");