Exemple #1
0
        public void WriteField(ref Writer writer, SerializerSession session, uint fieldIdDelta, Type expectedType, TField value)
        {
            // If the value is null then we will not be able to get its type in order to get a concrete codec for it.
            // Therefore write the null reference and exit.
            if (value is null)
            {
                ReferenceCodec.TryWriteReferenceField(ref writer, session, fieldIdDelta, expectedType, null);
                return;
            }

            var fieldType          = value.GetType();
            var specificSerializer = session.CodecProvider.GetCodec(fieldType);

            if (specificSerializer != null)
            {
                specificSerializer.WriteField(ref writer, session, fieldIdDelta, expectedType, value);
            }
            else
            {
                ThrowSerializerNotFound(fieldType);
            }
        }
Exemple #2
0
        public void WriteField(ref Writer writer, SerializerSession session, uint fieldIdDelta, Type expectedType, object value)
        {
            if (ReferenceCodec.TryWriteReferenceField(ref writer, session, fieldIdDelta, expectedType, value))
            {
                return;
            }
            var type = value.GetType();

            writer.WriteFieldHeader(session, fieldIdDelta, expectedType, CodecType, WireType.TagDelimited);
            this.typeCodec.WriteField(ref writer, session, 0, typeof(Type), type);

            if (type.IsValueType)
            {
                var serializer = this.valueTypeSerializerFactory.GetSerializer(type);
                serializer.WriteValue(ref writer, session, value);
            }
            else
            {
                this.objectSerializer.WriteValue(ref writer, session, value);
            }

            writer.WriteEndObject();
        }
Exemple #3
0
    /// <inheritdoc/>
    void IFieldCodec <object> .WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, object value)
    {
        if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value))
        {
            return;
        }

        // The schema type when serializing the field is the type of the codec.
        // In practice it could be any unique type as long as this codec is registered as the handler.
        // By checking against the codec type in IsSupportedType, the codec could also just be registered as an IGenericCodec.
        // Note that the codec is responsible for serializing the type of the value itself.
        writer.WriteFieldHeader(fieldIdDelta, expectedType, SelfType, WireType.TagDelimited);

        var type = value.GetType();

        // Write the type name
        ReferenceCodec.MarkValueField(writer.Session);
        writer.WriteFieldHeader(0, typeof(byte[]), typeof(byte[]), WireType.LengthPrefixed);
        writer.Session.TypeCodec.WriteLengthPrefixed(ref writer, type);

        // Write the serialized payload
        // Note that the Utf8JsonWriter and PooledArrayBufferWriter could be pooled as long as they're correctly
        // reset at the end of each use.
        var bufferWriter = new BufferWriterBox <PooledArrayBufferWriter>(new PooledArrayBufferWriter());

        using var jsonWriter = new Utf8JsonWriter(bufferWriter);
        JsonSerializer.Serialize(jsonWriter, value, _options.SerializerOptions);
        jsonWriter.Flush();

        ReferenceCodec.MarkValueField(writer.Session);
        writer.WriteFieldHeader(1, typeof(byte[]), typeof(byte[]), WireType.LengthPrefixed);
        writer.WriteVarUInt32((uint)bufferWriter.Value.Length);
        bufferWriter.Value.CopyTo(ref writer);
        bufferWriter.Value.Reset();

        writer.WriteEndObject();
    }
        public void WriteField <TBufferWriter>(ref Writer <TBufferWriter> writer, uint fieldIdDelta, Type expectedType, object value) where TBufferWriter : IBufferWriter <byte>
        {
            if (ReferenceCodec.TryWriteReferenceField(ref writer, fieldIdDelta, expectedType, value))
            {
                return;
            }

            var type = value.GetType();

            writer.WriteFieldHeader(fieldIdDelta, expectedType, CodecType, WireType.TagDelimited);
            _typeCodec.WriteField(ref writer, 0, typeof(Type), type);

            if (type.IsValueType)
            {
                var serializer = _valueTypeSerializerFactory.GetSerializer(type);
                serializer.WriteValue(ref writer, value);
            }
            else
            {
                _objectSerializer.WriteValue(ref writer, value);
            }

            writer.WriteEndObject();
        }