예제 #1
0
        private bool SerializeObjectWithProperties(object value, StringBuilder destination, JsonSerializeOptions options, ref SingleItemOptimizedHashSet <object> objectsInPath, int depth)
        {
            if (depth < options.MaxRecursionLimit)
            {
                var objectPropertyList = _objectReflectionCache.LookupObjectProperties(value);
                if (!objectPropertyList.ConvertToString)
                {
                    if (ReferenceEquals(options, Instance._serializeOptions) && value is Exception)
                    {
                        // Exceptions are seldom under control, and can include random Data-Dictionary-keys, so we sanitize by default
                        options = Instance._exceptionSerializeOptions;
                    }

                    using (new SingleItemOptimizedHashSet <object> .SingleItemScopedInsert(value, ref objectsInPath, false, _referenceEqualsComparer))
                    {
                        return(SerializeObjectProperties(objectPropertyList, destination, options, objectsInPath, depth));
                    }
                }
            }

            return(SerializeObjectAsString(value, destination, options));
        }
예제 #2
0
        private bool SerializeObjectWithProperties(object value, StringBuilder destination, JsonSerializeOptions options, ref SingleItemOptimizedHashSet <object> objectsInPath, int depth)
        {
            int originalLength = destination.Length;

            if (originalLength > MaxJsonLength)
            {
                return(false);
            }

            if (depth < options.MaxRecursionLimit)
            {
                try
                {
                    if (ReferenceEquals(options, instance._serializeOptions) && value is Exception)
                    {
                        // Exceptions are seldom under control, and can include random Data-Dictionary-keys, so we sanitize by default
                        options = instance._exceptionSerializeOptions;
                    }

                    var objectPropertyList = _objectReflectionCache.LookupObjectProperties(value);
                    using (new SingleItemOptimizedHashSet <object> .SingleItemScopedInsert(value, ref objectsInPath, false, _referenceEqualsComparer))
                    {
                        return(SerializeObjectProperties(objectPropertyList, destination, options, objectsInPath, depth));
                    }
                }
                catch
                {
                    //nothing to add, so return is OK
                    destination.Length = originalLength;
                    return(false);
                }
            }
            else
            {
                return(SerializeObjectAsString(value, TypeCode.Object, destination, options));
            }
        }