public SerializationWriterContext(ISerializationWriter writer, SerializationContext serializationContext, object container, object value, MemberInfo member, int index, int depth, bool isCircularReference, bool isCollection) { if(writer == null) throw new ArgumentNullException("writer"); if(serializationContext == null) throw new ArgumentNullException("serializationContext"); _writer = writer; _serializationContext = serializationContext; _value = value; _index = index; _depth = depth; _member = member; _container = container; _terminated = value == null || value.GetType().IsPrimitive; _isCircularReference = isCircularReference; _isCollection = isCollection; }
protected TextWriter GetWriter(SerializationContext context) { if(context == null) return null; if(_writers == null) System.Threading.Interlocked.CompareExchange(ref _writers, new ConcurrentDictionary<SerializationContext, TextWriter>(), null); TextWriter writer; if(!_writers.TryGetValue(context, out writer)) { writer = this.CreateTextWriter(context); if(writer != null) _writers.Add(context, writer); } return writer; }
protected virtual TextWriter CreateTextWriter(SerializationContext context) { return new StreamWriter(context.SerializationStream, _encoding, 1024 * 32, true); }
protected virtual void OnSerialized(SerializationContext context) { var writer = this.GetWriter(context); if(writer != null) { writer.Flush(); writer.Dispose(); } }
protected virtual void OnSerializing(SerializationContext context) { }
void ISerializationWriter.OnSerialized(SerializationContext context) { this.OnSerialized(context); var writers = _writers; if(writers != null) writers.Remove(context); }
void ISerializationWriter.OnSerializing(SerializationContext context) { this.OnSerializing(context); }