public static JsonKeyValuePair Deserialize(string json, Type typ, bool onlyValue = false, JsonKeyValuePair existing_jkvp = null)
        {
            JsonKeyValuePair ret = new JsonKeyValuePair();

            if (existing_jkvp == null)
            {
                if (onlyValue)
                {
                    ret.value = json;
                }
                else
                {
                    ret = JsonReader.GetKeyValueData(json);
                }
            }
            else
            {
                ret = existing_jkvp;
            }

            if (ret.value.ToString() == "null")
            {
                ret.value = null;
            }
            else
            {
                if (typ.IsNumericType())
                {
                    ret.value = Convert.ChangeType(Convert.ToDouble(ret.value.ToString()), typ);
                }

                else if (Type.GetTypeCode(typ) == TypeCode.Boolean)
                {
                    ret.value = (ret.value.ToString().ToLower() == "true") ? true : false;
                }

                // TODO: DATETIME
                else if (typ.IsList()) // A List<>
                {
                    ret.value = DeserializeArray(ret.value.ToString(), typ);
                }

                else if (typ.IsArray) // An Array - same as list but without .ToList();
                {
                    ret.value = DeserializeArray(ret.value.ToString(), typ).ToArray();
                }

                else if (typ.IsDictionary())
                {
                    ret.value = DeserializeDictionary(ret.value.ToString(), typ);
                }

                else if (typ == typeof(Bitmap))
                {
                    ret.value = ImageToText.ConvertTextToImage(ret.value.ToString());
                }

                else if (typ == typeof(Image))
                {
                    ret.value = ImageToText.ConvertTextToImage(ret.value.ToString());
                }

                else if (typ == typeof(Point))
                {
                    ret.value = new PointConverter().ConvertFromString(ret.value.ToString());
                }

                else if (typ == typeof(Size))
                {
                    ret.value = new SizeConverter().ConvertFromString(ret.value.ToString());
                }

                else if (typ == typeof(Rectangle))
                {
                    ret.value = new RectangleConverter().ConvertFromString(ret.value.ToString());
                }

                else if (typ == typeof(DateTime))
                {
                    ret.value = DeserializeDateTime(ret.value.ToString());
                }

                else if (typ == typeof(Color))
                {
                    ret.value = new ColorConverter().ConvertFromString(ret.value.ToString());
                }

                else if (typ == typeof(Font))
                {
                    ret.value = new FontConverter().ConvertFromString(ret.value.ToString());
                }

                else if (typ == typeof(char))
                {
                    ret.value = new CharConverter().ConvertFromString(ret.value.ToString());
                }

                else if (typ == typeof(Guid))
                {
                    ret.value = new GuidConverter().ConvertFromString(ret.value.ToString());
                }

                else if (typ != typeof(string))
                {
                    ret.value = DeserializeObject(ret.value.ToString(), typ);
                }

                //else if (Activator.CreateInstance(typ) is Bitmap)
                //    ret.value = ImageToText.ConvertTextToImage(ret.value.ToString());

                //else if (Activator.CreateInstance(typ) is Image)
                //    ret.value = ImageToText.ConvertTextToImage(ret.value.ToString());

                //else if (Activator.CreateInstance(typ) is Point)
                //    ret.value = new Point(int.Parse(ret.value.ToString().Split(',')[0]), int.Parse(ret.value.ToString().Split(',')[1]));
            }
            return(ret);
        }
Пример #2
0
        public static string Serialize(string name, object obj, JsonFormatting format, int indentLevel, bool justValue = false)
        {
            string result = "";

            if (obj == null)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(obj, JsonKeyValueType.Null, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, obj, JsonKeyValueType.Null, format, indentLevel);
                }
            }

            else if (obj is bool)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(obj, JsonKeyValueType.Boolean, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, obj, JsonKeyValueType.Boolean, format, indentLevel);
                }
            }

            else if (obj is string)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(obj, JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, obj, JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is DateTime)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(obj, JsonKeyValueType.DateTime, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, obj, JsonKeyValueType.DateTime, format, indentLevel);
                }
            }

            else if (obj.IsNumericType())
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(obj, JsonKeyValueType.Numerical, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, obj, JsonKeyValueType.Numerical, format, indentLevel);
                }
            }

            else if (obj.IsArray())
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(obj, JsonKeyValueType.Array, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, obj, JsonKeyValueType.Array, format, indentLevel);
                }
            }

            else if (obj.IsDictionary())
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(obj, JsonKeyValueType.Dictionary, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, obj, JsonKeyValueType.Dictionary, format, indentLevel);
                }
            }

            else if (obj is Bitmap)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(ImageToText.ConvertImageToText((Bitmap)obj, System.Drawing.Imaging.ImageFormat.Png), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, ImageToText.ConvertImageToText((Bitmap)obj, System.Drawing.Imaging.ImageFormat.Png), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is Image)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(ImageToText.ConvertImageToText((Image)obj, System.Drawing.Imaging.ImageFormat.Png), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, ImageToText.ConvertImageToText((Image)obj, System.Drawing.Imaging.ImageFormat.Png), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is Point)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(new PointConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, new PointConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is Size)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(new SizeConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, new SizeConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is Rectangle)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(new RectangleConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, new RectangleConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is Color)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(new ColorConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, new ColorConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is Font)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(new FontConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, new FontConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is char)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(new CharConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, new CharConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (obj is Guid)
            {
                if (justValue)
                {
                    result += JsonWriter.WriteValue(new GuidConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
                else
                {
                    result += JsonWriter.WriteKeyValuePair(name, new GuidConverter().ConvertToString(obj), JsonKeyValueType.Text, format, indentLevel);
                }
            }

            else if (justValue)
            {
                result += JsonWriter.WriteValue(obj, JsonKeyValueType.Object, format, indentLevel);
            }
            else
            {
                result += JsonWriter.WriteKeyValuePair(name, obj, JsonKeyValueType.Object, format, indentLevel);
            }

            return(result);
        }