/// <summary> /// Compiles the roundtrip-based rule. /// </summary> private void Initialize() { lock (_initializationLock) { if (_pair == null) { _pair = RoundtripFindMembers <T> .Extract(_roundtrip, _parent._wrap, _parent._unwrap); _roundtrip = null; _parent = null; } } }
/// <summary> /// Creates a new serializer with the given configuration parameters. /// </summary> /// <param name="tag">Function to tag serialization output with a rule identifier.</param> /// <param name="untag">Function to extract a rule identifier and a payload from serialization output.</param> /// <param name="newContext">Function to get a new serialization context during serialization.</param> /// <param name="addContext">Function to add serialization context to the serialization output.</param> /// <param name="getContext">Function to get deserialization context from deserialization input.</param> /// <param name="ruleTable">Rule table with serialization and deserialization rules.</param> public SerializerBase(Func <TaggedByRule <TOutput>, TOutput> tag, Func <TOutput, TaggedByRule <TOutput> > untag, Func <TContext> newContext, Func <Contextual <TContext, TOutput>, TOutput> addContext, Func <TOutput, Contextual <TContext, TOutput> > getContext, RuleTableBase <TInput, TOutput, TContext> ruleTable) { _tag = tag ?? throw new ArgumentNullException(nameof(tag)); _untag = untag ?? throw new ArgumentNullException(nameof(untag)); _newContext = newContext ?? throw new ArgumentNullException(nameof(newContext)); _addContext = addContext ?? throw new ArgumentNullException(nameof(addContext)); _getContext = getContext ?? throw new ArgumentNullException(nameof(getContext)); Rules = ruleTable ?? throw new ArgumentNullException(nameof(ruleTable)); }
/// <summary> /// Combines this rule table with the given rule table. /// </summary> /// <param name="second">Rule table to combine with the current table instance.</param> /// <returns>Concatenation of current table instance with the given second rule table instance.</returns> public RuleTableBase <TInput, TOutput, TContext> Concat(RuleTableBase <TInput, TOutput, TContext> second) { return(new RuleTableConcat <TInput, TOutput, TContext>(this, second, _wrap, _unwrap)); }
/// <summary> /// Creates a new thunk for lazy initialization of a roundtrip-based rule. /// </summary> /// <param name="name">Name for the rule to add to the serializer's rule table. This name needs to be unique and should be match on the serializer and deserializer sides.</param> /// <param name="filter">Predicate function to evaluate whether the rule is applicable for a given object during serialization.</param> /// <param name="roundtrip">Roundtrip function containing member lookups for recursive serialization and deserialization.</param> /// <param name="parent">Reference to the containing rule table.</param> public RoundtripRuleThunk(string name, Func <T, bool> filter, Expression <Func <T, T> > roundtrip, RuleTableBase <TInput, TOutput, TContext> parent) : base(name, filter) { _roundtrip = roundtrip; _parent = parent; }
/// <summary> /// Creates a read-only view on a rule table. /// </summary> /// <param name="baseTable">Base table to provide a read-only view over.</param> /// <param name="wrap">Function to wrap a series of recursively acquired serialization outputs.</param> /// <param name="unwrap">Function to unwrap a piece of recursive serialization output.</param> public RuleTableReadOnly(RuleTableBase <TInput, TOutput, TContext> baseTable, Func <IDictionary <string, Expression>, Expression> wrap, Func <Expression, string, Expression> unwrap) : base(wrap, unwrap) { _baseTable = baseTable; }
/// <summary> /// Creates a concatenation of two rule tables. /// </summary> /// <param name="first">First table included in the concatenation.</param> /// <param name="second">Second table included in the concatenation.</param> /// <param name="wrap">Function to wrap a series of recursively acquired serialization outputs.</param> /// <param name="unwrap">Function to unwrap a piece of recursive serialization output.</param> public RuleTableConcat(RuleTableBase <TInput, TOutput, TContext> first, RuleTableBase <TInput, TOutput, TContext> second, Func <IDictionary <string, Expression>, Expression> wrap, Func <Expression, string, Expression> unwrap) : base(wrap, unwrap) { _first = first; _second = second; }
/// <summary> /// Creates an extension table layered on a rule table. /// </summary> /// <param name="baseTable">Base table to provide a table extension for.</param> /// <param name="wrap">Function to wrap a series of recursively acquired serialization outputs.</param> /// <param name="unwrap">Function to unwrap a piece of recursive serialization output.</param> public RuleTableExtension(RuleTableBase <TInput, TOutput, TContext> baseTable, Func <IDictionary <string, Expression>, Expression> wrap, Func <Expression, string, Expression> unwrap) : base(wrap, unwrap) { _baseTable = baseTable; _extension = new RuleTable <TInput, TOutput, TContext>(wrap, unwrap); }