/// <summary>
        /// Serializes an unknown object type.
        /// </summary>
        /// <typeparam name="T">The type of the object. This may not reflect the exact object type.</typeparam>
        /// <param name="value">The object value.</param>
        /// <returns>The serialized <see cref="JToken"/>.</returns>
        protected virtual JToken SerializeUnknown <T>(T value)
        {
            if (ReferenceEquals(value, null))
            {
                return(null);
            }

            var mySerializers = _mySerializers;

            if (mySerializers == null)
            {
                Thread.MemoryBarrier();
                mySerializers  = _serializers.GetOrAdd(GetType().TypeHandle, th => new SerializerInfo(Type.GetTypeFromHandle(th)));
                _mySerializers = mySerializers;
            }

            return(_mySerializers.Serialize(this, value));
        }
Beispiel #2
0
        /// <summary>
        /// Serializes an unknown object type.
        /// </summary>
        /// <typeparam name="T">The type of the object. This may not reflect the exact object type.</typeparam>
        /// <param name="value">The object value.</param>
        /// <returns>The serialized <see cref="JsonValue"/>.</returns>
        protected virtual JsonValue SerializeUnknown <T>(T value)
        {
#pragma warning disable S1168 // Empty arrays and collections should be returned instead of null
            // Null is significant in JSON.

            if (ReferenceEquals(value, null))
            {
                return(null);
            }
#pragma warning restore S1168 // Empty arrays and collections should be returned instead of null

            var mySerializers = _mySerializers;
            if (mySerializers == null)
            {
                Thread.MemoryBarrier();
                mySerializers  = _serializers.GetOrAdd(GetType().TypeHandle, th => new SerializerInfo(Type.GetTypeFromHandle(th)));
                _mySerializers = mySerializers;
            }

            return(_mySerializers.Serialize(this, value));
        }