Exemple #1
0
        /// <summary>
        /// Deserializes a <see cref="LambdaExpression"/>.
        /// </summary>
        /// <param name="json">The serialized fragment.</param>
        /// <param name="state">State, such as <see cref="JsonSerializerOptions"/>, for the deserialization.</param>
        /// <returns>The <see cref="LambdaExpression"/>.</returns>
        public override LambdaExpression Deserialize(
            JsonElement json,
            SerializationState state)
        {
            var materializedType = GetMemberFromKey <Type>(
                json.GetProperty(nameof(Lambda.LambdaTypeKey))
                .GetString());
            var materializedReturnType = GetMemberFromKey <Type>(json.GetProperty(nameof(Lambda.ReturnTypeKey))
                                                                 .GetString());
            var body          = Serializer.Deserialize(json.GetProperty(nameof(Lambda.Body)), state);
            var name          = json.GetNullableProperty(nameof(Lambda.Name)).GetString();
            var list          = json.GetNullableProperty(nameof(Lambda.Parameters));
            var parameterList = list.ValueKind == JsonValueKind.Array ?
                                list.EnumerateArray().Select(element =>
                                                             Serializer.Deserialize(element, state) as ParameterExpression).ToList() :
                                new List <ParameterExpression>();

            return
                (AnonymousTypeAdapter.TransformLambda(
                     Expression.Lambda(materializedType, body, name, parameterList)));
        }
Exemple #2
0
        /// <summary>
        /// Serialize a <see cref="UnaryExpression"/> to a <see cref="Unary"/>.
        /// </summary>
        /// <param name="expression">The <see cref="UnaryExpression"/>.</param>
        /// <param name="state">State, such as <see cref="JsonSerializerOptions"/>, for the serialization.</param>
        /// <returns>The <see cref="Unary"/>.</returns>
        public override Unary Serialize(
            UnaryExpression expression,
            SerializationState state)
        {
            if (expression == null)
            {
                return(null);
            }

            var unary = new Unary(expression)
            {
                Operand = Serializer.Serialize(expression.Operand, state),
            };

            if (!string.IsNullOrWhiteSpace(unary.UnaryMethodKey))
            {
                unary.UnaryMethodKey = AnonymousTypeAdapter.MemberKeyTransformer(unary.UnaryMethodKey);
            }

            unary.UnaryTypeKey = AnonymousTypeAdapter.MemberKeyTransformer(unary.UnaryTypeKey);

            return(unary);
        }
Exemple #3
0
        /// <summary>
        /// Serialize a <see cref="LambdaExpression"/> to a <see cref="Lambda"/>.
        /// </summary>
        /// <param name="expression">The <see cref="LambdaExpression"/>.</param>
        /// <param name="state">State, such as <see cref="JsonSerializerOptions"/>, for the serialization.</param>
        /// <returns>The <see cref="Lambda"/>.</returns>
        public override Lambda Serialize(
            LambdaExpression expression,
            SerializationState state)
        {
            if (expression == null)
            {
                return(null);
            }

            expression = AnonymousTypeAdapter.TransformLambda(expression);

            var result = new Lambda(expression)
            {
                Body = Serializer.Serialize(expression.Body, state),
            };

            foreach (var parameter in expression.Parameters)
            {
                result.Parameters.Add(Serializer.Serialize(parameter, state) as Parameter);
            }

            return(result);
        }
        /// <summary>
        /// Serialize a <see cref="MethodCallExpression"/>.
        /// </summary>
        /// <param name="expression">The <see cref="MethodCallExpression"/> to serialize.</param>
        /// <param name="state">State, such as <see cref="JsonSerializerOptions"/>, for the serialization.</param>
        /// <returns>The serializable <see cref="MethodExpr"/>.</returns>
        public override MethodExpr Serialize(
            MethodCallExpression expression,
            SerializationState state)
        {
            if (expression == null)
            {
                return(null);
            }

            var method = new MethodExpr(expression)
            {
                MethodObject = Serializer.Serialize(expression.Object, state),
            };

            method.MethodInfoKey = AnonymousTypeAdapter.MemberKeyTransformer(method.MethodInfoKey);

            foreach (var arg in expression.Arguments)
            {
                method.Arguments.Add(Serializer.Serialize(arg, state));
            }

            return(method);
        }