예제 #1
0
        /// <summary>
        /// Adds an encoder for a specific type of element to this more
        /// general codec.
        /// </summary>
        /// <param name="element">A codec for a particular type of element.</param>
        /// <returns>
        /// A new codec that can encode and decode elements of type <typeparamref name="T"/>.
        /// </returns>
        public PiecewiseCodec <TObj> Add <T>(CodecElement <T, LNode> element)
            where T : TObj
        {
            var newDecoders = decoders.Add(
                element.Identifier,
                (data, state) => element.Decode(data, state));

            var newEncoders = encoders.Add(
                typeof(T),
                (obj, state) => element.Encode((T)obj, state));

            return(new PiecewiseCodec <TObj>(newDecoders, newEncoders));
        }
예제 #2
0
        /// <summary>
        /// Adds an encoder for a specific type of element to this more
        /// general codec.
        /// </summary>
        /// <param name="element">A codec for a particular type of element.</param>
        /// <returns>
        /// A new codec that can encode and decode elements of type <typeparamref name="T"/>.
        /// </returns>
        public PiecewiseCodec <TObj> Add <T>(CodecElement <T, IReadOnlyList <LNode> > element)
            where T : TObj
        {
            var newDecoders = decoders.Add(
                element.Identifier,
                (data, state) => element.Decode(data.Args, state));

            var newEncoders = encoders.Add(
                typeof(T),
                (obj, state) =>
                state.Factory.Call(
                    element.Identifier,
                    element.Encode((T)obj, state)));

            return(new PiecewiseCodec <TObj>(newDecoders, newEncoders));
        }