Esempio n. 1
0
        protected override void WriteObjectProperties(Utf8JsonWriter writer, TExpression instance, IReadOnlyCollection <Property> properties, JsonSerializerOptions options)
        {
            if (instance is ConstantExpression constantExpression)
            {
                writer.WritePropertyName(nameof(ConstantExpression.Type));
                writer.Serialize(constantExpression.Type, options);

                var type = constantExpression.Type?.ToType();
                if (constantExpression.Value is not null)
                {
                    var valueType = constantExpression.Value.GetType();
                    if (valueType != type)
                    {
                        type = valueType;
                        writer.WritePropertyName(ValueTypePropertyName);
                        writer.Serialize(type.AsTypeInfo(), options);
                    }
                }

                writer.WritePropertyName(nameof(ConstantExpression.Value));
                writer.Serialize(constantExpression.Value, options);
            }
            else
            {
                base.WriteObjectProperties(writer, instance, properties, options);
            }
        }
Esempio n. 2
0
        protected override void WriteObjectProperties(Utf8JsonWriter writer, VariableQueryArgumentList instance, IReadOnlyCollection <Property> properties, JsonSerializerOptions options)
        {
            writer.WritePropertyName(nameof(VariableQueryArgumentList.ElementType));
            writer.Serialize(instance.ElementType, options);

            writer.WritePropertyName(nameof(VariableQueryArgumentList.Values));
            writer.Serialize(instance.Values, options);
        }
Esempio n. 3
0
        public void WriteToJson(Utf8JsonWriter writer, WritedObjects objrefs)
        {
            writer.WriteBoolean(nameof(OrderByDesc), OrderByDesc);

            writer.WritePropertyName(nameof(Indexes));
            if (!HasIndexes)
            {
                writer.WriteEmptyArray();
            }
            else
            {
                writer.Serialize(Indexes.Where(t => t.PersistentState != Data.PersistentState.Deleted).ToArray(), objrefs);
            }

            //写入分区键集合
            writer.WritePropertyName(nameof(PartitionKeys));
            writer.WriteStartArray();
            if (HasPartitionKeys)
            {
                for (int i = 0; i < PartitionKeys.Length; i++)
                {
                    writer.WriteStartObject();
                    writer.WriteNumber("MemberId", PartitionKeys[i].MemberId);
                    writer.WriteBoolean("OrderByDesc", PartitionKeys[i].OrderByDesc);
                    writer.WriteNumber("Rule", (int)PartitionKeys[i].Rule);
                    writer.WriteNumber("RuleArg", PartitionKeys[i].RuleArgument);
                    writer.WriteEndObject();
                }
            }
            writer.WriteEndArray();
        }
Esempio n. 4
0
        void IJsonSerializable.WriteToJson(Utf8JsonWriter writer, WritedObjects objrefs)
        {
            writer.WriteString(nameof(ID), ID);
            writer.WriteNumber("Type", (int)NodeType);
            writer.WriteString("Text", Text);
            if (!(this is ModelNode))
            {
                writer.WritePropertyName("Nodes");
                writer.WriteStartArray();
                for (int i = 0; i < Nodes.Count; i++)
                {
                    writer.Serialize(Nodes[i], objrefs);
                }
                writer.WriteEndArray();
            }

            if (CheckoutInfo != null)
            {
                writer.WritePropertyName("CheckoutBy");
                if (IsCheckoutByMe)
                {
                    writer.WriteStringValue("Me");
                }
                else
                {
                    writer.WriteStringValue(CheckoutInfo.DeveloperName);
                }
            }

            WriteMembers(writer, objrefs);
        }
Esempio n. 5
0
 public static void WriteEnumerable(this Utf8JsonWriter writer, IEnumerable list, WritedObjects objrefs)
 {
     writer.WriteStartArray();
     foreach (var item in list)
     {
         writer.Serialize(item, objrefs);
     }
     writer.WriteEndArray();
 }
Esempio n. 6
0
 public static void WriteList(this Utf8JsonWriter writer, IList list, WritedObjects objrefs)
 {
     writer.WriteStartArray();
     for (int i = 0; i < list.Count; i++)
     {
         writer.Serialize(list[i], objrefs);
     }
     writer.WriteEndArray();
 }
Esempio n. 7
0
        public void WriteToJson(Utf8JsonWriter writer, WritedObjects objrefs)
        {
            writer.WritePropertyName("IsNew");
            writer.WriteBooleanValue(PersistentState == Data.PersistentState.Detached);

            //写入成员列表,注意不向前端发送EntityRef的隐藏成员及已标为删除的成员
            writer.WritePropertyName(nameof(Members));
            var q = from t in Members
                    where t.PersistentState != Data.PersistentState.Deleted &&
                    !(t is DataFieldModel && ((DataFieldModel)t).IsForeignKey)
                    select t;

            writer.Serialize(q.ToArray(), objrefs);

            //写入存储选项
            if (StoreOptions != null)
            {
                writer.WritePropertyName(nameof(StoreOptions));
                writer.Serialize(StoreOptions, objrefs);
            }
        }
Esempio n. 8
0
 public void WriteToJson(Utf8JsonWriter writer, WritedObjects objrefs)
 {
     writer.WriteNumber(nameof(ParentNodeType), ParentNodeType);
     writer.WriteString(nameof(ParentNodeID), ParentNodeID);
     writer.WritePropertyName(nameof(NewNode));
     writer.Serialize(NewNode, objrefs);
     if (!string.IsNullOrEmpty(RootNodeID))
     {
         writer.WriteString(nameof(RootNodeID), RootNodeID);
     }
     writer.WriteNumber(nameof(InsertIndex), InsertIndex);
 }
Esempio n. 9
0
        public static void Serialize(this Utf8JsonWriter writer, object res, WritedObjects objrefs)
        {
            if (res == null || res == DBNull.Value)
            {
                writer.WriteNullValue();
                return;
            }

            //先检查引用类型是否已序列化过
            var  type        = res.GetType();
            bool isRefObject = type.IsClass && type != typeof(string);

            if (isRefObject)
            {
                if (objrefs.TryGetValue(res, out string refID))
                {
                    if (!string.IsNullOrEmpty(refID))
                    {
                        writer.WriteStartObject();
                        writer.WritePropertyName(RefPropertyName);
                        writer.WriteStringValue(refID);
                        writer.WriteEndObject();
                        return;
                    }
                    throw new Exception("Json序列化存在重复引用");
                }
                if (res is Entity entity && !entity.Model.IsDTO)
                {
                    refID = $"{(byte)entity.PersistentState}{entity.ModelId}{entity.Id}";
                }
                objrefs.Add(res, refID);
            }

            if (res is IJsonSerializable serializable)
            {
                //开始写入对象
                if (serializable.JsonPayloadType != PayloadType.JsonResult)
                {
                    writer.WriteStartObject();
                }
                if (serializable.JsonPayloadType != PayloadType.UnknownType && serializable.JsonPayloadType != PayloadType.JsonResult)
                {
                    writer.WritePropertyName(TypePropertyName);             // 写入对象类型
                    if (serializable.JsonPayloadType == PayloadType.Entity) //实体类写入的是持久化状态及模型标识
                    {
                        var entity = (Entity)res;
                        writer.WriteStringValue($"{(byte)entity.PersistentState}{entity.ModelId}");
                        if (entity.Model.SysStoreOptions != null)
                        {
                            writer.WritePropertyName(IdPropertyName); // 写入对象标识
                            writer.WriteStringValue((Guid)entity.Id);
                        }
                        else if (entity.Model.SqlStoreOptions != null)
                        {
                            //TODO:写入$Id标识,从WriteObjectRefs内累加标识计数器
                        }
                    }
                    else if (serializable.JsonPayloadType == PayloadType.ExtKnownType) //扩展类型写入反射信息
                    {
                        writer.WriteStringValue(type.FullName);
                    }
                    else
                    {
                        writer.WriteNumberValue((int)serializable.JsonPayloadType);
                    }
                }

                // 开始调用实现
                serializable.WriteToJson(writer, objrefs);
                //结束写入对象
                if (serializable.JsonPayloadType != PayloadType.JsonResult)
                {
                    writer.WriteEndObject();
                }
            }
            else //----非实现了IJsonSerializable的对象---
            {
                if (type.IsPrimitive || type == typeof(string) || type == typeof(DateTime) ||
                    type == typeof(Guid) || type == typeof(decimal))
                {
                    System.Text.Json.JsonSerializer.Serialize(writer, res, type);
                }
                else if (res is IList) // 优先转换IList集合
                {
                    WriteList(writer, (IList)res, objrefs);
                }
                else if (res is IEnumerable) // 其他集合
                {
                    WriteEnumerable(writer, (IEnumerable)res, objrefs);
                }
                else // 转换其他类型如匿名类和没有继承IJsonSerializable接口的类
                {
                    //todo:***** 暂反射简单实现,另考虑缓存
                    var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                    writer.WriteStartObject();
                    for (int i = 0; i < properties.Length; i++)
                    {
                        writer.WritePropertyName(properties[i].Name);
                        writer.Serialize(properties[i].GetValue(res), objrefs);
                    }
                    writer.WriteEndObject();
                }
            }
        }
Esempio n. 10
0
        void IJsonSerializable.WriteToJson(Utf8JsonWriter writer, WritedObjects objrefs)
        {
            var model = Model;
            EntityMemberModel mm;

            for (int i = 0; i < model.Members.Count; i++)
            {
                mm = model.Members[i];
                ref EntityMember m = ref GetMember(mm.Name);
                switch (mm.Type)
                {
                case EntityMemberType.EntityRef:
                    if (m.HasValue)     //注意:无加载值不写入给前端
                    {
                        writer.WritePropertyName(mm.Name);
                        writer.Serialize(m.ObjectValue, objrefs);
                    }
                    break;

                case EntityMemberType.EntitySet:
                    if (m.HasValue)     //注意:无加载值不写入给前端
                    {
                        writer.WritePropertyName(mm.Name);
                        writer.WriteList((IList)m.ObjectValue, objrefs);
                    }
                    break;

                case EntityMemberType.EntityRefDisplayText:
                    if (m.HasValue)     //注意:无加载值不写入给前端
                    {
                        writer.WritePropertyName(mm.Name);
                        writer.WriteStringValue((string)m.ObjectValue);
                    }
                    break;

                case EntityMemberType.DataField:
                    writer.WritePropertyName(mm.Name);
                    if (m.HasValue)
                    {
                        switch (m.ValueType)
                        {
                        case EntityFieldType.Binary:
                            writer.WriteBase64StringValue((byte[])m.ObjectValue); break;

                        case EntityFieldType.Boolean:
                            writer.WriteBooleanValue(m.BooleanValue); break;

                        case EntityFieldType.Byte:
                            writer.WriteNumberValue(m.ByteValue); break;

                        case EntityFieldType.DateTime:
                            writer.WriteStringValue(m.DateTimeValue); break;

                        case EntityFieldType.Decimal:
                            writer.WriteNumberValue(m.DecimalValue); break;

                        case EntityFieldType.Double:
                            writer.WriteNumberValue(m.DoubleValue); break;

                        case EntityFieldType.Enum:
                            writer.WriteNumberValue(m.Int32Value); break;

                        case EntityFieldType.Float:
                            writer.WriteNumberValue(m.FloatValue); break;

                        case EntityFieldType.Guid:
                            writer.WriteStringValue(m.GuidValue); break;

                        case EntityFieldType.EntityId:
                            writer.WriteStringValue((Guid)((EntityId)m.ObjectValue)); break;

                        case EntityFieldType.Int16:
                            writer.WriteNumberValue(m.Int16Value); break;

                        case EntityFieldType.UInt16:
                            writer.WriteNumberValue(m.UInt16Value); break;

                        case EntityFieldType.Int32:
                            writer.WriteNumberValue(m.Int32Value); break;

                        case EntityFieldType.UInt32:
                            writer.WriteNumberValue(m.UInt32Value); break;

                        case EntityFieldType.Int64:
                            writer.WriteNumberValue(m.Int64Value); break;

                        case EntityFieldType.UInt64:
                            writer.WriteStringValue(m.UInt64Value.ToString()); break;         //暂序列化为字符串

                        case EntityFieldType.String:
                            writer.WriteStringValue((string)m.ObjectValue); break;

                        default: throw new NotSupportedException();
                        }
                    }
                    else
                    {
                        writer.WriteNullValue();
                    }
                    break;

                default:
                    throw ExceptionHelper.NotImplemented();
                    //writer.WritePropertyName(m.Name);
                    //writer.Serialize(m.BoxedValue);
                    //break;
                }
            }
Esempio n. 11
0
        /// <summary>
        /// 将调用服务的结果AnyValue序列化Json
        /// </summary>
        /// <param name="isResponseError">仅适用于子进程</param>
        public static bool SerializeAsInvokeResponse(this AnyValue obj, Stream stream,
                                                     int msgId, bool isResponseError = false)
        {
            try
            {
                using var jw = new Utf8JsonWriter(stream);
                jw.WriteStartObject();
                jw.WriteNumber(ResponseIdPropertyName.AsSpan(), msgId);

                if (isResponseError || (obj.Type == AnyValueType.Object && obj.ObjectValue is Exception))
                {
                    jw.WriteString(ResponseErrorPropertyName.AsSpan(),
                                   isResponseError ? (string)obj.ObjectValue : ((Exception)obj.ObjectValue).Message);
                }
                else
                {
                    jw.WritePropertyName(ResponseDataPropertyName.AsSpan());
                    switch (obj.Type)
                    {
                    case AnyValueType.Empty: jw.WriteNullValue(); break;

                    case AnyValueType.Boolean: jw.WriteBooleanValue(obj.BooleanValue); break;

                    case AnyValueType.Byte: jw.WriteNumberValue(obj.ByteValue); break;

                    case AnyValueType.Int16: jw.WriteNumberValue(obj.Int16Value); break;

                    case AnyValueType.UInt16: jw.WriteNumberValue(obj.UInt16Value); break;

                    case AnyValueType.Int32: jw.WriteNumberValue(obj.Int32Value); break;

                    case AnyValueType.UInt32: jw.WriteNumberValue(obj.UInt32Value); break;

                    case AnyValueType.Float: jw.WriteNumberValue(obj.FloatValue); break;

                    case AnyValueType.Double: jw.WriteNumberValue(obj.DoubleValue); break;

                    case AnyValueType.Decimal: jw.WriteNumberValue(obj.DecimalValue); break;

                    //暂Int64 & UInt64转换为字符串
                    case AnyValueType.Int64: jw.WriteStringValue(obj.Int64Value.ToString()); break;

                    case AnyValueType.UInt64: jw.WriteStringValue(obj.UInt64Value.ToString()); break;

                    case AnyValueType.DateTime: jw.WriteStringValue(obj.DateTimeValue); break;

                    case AnyValueType.Guid: jw.WriteStringValue(obj.GuidValue); break;

                    case AnyValueType.Object:
                        var objrefs = new WritedObjects();
                        jw.Serialize(obj.ObjectValue, objrefs);
                        break;
                    }
                }

                jw.WriteEndObject();
                jw.Flush();
                return(true);
            }
            catch (Exception ex)
            {
                Log.Warn($"Serialize error: {ExceptionHelper.GetExceptionDetailInfo(ex)}");
                return(false);
            }
        }