/// <summary>
        /// Serializes an expression to Bonsai.
        /// </summary>
        /// <param name="expression">Expression to serialize.</param>
        /// <returns>Bonsai representation of the given expression.</returns>
        public Json.Expression Serialize(ExpressionSlim expression)
        {
            if (_mergeContext && _serializationState != null)
            {
                return(new SerializerImpl(_serializationState, _liftFactory).Visit(expression));
            }
            else
            {
                var state = new SerializationState(_version);

                _serializationState = state;

                var body    = new SerializerImpl(state, _liftFactory).Visit(expression);
                var context = state.ToJson();

                _serializationState = null;

                // PERF: We could consider creating a specialized IDictionary for the
                //       top-level JSON structure to avoid Dictionary allocation.

                return(Json.Expression.Object(new Dictionary <string, Json.Expression>(2)
                {
                    { "Context", context },
                    { "Expression", body },
                }));
            }
        }
        /// <summary>
        /// Serializes an expression to Bonsai.
        /// </summary>
        /// <param name="expression">Expression to serialize.</param>
        /// <returns>Bonsai representation of the given expression.</returns>
        private Json.Expression JsonSerialize(ExpressionSlim expression)
        {
            var serializationState = new SerializationState(_version);
            var body    = new SerializerImpl(serializationState, GetConstantSerializerDelegate).Visit(expression);
            var context = serializationState.ToJson();

            // PERF: We could consider creating a specialized IDictionary for the
            //       top-level JSON structure to avoid Dictionary allocation.

            return(Json.Expression.Object(new Dictionary <string, Json.Expression>(2)
            {
                { "Context", context },
                { "Expression", body },
            }));
        }
 protected ExpressionSlimToBonsaiConverter(SerializationState state)
 {
     _state = state;
 }
 public SerializerImpl(SerializationState state, Func <Type, Func <object, Json.Expression> > liftFactory)
     : base(state)
 {
     _liftFactory = liftFactory;
 }