Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

序列化与反序列化不支持自定义对象、泛型集合 #177

Open
lyj251lyj opened this issue Feb 6, 2024 · 2 comments
Open

序列化与反序列化不支持自定义对象、泛型集合 #177

lyj251lyj opened this issue Feb 6, 2024 · 2 comments

Comments

@lyj251lyj
Copy link

internal object SerializeRedisValue(object value)
{
    if (value != null)
    {
        if (!(value is string) && !(value is byte[]))
        {
            if (!(value is bool))
            {
                if (!(value is char))
                {
                    if (!(value is decimal) && !(value is double) && !(value is float) && !(value is int) && !(value is long) && !(value is sbyte) && !(value is short) && !(value is uint) && !(value is ulong) && !(value is ushort))
                    {
                        if (!(value is DateTime dateTime))
                        {
                            if (!(value is DateTimeOffset))
                            {
                                if (!(value is TimeSpan timeSpan))
                                {
                                    if (value is Guid)
                                    {
                                        return value.ToString();
                                    }

                                    if (Adapter.TopOwner.Serialize != null)
                                    {
                                        return Adapter.TopOwner.Serialize(value);
                                    }

                                    return value.ConvertTo<string>();
                                }

                                return timeSpan.Ticks;
                            }

                            return value.ToString();
                        }

                        return dateTime.ToString("yyyy-MM-ddTHH:mm:sszzzz", DateTimeFormatInfo.InvariantInfo);
                    }

                    return value.ToInvariantCultureToString();
                }

                return value;
            }

            return ((bool)value) ? "1" : "0";
        }

        return value;
    }

    return null;
}

internal T DeserializeRedisValue(byte[] valueRaw, Encoding encoding)
{
if (valueRaw == null)
{
return default(T);
}

Type typeFromHandle = typeof(T);
string text = typeFromHandle.ToString().TrimEnd(']');
switch (text)
{
    case "System.Byte[":
        return (T)Convert.ChangeType(valueRaw, typeFromHandle);
    case "System.String":
        return (T)Convert.ChangeType(encoding.GetString(valueRaw), typeFromHandle);
    case "System.Boolean[":
        return (T)Convert.ChangeType(valueRaw.Select((byte a) => a == 49).ToArray(), typeFromHandle);
    default:
        {
            if (valueRaw.Length == 0)
            {
                return default(T);
            }

            string text2 = null;
            if (typeFromHandle.IsValueType)
            {
                text2 = encoding.GetString(valueRaw);
                string text3 = (text.StartsWith("System.Nullable`1[") ? text.Substring(18) : text);
                bool flag = false;
                object obj = null;
                long result2;
                switch (text3)
                {
                    case "System.Boolean":
                        if (text2 == "1")
                        {
                            obj = true;
                        }
                        else if (text2 == "0")
                        {
                            obj = false;
                        }

                        break;
                    case "System.Byte":
                        {
                            if (byte.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result8))
                            {
                                obj = result8;
                            }

                            break;
                        }
                    case "System.Char":
                        if (text2.Length > 0)
                        {
                            obj = text2[0];
                        }

                        break;
                    case "System.Decimal":
                        {
                            if (decimal.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result4))
                            {
                                obj = result4;
                            }

                            break;
                        }
                    case "System.Double":
                        {
                            if (double.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result13))
                            {
                                obj = result13;
                            }

                            break;
                        }
                    case "System.Single":
                        {
                            if (float.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result10))
                            {
                                obj = result10;
                            }

                            break;
                        }
                    case "System.Int32":
                        {
                            if (int.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result6))
                            {
                                obj = result6;
                            }

                            break;
                        }
                    case "System.Int64":
                        if (long.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out result2))
                        {
                            obj = result2;
                        }

                        break;
                    case "System.SByte":
                        {
                            if (sbyte.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result14))
                            {
                                obj = result14;
                            }

                            break;
                        }
                    case "System.Int16":
                        {
                            if (short.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result12))
                            {
                                obj = result12;
                            }

                            break;
                        }
                    case "System.UInt32":
                        {
                            if (uint.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result11))
                            {
                                obj = result11;
                            }

                            break;
                        }
                    case "System.UInt64":
                        {
                            if (ulong.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result9))
                            {
                                obj = result9;
                            }

                            break;
                        }
                    case "System.UInt16":
                        {
                            if (ushort.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out var result7))
                            {
                                obj = result7;
                            }

                            break;
                        }
                    case "System.DateTime":
                        {
                            if (DateTime.TryParse(text2, out var result5))
                            {
                                obj = result5;
                            }

                            break;
                        }
                    case "System.DateTimeOffset":
                        {
                            if (DateTimeOffset.TryParse(text2, out var result3))
                            {
                                obj = result3;
                            }

                            break;
                        }
                    case "System.TimeSpan":
                        if (long.TryParse(text2, NumberStyles.Any, CultureInfo.InvariantCulture.NumberFormat, out result2))
                        {
                            obj = new TimeSpan(result2);
                        }

                        break;
                    case "System.Guid":
                        {
                            if (Guid.TryParse(text2, out var result))
                            {
                                obj = result;
                            }

                            break;
                        }
                    default:
                        flag = true;
                        break;
                }

                if (!flag)
                {
                    if (obj == null)
                    {
                        return default(T);
                    }

                    return (T)obj;
                }
            }

            if (Adapter.TopOwner.DeserializeRaw != null)
            {
                return (T)Adapter.TopOwner.DeserializeRaw(valueRaw, typeof(T));
            }

            if (text2 == null)
            {
                text2 = encoding.GetString(valueRaw);
            }

            if (Adapter.TopOwner.Deserialize != null)
            {
                return (T)Adapter.TopOwner.Deserialize(text2, typeof(T));
            }

            return text2.ConvertTo<T>();
        }
}

}

@2881099
Copy link
Owner

2881099 commented Feb 6, 2024

public static RedisClient cli = new RedisClient("127.0.0.1:6379,password=123,defaultDatabase=13");
cli.Serialize = obj => JsonConvert.SerializeObject(obj);
cli.Deserialize = (json, type) => JsonConvert.DeserializeObject(json, type);
cli.Notice += (s, e) => Console.WriteLine(e.Log); //print command log

@lyj251lyj
Copy link
Author

解决了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants