Example #1
0
 static void SerializeObject(object p_value, Utf8JsonWriter p_writer)
 {
     p_writer.WriteStartArray();
     p_writer.WriteStringValue("#" + SerializeTypeAlias.GetAlias(p_value.GetType()));
     JsonSerializer.Serialize(p_writer, p_value);
     p_writer.WriteEndArray();
 }
Example #2
0
        static void SerializeArray(IEnumerable p_value, Utf8JsonWriter p_writer)
        {
            p_writer.WriteStartArray();
            p_writer.WriteStringValue("&" + SerializeTypeAlias.GetAlias(p_value.GetType()));
            if (p_value is List <object> lo)
            {
                // 记录item类型
                foreach (object item in lo)
                {
                    p_writer.WriteStartArray();

                    if (item != null)
                    {
                        var tp = item.GetType();
                        if (tp.FullName == "System." + tp.Name)
                        {
                            // 简单类型
                            p_writer.WriteStringValue(tp.Name);
                        }
                        else if (tp.IsGenericType && tp.GetGenericTypeDefinition() == typeof(Nullable <>))
                        {
                            // 可空值类型
                            p_writer.WriteStringValue(tp.GetGenericArguments()[0].Name + "?");
                        }
                        else
                        {
                            // 复杂类型空即可
                            p_writer.WriteStringValue("");
                        }
                        Serialize(item, p_writer);
                    }
                    else
                    {
                        // null按string
                        p_writer.WriteStringValue("String");
                        p_writer.WriteNullValue();
                    }

                    p_writer.WriteEndArray();
                }
            }
            else
            {
                foreach (object item in p_value)
                {
                    Serialize(item, p_writer);
                }
            }
            p_writer.WriteEndArray();
        }