Exemplo n.º 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)));
        }
Exemplo n.º 2
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);
        }