Beispiel #1
0
        /// <summary>
        /// Returns true if object name should be ignored
        /// </summary>
        /// <param name="name">Property or field name</param>
        /// <param name="path">Full path to object</param>
        /// <param name="options">Comparison options</param>
        /// <param name="ignorePropertiesOrPaths">List of names or paths to ignore</param>
        /// <returns></returns>
        protected bool IgnoreObjectName(string name, string path, IEnumerable<CustomAttributeData> attributes = null)
        {
            var ignoreByNameOrPath = _ignorePropertiesOrPaths?.Contains(name) == true || _ignorePropertiesOrPaths?.Contains(path) == true;
            if (ignoreByNameOrPath)
            {
                return true;
            }
#if FEATURE_CUSTOM_ATTRIBUTES
            if (attributes?.Any(x => !_options.BitwiseHasFlag(SerializerOptions.DisableIgnoreAttributes) && (_ignoreAttributes.Contains(x.AttributeType) || _ignoreAttributes.Contains(x.AttributeType.Name))) == true)
            {
                return true;
            }
#else
            if (attributes?.Any(x => !_options.BitwiseHasFlag(SerializerOptions.DisableIgnoreAttributes) && (_ignoreAttributes.Contains(x.Constructor.DeclaringType) || _ignoreAttributes.Contains(x.Constructor.DeclaringType.Name))) == true)
            {
                return true;
            }
#endif
            return false;
        }
Beispiel #2
0
        /// <summary>
        /// Inspect an object and serialize its contents
        /// </summary>
        /// <param name="sourceObject"></param>
        /// <param name="currentDepth"></param>
        /// <param name="maxDepth"></param>
        /// <param name="options">The serialization options</param>
        /// <param name="objectTree"></param>
        /// <param name="ignoreAttributes"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        internal byte[] InspectAndSerialize(object sourceObject, uint maxDepth, SerializerOptions options, ICollection <object> ignoreAttributes, ICollection <string> ignorePropertiesOrPaths = null)
        {
            if (sourceObject == null)
            {
                return(null);
            }

            var typeSupport = sourceObject.GetType().GetExtendedType();

            // drop any objects we are ignoring by attribute
            if (typeSupport.Attributes.Any(x => ignoreAttributes.Contains(x)))
            {
                return(null);
            }

            // for delegate types, return null
            if (typeSupport.IsDelegate)
            {
                return(null);
            }

            byte[]          dataBytes       = null;
            TypeDescriptors typeDescriptors = null;

            using (var stream = new MemoryStream())
            {
                using (var writer = new BinaryWriter(stream))
                {
                    typeDescriptors = TypeWriter.Write(writer, sourceObject, typeSupport, maxDepth, options, ignoreAttributes, out var diagnosticLog, ignorePropertiesOrPaths);
                    DiagnosticLog   = diagnosticLog;
                }
                dataBytes = stream.ToArray();
            }

            if (typeDescriptors != null)
            {
                dataBytes = BuildTypeDescriptorMap(dataBytes, typeDescriptors);
            }

            if (options.BitwiseHasFlag(SerializerOptions.Compress))
            {
#if FEATURE_COMPRESSION
                // enable data compression for strings
                dataBytes = CompressData(dataBytes);
#else
                throw new InvalidOperationException($"Compression is only available in .Net Framework 4.6+ and .Net Standard 1.6+");
#endif
            }

            return(dataBytes);
        }
Beispiel #3
0
        /// <summary>
        /// Write the parent object, and recursively process it's children
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="obj"></param>
        /// <param name="typeSupport"></param>
        /// <param name="maxDepth"></param>
        /// <param name="options">The serialization options</param>
        /// <param name="objectTree"></param>
        /// <param name="ignoreAttributes"></param>
        internal static TypeDescriptors Write(BinaryWriter writer, object obj, ExtendedType typeSupport, uint maxDepth, SerializerOptions options, ICollection <object> ignoreAttributes, out string diagnosticLog, ICollection <string> ignorePropertiesOrPaths = null)
        {
            var currentDepth = 0;

            diagnosticLog = string.Empty;
            TypeDescriptors typeDescriptors = null;

            if (options.BitwiseHasFlag(SerializerOptions.EmbedTypes))
            {
                typeDescriptors = new TypeDescriptors();
            }

            var dataSettings = SerializerDataSettings.None;

            if (typeDescriptors != null)
            {
                dataSettings |= SerializerDataSettings.TypeMap;
            }
            if (options.BitwiseHasFlag(SerializerOptions.Compact))
            {
                dataSettings |= SerializerDataSettings.Compact;
            }
            if (options.BitwiseHasFlag(SerializerOptions.Compress))
            {
                dataSettings |= SerializerDataSettings.Compress;
            }
            // write the serializer byte 0, data settings
            writer.Write((byte)dataSettings);

            var typeWriter = new TypeWriter(maxDepth, dataSettings, options, ignoreAttributes, typeDescriptors, ignorePropertiesOrPaths);

            typeWriter.WriteObject(writer, obj, typeSupport, currentDepth, string.Empty, 0);
            if (options.BitwiseHasFlag(SerializerOptions.WriteDiagnosticLog))
            {
                diagnosticLog = typeWriter.GetDiagnosticLog();
            }
            return(typeDescriptors);
        }