Ejemplo n.º 1
0
        /// <summary>
        /// Deserialize to an <see cref="Expression"/> tree.
        /// </summary>
        /// <param name="json">The json text.</param>
        /// <param name="queryRoot">The root of the query to apply.</param>
        /// <param name="config">Optional configuration.</param>
        /// <returns>The deserialized <see cref="Expression"/> or <c>null</c>.</returns>
        /// <exception cref="ArgumentException">Thrown when the json is <c>null</c> or whitespace.</exception>
        public static Expression Deserialize(
            string json,
            Expression queryRoot = null,
            Action <IConfigurationBuilder> config = null)
        {
            Ensure.NotNullOrWhitespace(() => json);

            SerializationState state;

            if (config != null)
            {
                var builder = ServiceHost.GetService <IConfigurationBuilder>();
                config(builder);
                state = builder.Configure();
            }
            else
            {
                state = DefaultConfiguration.Value.GetDefaultState();
            }

            state.QueryRoot = queryRoot;

            var root = JsonSerializer.Deserialize <SerializationRoot>(json, state.Options);

            state.TypeIndex = new List <string>(
                root.TypeIndex ?? new string[0]);
            if (root.Expression is JsonElement jsonChild)
            {
                return(SerializerValue.Deserialize(jsonChild, state));
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deserialize to an <see cref="Expression"/> tree.
        /// </summary>
        /// <param name="root">The root expression.</param>
        /// <param name="queryRoot">The root of the query to apply.</param>
        /// <param name="config">Optional configuration.</param>
        /// <param name="stateCallback">Register a callback to inspect the state.</param>
        /// <returns>The deserialized <see cref="Expression"/> or <c>null</c>.</returns>
        /// <exception cref="ArgumentException">Thrown when the json is <c>null</c> or whitespace.</exception>
        public static Expression Deserialize(
            SerializationRoot root,
            Expression queryRoot = null,
            Action <IConfigurationBuilder> config     = null,
            Action <SerializationState> stateCallback = null)
        {
            SerializationState state;

            if (config != null)
            {
                var builder = ServiceHost.GetService <IConfigurationBuilder>();
                config(builder);
                state = builder.Configure();
            }
            else
            {
                state = DefaultConfiguration.Value.GetDefaultState();
            }

            state.QueryRoot = queryRoot;

            state.TypeIndex = new List <string>(
                root.TypeIndex ?? new string[0]);

            var result = SerializerValue.Deserialize(root.Expression, state);

            stateCallback?.Invoke(state);

            return(result);
        }
Ejemplo n.º 3
0
        private static Expression Roundtrip(Expression expr, IExpressionSerializer serializer)
        {
            var slim             = serializer.Lift(expr);
            var bonsai           = serializer.Serialize(slim);
            var roundtrippedSlim = serializer.Deserialize(bonsai);

            return(serializer.Reduce(roundtrippedSlim));
        }
 /// <summary>
 /// Deserializes JSON into a slim representation of an expression.
 /// </summary>
 /// <param name="json">The JSON to deserialize.</param>
 /// <returns>The deserialized slim expression.</returns>
 private ExpressionSlim DeserializeExpressionSlim(string json) => _expressionSerializer.Deserialize(json);