7 __attribute__((unused)) 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;
181 *array = eInstance_New(type);
186 Class arrayType = null;
187 JSONResult itemResult;
189 if(eClass_IsDerived(type, class(Container)))
191 arrayType = type.templateArgs[0].dataTypeClass;
194 itemResult = GetValue(arrayType, value);
195 if(itemResult == success)
197 // TODO: Verify the matching between template type and uint64
199 if(arrayType.type == structClass)
201 t = (uint64) value.p;
203 else if(arrayType == class(double) || !strcmp(arrayType.dataTypeString, "double"))
205 t = *(uint64 *)&value.d;
207 else if(arrayType == class(float) || !strcmp(arrayType.dataTypeString, "float"))
209 t = *(uint *)&value.f;
211 else if(arrayType.typeSize == sizeof(int64) || !strcmp(arrayType.dataTypeString, "int64") ||
212 !strcmp(arrayType.dataTypeString, "unsigned int64") || !strcmp(arrayType.dataTypeString, "uint64"))
216 else if(arrayType.typeSize == sizeof(int) || !strcmp(arrayType.dataTypeString, "int") ||
217 !strcmp(arrayType.dataTypeString, "unsigned int") || !strcmp(arrayType.dataTypeString, "uint"))
221 else if(arrayType.typeSize == sizeof(short int) || !strcmp(arrayType.dataTypeString, "short") ||
222 !strcmp(arrayType.dataTypeString, "unsigned short") || !strcmp(arrayType.dataTypeString, "uint16") ||
223 !strcmp(arrayType.dataTypeString, "int16"))
227 else if(arrayType.typeSize == sizeof(byte) || !strcmp(arrayType.dataTypeString, "char") ||
228 !strcmp(arrayType.dataTypeString, "unsigned char") || !strcmp(arrayType.dataTypeString, "byte"))
236 ((void *(*)(void *, uint64))(void *)array->Add)(*array, t);
240 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))
346 DataMember member = null;
347 Property prop = null;
352 string[0] = (char)tolower(string[0]);
353 member = eClass_FindDataMember(objectType, string, objectType.module, null, null);
356 type = eSystem_FindClass(__thisModule, member.dataTypeString);
358 type = eSystem_FindClass(__thisModule.application, member.dataTypeString);
362 prop = eClass_FindProperty(objectType, string, objectType.module);
365 type = eSystem_FindClass(__thisModule, prop.dataTypeString);
367 type = eSystem_FindClass(__thisModule.application, prop.dataTypeString);
370 PrintLn("Warning: member ", string, " not found in class ", (String)objectType.name);
373 // Find Member in Object Class
377 if(type && type.type == structClass)
379 value.p = (byte *)*object + member._class.offset + member.offset;
385 JSONResult itemResult = GetValue(type, value);
386 if(itemResult != syntaxError)
392 PrintLn("warning: Unresolved data type ", member ? (String)member.dataTypeString : (String)prop.dataTypeString);
394 else if(itemResult == success)
399 // TOFIX: How to swiftly handle classes with base data type?
400 if(type.type == structClass)
402 else if(type.type == normalClass || type.type == noHeadClass)
404 void ** ptr = (void**)((byte *)*object + member._class.offset + member.offset);
405 if(eClass_IsDerived(type, class(Container)) && *ptr)
407 Container container = (Container)*ptr;
413 else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
415 *(double *)((byte *)*object + member._class.offset + member.offset) = value.d;
417 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
419 *(float *)((byte *)*object + member._class.offset + member.offset) = value.f;
421 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64") ||
422 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
424 *(uint64 *)((byte *)*object + member._class.offset + member.offset) = value.ui64;
426 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
427 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
429 *(int *)((byte *)*object + member._class.offset + member.offset) = value.i;
431 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
432 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
433 !strcmp(type.dataTypeString, "int16"))
435 *(short *)((byte *)*object + member._class.offset + member.offset) = value.s;
437 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
438 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
440 *(char *)((byte *)*object + member._class.offset + member.offset) = value.c;
444 *(void **)((byte *)*object + member._class.offset + member.offset) = value.p;
447 else if(prop && prop.Set)
449 if(!strcmp(type.dataTypeString, "char *"))
451 ((void (*)(void *, void *))(void *)prop.Set)(*object, value.p);
454 // TOFIX: How to swiftly handle classes with base data type?
455 else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
457 ((void (*)(void *, double))(void *)prop.Set)(*object, value.d);
459 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
461 ((void (*)(void *, float))(void *)prop.Set)(*object, value.f);
463 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64") ||
464 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
466 ((void (*)(void *, uint64))(void *)prop.Set)(*object, value.ui64);
468 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
469 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
471 ((void (*)(void *, int))(void *)prop.Set)(*object, value.i);
473 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
474 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
475 !strcmp(type.dataTypeString, "int16"))
477 ((void (*)(void *, short))(void *)prop.Set)(*object, value.s);
479 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
480 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
482 ((void (*)(void *, char))(void *)prop.Set)(*object, value.c);
486 ((void (*)(void *, void *))(void *)prop.Set)(*object, value.p);
492 PrintLn("Warning: Incompatible value for ", member ? (String)member.name : (String)prop.name,
493 ", expected ", member ? (String)member.dataTypeString : (String)prop.dataTypeString);
499 result = syntaxError;
503 result = syntaxError;
514 result = syntaxError;
522 JSONResult GetNumber(Class type, DataValue value)
524 JSONResult result = syntaxError;
527 while(c < sizeof(buffer)-1 && (ch == '-' || ch == '.' || tolower(ch) == 'e' || ch == '+' || isdigit(ch)))
530 if(!f.Getc(&ch)) break;
533 //if(strchr(buffer, '.'))
535 // TOFIX: How to swiftly handle classes with base data type?
536 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
538 value.d = strtod(buffer, null);
541 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
543 value.f = (float)strtod(buffer, null);
546 // TOFIX: int64 looks for class long long?
547 //else if(type == class(int64) || !strcmp(type.dataTypeString, "int64"))
548 else if(!strcmp(type.dataTypeString, "int64"))
550 value.i64 = strtol(buffer, null, 10); // TOFIX: 64 bit support
553 else if(type == class(uint64) || !strcmp(type.dataTypeString, "uint64"))
555 value.ui64 = strtol(buffer, null, 10); // TOFIX: 64 bit support
560 value.i = strtol(buffer, null, 10);
567 bool WriteArray(File f, Class type, Container array, int indent)
573 Iterator it { array };
574 Class arrayType = type.templateArgs[0].dataTypeClass;
581 uint64 t = ((uint64(*)(void *, void *))(void *)array.GetData)(array, it.pointer);
588 // TODO: Verify the matching between template type and uint64
589 if(arrayType.type == structClass)
593 else if(arrayType == class(double) || !strcmp(arrayType.dataTypeString, "double"))
595 value.d = *(double *)&t;
597 else if(arrayType == class(float) || !strcmp(arrayType.dataTypeString, "float"))
599 value.f = *(float *)&t;
601 else if(arrayType.typeSize == sizeof(int64) || !strcmp(arrayType.dataTypeString, "int64") ||
602 !strcmp(arrayType.dataTypeString, "unsigned int64") || !strcmp(arrayType.dataTypeString, "uint64"))
606 else if(arrayType.typeSize == sizeof(int) || !strcmp(arrayType.dataTypeString, "int") ||
607 !strcmp(arrayType.dataTypeString, "unsigned int") || !strcmp(arrayType.dataTypeString, "uint"))
611 else if(arrayType.typeSize == sizeof(short int) || !strcmp(arrayType.dataTypeString, "short") ||
612 !strcmp(arrayType.dataTypeString, "unsigned short") || !strcmp(arrayType.dataTypeString, "uint16") ||
613 !strcmp(arrayType.dataTypeString, "int16"))
617 else if(arrayType.typeSize == sizeof(byte) || !strcmp(arrayType.dataTypeString, "char") ||
618 !strcmp(arrayType.dataTypeString, "unsigned char") || !strcmp(arrayType.dataTypeString, "byte"))
626 for(i = 0; i<indent; i++) f.Puts(" ");
627 WriteValue(f, arrayType, value, indent);
631 for(i = 0; i<indent; i++) f.Puts(" ");
639 bool WriteNumber(File f, Class type, DataValue value, int indent)
642 bool needClass = false;
644 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
645 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.d, buffer, 0, &needClass);
646 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
647 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.f, buffer, null, &needClass);
648 else if(!strcmp(type.dataTypeString, "int64"))
649 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.i64, buffer, null, &needClass);
650 else if(!strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64") || type.typeSize == sizeof(int64))
651 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.ui64, buffer, null, &needClass);
652 else if(!strcmp(type.dataTypeString, "int"))
653 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.i, buffer, null, &needClass);
654 else if(!strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint") || type.typeSize == sizeof(int))
655 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.ui, buffer, null, &needClass);
656 else if(!strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "int16"))
657 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.s, buffer, null, &needClass);
658 else if(!strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") || type.typeSize == sizeof(short int))
659 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.us, buffer, null, &needClass);
660 else if(!strcmp(type.dataTypeString, "char"))
661 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.c, buffer, null, &needClass);
662 else if(!strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte") || type.typeSize == sizeof(byte))
663 ((char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.uc, buffer, null, &needClass);
668 bool WriteValue(File f, Class type, DataValue value, int indent)
670 if(!strcmp(type.name, "String") || !strcmp(type.dataTypeString, "char *"))
673 //if(strchr(value.p, '\"') || strchr(value.p, '\\'))
678 char * string = value.p;
697 else if(b == sizeof(buffer)-2 || !ch)
700 if(ch) buffer[b] = 0;
713 else if(!strcmp(type.name, "bool"))
720 else if(!strcmp(type.name, "SetBool"))
722 if(value.i == SetBool::true)
724 else if(value.i == SetBool::false)
729 else if(type.type == enumClass)
732 WriteNumber(f, type, value, indent);
735 else if(eClass_IsDerived(type, class(Container)))
737 WriteArray(f, type, value.p, indent);
739 else if(type.type == normalClass || type.type == noHeadClass || type.type == structClass || type.type == bitClass)
741 _WriteJSONObject(f, type, value.p, indent);
743 else if(type.type == systemClass)
745 WriteNumber(f, type, value, indent);
750 public bool WriteJSONObject(File f, Class objectType, void * object, int indent)
755 result = _WriteJSONObject(f, objectType, object, indent);
761 static bool _WriteJSONObject(File f, Class objectType, void * object, int indent)
765 char * string = null;
767 if(objectType._vTbl[__ecereVMethodID_class_OnGetString] != objectType.base._vTbl[__ecereVMethodID_class_OnGetString])
771 string = ((char *(*)())(void *)objectType._vTbl[__ecereVMethodID_class_OnGetString])(objectType, object, buffer, null, null);
775 // TOCHECK: ProjectNode.ec why do we add quotes in OnGetString there?
776 if(string[0] == '\"')
794 for(prop = objectType.membersAndProperties.first; prop; prop = prop.next)
796 if(prop.memberAccess != publicAccess || (prop.isProperty && (!prop.Set || !prop.Get))) continue;
799 if(!prop.conversion && (!prop.IsSet || prop.IsSet(object)))
802 Class type = eSystem_FindClass(__thisModule, prop.dataTypeString);
804 type = eSystem_FindClass(__thisModule.application, prop.dataTypeString);
806 // TOFIX: How to swiftly handle classes with base data type?
807 if(type == class(double) || !strcmp(type.dataTypeString, "double"))
809 value.d = ((double (*)(void *))(void *)prop.Get)(object);
811 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
813 value.f = ((float (*)(void *))(void *)prop.Get)(object);
815 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64") ||
816 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
818 value.ui64 = ((uint64 (*)(void *))(void *)prop.Get)(object);
820 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
821 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
823 value.i = ((int (*)(void *))(void *)prop.Get)(object);
825 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
826 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
827 !strcmp(type.dataTypeString, "int16"))
829 value.s = ((short (*)(void *))(void *)prop.Get)(object);
831 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
832 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
834 value.c = ((char (*)(void *))(void *)prop.Get)(object);
838 value.p = ((void *(*)(void *))(void *)prop.Get)(object);
841 if(!isFirst) f.Puts(",\n");
842 for(c = 0; c<indent; c++) f.Puts(" ");
845 f.Putc((char)toupper(prop.name[0]));
848 WriteValue(f, type, value, indent);
854 DataMember member = (DataMember)prop;
856 Class type = eSystem_FindClass(__thisModule, member.dataTypeString);
858 type = eSystem_FindClass(__thisModule.application, member.dataTypeString);
862 if(type.type == normalClass || type.type == noHeadClass || type.type == structClass || !strcmp(type.name, "String"))
864 if(type.type == structClass)
865 value.p = (void *)((byte *)object + member._class.offset + member.offset);
867 value.p = *(void **)((byte *)object + member._class.offset + member.offset);
871 else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
873 value.d = *(double *)((byte *)object + member._class.offset + member.offset);
875 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
877 value.f = *(float *)((byte *)object + member._class.offset + member.offset);
879 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64") ||
880 !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
882 value.ui64 = *(uint64 *)((byte *)object + member._class.offset + member.offset);
884 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
885 !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
887 value.i = *(int *)((byte *)object + member._class.offset + member.offset);
888 if(!strcmp(type.name, "bool") || type.type == enumClass)
892 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
893 !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
894 !strcmp(type.dataTypeString, "int16"))
896 value.s = *(short *)((byte *)object + member._class.offset + member.offset);
898 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
899 !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
901 value.c = *(char *)((byte *)object + member._class.offset + member.offset);
905 value.i = *(int *)((byte *)object + member._class.offset + member.offset);
908 if(!isFirst) f.Puts(",\n");
909 for(c = 0; c<indent; c++) f.Puts(" ");
912 f.Putc((char)toupper(member.name[0]));
913 f.Puts(member.name+1);
915 WriteValue(f, type, value, indent);
923 for(c = 0; c<indent; c++) f.Puts(" "); f.Puts("}");