Example #1
0
        /// <summary>
        /// Creates a new instance of a <see cref="StreamObjectWriter"/>.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="knownObjects">An optional list of objects assumed known by the corresponding <see cref="StreamObjectReader"/>.</param>
        /// <param name="binder">A binder that provides object and type encoding.</param>
        /// <param name="recursive">True if the writer encodes objects recursively.</param>
        /// <param name="cancellationToken"></param>
        public StreamObjectWriter(
            Stream stream,
            ObjectData knownObjects = null,
            ObjectBinder binder = null,
            bool recursive = true,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            // String serialization assumes both reader and writer to be of the same endianness.
            // It can be adjusted for BigEndian if needed.
            Debug.Assert(BitConverter.IsLittleEndian);

            _writer = new BinaryWriter(stream, Encoding.UTF8);
            _referenceMap = new ReferenceMap(knownObjects);
            _binder = binder ?? FixedObjectBinder.Empty;
            _recursive = recursive;
            _cancellationToken = cancellationToken;

            WriteVersion();

            if (_recursive)
            {
                _writer.Write((byte)EncodingKind.Recursive);
            }
            else
            {
                _writer.Write((byte)EncodingKind.NonRecursive);
                _valueStack = s_variantStackPool.Allocate();
                _memberList = s_variantListPool.Allocate();
                _memberWriter = new VariantListWriter(_memberList);
            }
        }
Example #2
0
            private static ImmutableDictionary<object, int> CreateBaseMap(ObjectData data)
            {
                var builder = ImmutableDictionary<object, int>.Empty.ToBuilder();
                for (int i = 0; i < data.Objects.Length; i++)
                {
                    builder.Add(data.Objects[i], i);
                }

                return builder.ToImmutable();
            }
Example #3
0
 public ReferenceMap(ObjectData data)
     : this(data != null ? GetBaseMap(data) : null)
 {
 }
Example #4
0
            private static ImmutableDictionary<object, int> GetBaseMap(ObjectData data)
            {
                ImmutableDictionary<object, int> baseData;
                if (!s_baseDataMap.TryGetValue(data, out baseData))
                {
                    baseData = s_baseDataMap.GetValue(data, CreateBaseMap);
                }

                return baseData;
            }
Example #5
0
 public ReferenceMap(ObjectData baseData)
 {
     _baseData      = baseData;
     _baseDataCount = baseData != null ? _baseData.Objects.Length : 0;
     _values        = s_objectListPool.Allocate();
 }