Пример #1
0
        public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, Dictionary <TKey, TValue> value) where TBufferWriter : IBufferWriter <byte>
        {
            if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value))
            {
                return;
            }

            writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited);

            if (value.Comparer != EqualityComparer <TKey> .Default)
            {
                _comparerCodec.WriteField(ref writer, 0, typeof(IEqualityComparer <TKey>), value.Comparer);
            }

            Int32Codec.WriteField(ref writer, 1, typeof(int), value.Count);

            uint innerFieldIdDelta = 1;

            foreach (var element in value)
            {
                _pairCodec.WriteField(ref writer, innerFieldIdDelta, CodecFieldType, element);
                innerFieldIdDelta = 0;
            }

            writer.WriteEndObject();
        }
Пример #2
0
        public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, List <T> value) where TBufferWriter : IBufferWriter <byte>
        {
            if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value))
            {
                return;
            }

            writer.WriteFieldHeader(fieldIdDelta, expectedType, value.GetType(), WireType.TagDelimited);

            Int32Codec.WriteField(ref writer, 0, Int32Codec.CodecFieldType, value.Count);
            uint innerFieldIdDelta = 1;

            foreach (var element in value)
            {
                _fieldCodec.WriteField(ref writer, innerFieldIdDelta, CodecElementType, element);
                innerFieldIdDelta = 0;
            }

            writer.WriteEndObject();
        }
        /// <inheritdoc />
        public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, object value) where TBufferWriter : IBufferWriter <byte>
        {
            uint           type;
            CompareOptions compareOptions = default;
            CompareInfo    compareInfo    = default;

            if (value is null)
            {
                type = 0;
            }
            else
            {
#if NET6_0_OR_GREATER
                var comparer = (IEqualityComparer <string>)value;
                if (StringComparer.IsWellKnownOrdinalComparer(comparer, out var ignoreCase))
                {
                    // Ordinal. This also handles EqualityComparer<string>.Default.
                    type = 1;
                    if (ignoreCase)
                    {
                        compareOptions = CompareOptions.IgnoreCase;
                    }
                }
                else if (StringComparer.IsWellKnownCultureAwareComparer(comparer, out compareInfo, out compareOptions))
                {
                    type = 2;
                }
                else
                {
                    ThrowNotSupported(value.GetType());
                    return;
                }
#else
                var isOrdinal           = _ordinalComparer.Equals(value) || _defaultEqualityComparer.Equals(value);
                var isOrdinalIgnoreCase = _ordinalIgnoreCaseComparer.Equals(value);
                if (isOrdinal)
                {
                    type = 1;
                }
                else if (isOrdinalIgnoreCase)
                {
                    type           = 1;
                    compareOptions = CompareOptions.IgnoreCase;
                }
                else if (TryGetWellKnownCultureAwareComparerInfo(value, out compareInfo, out compareOptions, out var ignoreCase))
                {
                    type = 2;
                    if (ignoreCase)
                    {
                        compareOptions |= CompareOptions.IgnoreCase;
                    }
                }
                else
                {
                    ThrowNotSupported(value.GetType());
                    return;
                }
#endif
            }

            ReferenceCodec.MarkValueField(writer.Session);
            writer.WriteFieldHeader(fieldIdDelta, expectedType, typeof(WellKnownStringComparerCodec), WireType.TagDelimited);

            UInt32Codec.WriteField(ref writer, 0, typeof(int), type);
            UInt64Codec.WriteField(ref writer, 1, typeof(ulong), (ulong)compareOptions);

            if (compareInfo is not null)
            {
                Int32Codec.WriteField(ref writer, 1, typeof(int), compareInfo.LCID);
            }

            writer.WriteEndObject();
        }