ecere/sys/JSON: Fixed broken TimeStamp text serialization
[sdk] / ecere / src / sys / JSON.ec
index 9d29feb..e9f9f1b 100644 (file)
@@ -200,6 +200,8 @@ public:
                arrayType = type.templateArgs[0].dataTypeClass;
             }
 
+            if(arrayType && arrayType.type == structClass)
+               value.p = new0 byte[arrayType.structSize];
             itemResult = GetValue(arrayType, value);
             if(itemResult == success)
             {
@@ -243,6 +245,9 @@ public:
                   t = (uint64)(uintptr)value.p;
                }
                ((void *(*)(void *, uint64))(void *)array->Add)(*array, t);
+
+               if(arrayType && arrayType.type == structClass)
+                  delete value.p;
             }
             else
             {
@@ -476,7 +481,15 @@ public:
 
                   if(type && type.type == structClass)
                   {
-                     value.p = (byte *)*object + offset;
+                     if(member)
+                     {
+                        value.p = (byte *)*object + offset;
+                        memset(value.p, 0, type.structSize);
+                     }
+                     else if(prop)
+                     {
+                        value.p = new0 byte[type.structSize];
+                     }
                   }
                   ch = 0;
                   SkipEmpty();
@@ -598,6 +611,11 @@ public:
                   }
                   else
                      result = syntaxError;
+
+                  if(prop && type.type == structClass)
+                  {
+                     delete value.p;
+                  }
                }
             }
             else if(ch && ch != '}' && ch != ',')
@@ -653,7 +671,12 @@ public:
       }
       else if(type == class(uint64) || !strcmp(type.dataTypeString, "uint64"))
       {
-         value.ui64 = strtol(buffer, null, 10);  // TOFIX: 64 bit support
+         value.ui64 = strtoul(buffer, null, 10);  // TOFIX: 64 bit support
+         result = success;
+      }
+      else if(type == class(uint) || !strcmp(type.dataTypeString, "unsigned int"))
+      {
+         value.ui = strtoul(buffer, null, 10);  // TOFIX: 64 bit support
          result = success;
       }
       else
@@ -661,6 +684,52 @@ public:
          value.i = strtol(buffer, null, 10);
          result = success;
       }
+
+      if(result == success && type.type == unitClass)
+      {
+         // Convert to reference unit
+         Property prop;
+         for(prop = type.conversions.first; prop; prop = prop.next)
+         {
+            bool refProp = false;
+            Class c;
+            if(!strcmp(prop.name, type.base.fullName))
+               refProp = true;
+            else if( (c = eSystem_FindClass(type.module, prop.name) ) )
+            {
+               Property p;
+               for(p = c.conversions.first; p; p = p.next)
+               {
+                  if(!strcmp(p.name, type.base.fullName) && !p.Set && !p.Get)
+                  {
+                     refProp = true;
+                     break;
+                  }
+               }
+            }
+            if(refProp)
+            {
+               if(prop.Set && prop.Get)
+               {
+                  const String dts = type.base.dataTypeString;
+                  if(!strcmp(dts, "double"))
+                     value.d = ((double(*)(double))(void *)prop.Get)(value.d);
+                  else if(!strcmp(dts, "float"))
+                     value.f = ((float(*)(float))(void *)prop.Get)(value.f);
+                  else if(!strcmp(dts, "int"))
+                     value.i = ((int(*)(int))(void *)prop.Get)(value.i);
+                  else if(!strcmp(dts, "int64"))
+                     value.i64 = ((int64(*)(int64))(void *)prop.Get)(value.i64);
+                  else if(!strcmp(dts, "unsigned int"))
+                     value.ui = ((uint(*)(uint))(void *)prop.Get)(value.ui);
+                  else if(!strcmp(dts, "uint64"))
+                     value.ui64 = ((uint64(*)(uint64))(void *)prop.Get)(value.ui64);
+               }
+               else
+                  break;
+            }
+         }
+      }
       return result;
    }
 }
@@ -774,6 +843,7 @@ bool WriteNumber(File f, Class type, DataValue value, int indent)
 {
    char buffer[1024];
    bool needClass = false;
+   bool quote;
    buffer[0] = 0;
    if(type == class(double) || !strcmp(type.dataTypeString, "double"))
       ((const char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.d, buffer, 0, &needClass);
@@ -795,7 +865,11 @@ bool WriteNumber(File f, Class type, DataValue value, int indent)
       ((const char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.c, buffer, null, &needClass);
    else if(!strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte") || type.typeSize == sizeof(byte))
       ((const char *(*)(void *, void *, char *, void *, bool *))(void *)type._vTbl[__ecereVMethodID_class_OnGetString])(type, &value.uc, buffer, null, &needClass);
+
+   quote = (type.type == unitClass && ((buffer[0] != '.' && !isdigit(buffer[0])) || strchr(buffer, ' ')));
+   if(quote) f.Puts("\"");
    f.Puts(buffer);
+   if(quote) f.Puts("\"");
    return true;
 }
 
@@ -865,7 +939,7 @@ bool WriteValue(File f, Class type, DataValue value, int indent)
       else
          f.Puts("unset");
    }
-   else if(type.type == enumClass || type.type == unitClass)
+   else if(type.type == enumClass)
    {
       f.Puts("\"");
       WriteNumber(f, type, value, indent);
@@ -889,7 +963,7 @@ bool WriteValue(File f, Class type, DataValue value, int indent)
       dataType = eSystem_FindClass(__thisModule, type.dataTypeString);
       WriteNumber(f, dataType, value, indent);
    }
-   else if(type.type == systemClass)
+   else if(type.type == systemClass || type.type == unitClass)
    {
       WriteNumber(f, type, value, indent);
    }
@@ -1010,6 +1084,11 @@ static bool _WriteJSONObject(File f, Class objectType, void * object, int indent
                         {
                            value.c = ((char (*)(void *))(void *)prop.Get)(object);
                         }
+                        else if(type.type == structClass)
+                        {
+                           value.p = new0 byte[type.structSize];
+                           ((void (*)(void *, void *))(void *)prop.Get)(object, value.p);
+                        }
                         else
                         {
                            value.p = ((void *(*)(void *))(void *)prop.Get)(object);
@@ -1024,6 +1103,8 @@ static bool _WriteJSONObject(File f, Class objectType, void * object, int indent
                         f.Puts("\" : ");
                         WriteValue(f, type, value, indent);
                         isFirst = false;
+                        if(type.type == structClass)
+                           delete value.p;
                      }
                   }
                }