ecere/sys/JSON: add ColorAlpha de/stringification into JSON parser/writer.
[sdk] / ecere / src / sys / JSON.ec
index 45b8794..efd77f5 100644 (file)
@@ -76,6 +76,10 @@ public:
                result = success;
                delete string;
             }
+            else if(type && eClass_IsDerived(type, class(ColorAlpha)))
+            {
+               result = GetColorAlpha(string, value);
+            }
             else if(type && (type.type == structClass))
             {
                if(((bool (*)(void *, void *, const char *))(void *)type._vTbl[__ecereVMethodID_class_OnGetDataFromString])(type, value.p, string))
@@ -167,7 +171,8 @@ public:
             }
             else if(!strcmpi(buffer, "null"))
             {
-               value.p = 0;
+               if(type.type != structClass)
+                  value.p = 0;
             }
             else
                result = typeMismatch;
@@ -565,35 +570,42 @@ public:
                                     if(!isKey)
                                        delete value.p;
                                  }
-                                 // TOFIX: How to swiftly handle classes with base data type?
-                                 else if(type == class(double) || !strcmp(type.dataTypeString, "double"))
-                                 {
-                                    ((void (*)(void *, double))(void *)prop.Set)(*object, value.d);
-                                 }
-                                 else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
-                                 {
-                                    ((void (*)(void *, float))(void *)prop.Set)(*object, value.f);
-                                 }
-                                 else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64") ||
-                                    !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
-                                 {
-                                    ((void (*)(void *, uint64))(void *)prop.Set)(*object, value.ui64);
-                                 }
-                                 else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
-                                    !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
-                                 {
-                                    ((void (*)(void *, int))(void *)prop.Set)(*object, value.i);
-                                 }
-                                 else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
-                                    !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
-                                    !strcmp(type.dataTypeString, "int16"))
-                                 {
-                                    ((void (*)(void *, short))(void *)prop.Set)(*object, value.s);
-                                 }
-                                 else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
-                                    !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
+                                 else if(type.type == enumClass || type.type == bitClass || type.type == unitClass || type.type == systemClass)
                                  {
-                                    ((void (*)(void *, char))(void *)prop.Set)(*object, value.c);
+                                    // TOFIX: How to swiftly handle classes with base data type?
+                                    if(type == class(double) || !strcmp(type.dataTypeString, "double"))
+                                    {
+                                       ((void (*)(void *, double))(void *)prop.Set)(*object, value.d);
+                                    }
+                                    else if(type == class(float) || !strcmp(type.dataTypeString, "float"))
+                                    {
+                                       ((void (*)(void *, float))(void *)prop.Set)(*object, value.f);
+                                    }
+                                    else if(type.typeSize == sizeof(int64) || !strcmp(type.dataTypeString, "int64") ||
+                                       !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64"))
+                                    {
+                                       ((void (*)(void *, uint64))(void *)prop.Set)(*object, value.ui64);
+                                    }
+                                    else if(type.typeSize == sizeof(int) || !strcmp(type.dataTypeString, "int") ||
+                                       !strcmp(type.dataTypeString, "unsigned int") || !strcmp(type.dataTypeString, "uint"))
+                                    {
+                                       ((void (*)(void *, int))(void *)prop.Set)(*object, value.i);
+                                    }
+                                    else if(type.typeSize == sizeof(short int) || !strcmp(type.dataTypeString, "short") ||
+                                       !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
+                                       !strcmp(type.dataTypeString, "int16"))
+                                    {
+                                       ((void (*)(void *, short))(void *)prop.Set)(*object, value.s);
+                                    }
+                                    else if(type.typeSize == sizeof(byte) || !strcmp(type.dataTypeString, "char") ||
+                                       !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
+                                    {
+                                       ((void (*)(void *, char))(void *)prop.Set)(*object, value.c);
+                                    }
+                                    else
+                                    {
+                                       ((void (*)(void *, int))(void *)prop.Set)(*object, value.i);
+                                    }
                                  }
                                  else
                                  {
@@ -733,6 +745,38 @@ public:
       }
       return result;
    }
+
+   JSONResult GetColorAlpha(String string, DataValue value)
+   {
+      ColorAlpha color = 0;
+      DefinedColor c = 0;
+      if(string)
+      {
+         if(string[0] == '0' && string[1] == 'x')
+            color = (uint)strtoul(string, null, 0);
+         else
+         {
+            char *d;
+            byte a = 255;
+            if((d = strchr(string, ',')))
+            {
+               a = (byte)atoi(string);
+               d += 2;
+            }
+            else
+               d = string;
+            if(c.class::OnGetDataFromString(d))
+            {
+               color.a = a;
+               color.color = c;
+            }
+            else
+               color = (uint)strtoul(string, null, 16);
+         }
+      }
+      value.i = color;
+      return success;
+   }
 }
 
 bool WriteMap(File f, Class type, Map map, int indent)
@@ -874,6 +918,32 @@ bool WriteNumber(File f, Class type, DataValue value, int indent)
    return true;
 }
 
+public bool WriteColorAlpha(File f, Class type, DataValue value, int indent)
+{
+   char buffer[1024];
+   char * string = buffer;
+   ColorAlpha color = value.i;
+   int a = color.a;
+   int len;
+   DefinedColor c = color;
+   buffer[0] = '\0';
+   if(a != 255)
+   {
+      a.class::OnGetString(buffer, null, null);
+      len = strlen(buffer);
+      buffer[len++] = ',';
+      buffer[len++] = ' ';
+      buffer[len] = '\0';
+      string += len;
+   }
+   if(!c.class::OnGetString(string, null, null))
+      sprintf(buffer, "0x%x", color);
+   f.Puts("\"");
+   f.Puts(buffer);
+   f.Puts("\"");
+   return true;
+}
+
 bool WriteValue(File f, Class type, DataValue value, int indent)
 {
    if(!strcmp(type.name, "String") || !strcmp(type.dataTypeString, "char *"))
@@ -958,6 +1028,10 @@ bool WriteValue(File f, Class type, DataValue value, int indent)
    {
       _WriteJSONObject(f, type, value.p, indent);
    }
+   else if(eClass_IsDerived(type, class(ColorAlpha)))
+   {
+      WriteColorAlpha(f, type, value, indent);
+   }
    else if(type.type == bitClass)
    {
       Class dataType;