Example #1
0
        /// <summary>
        /// Serialize the <paramref name="instance"/> as a string and returns it.
        /// </summary>
        /// <param name="instance">The instance to serialize as a string.</param>
        /// <param name="settings">The serialization settings to use</param>
        /// <returns>A string version of the object.</returns>
        public static string ToSerializedString(object instance, SerializationSettings settings = null)
        {
            var sb = new StringBuilder(256);

            Serialize(instance, sb, settings);
            return(sb.ToString());
        }
Example #2
0
        /// <summary>
        /// Serialize the <paramref name="instance"/> as a string and returns it.
        /// </summary>
        /// <param name="instance">The instance to serialize as a string.</param>
        /// <param name="settings">The serialization settings to use</param>
        /// <returns>A string version of the object.</returns>
        public static string ToSerializedString(object instance, SerializationSettings settings = null)
        {
            var sb = new StringBuilder(256);

            Serialize(instance, new PrimitiveTextWriter(new StringWriter(sb, CultureInfo.InvariantCulture)), settings);
            return(sb.ToString());
        }
Example #3
0
        /// <summary>
        /// Serializes the specified object <paramref name="o"/> as a string.
        /// </summary>
        /// <param name="o">The object to serialize.</param>
        /// <param name="target">The target where the serialized version will be written.</param>
        /// <param name="settings">The serialization settings to use</param>
        public static void Serialize(object o, IPrimitiveWriter target, SerializationSettings settings = null)
        {
            var ow = new Writer(target)
            {
                Settings = settings
            };

            ow.Write(o);
        }
Example #4
0
        /// <summary>
        /// Serializes the specified object <paramref name="o"/> to a stream.
        /// </summary>
        /// <param name="o">The object to serialize.</param>
        /// <param name="target">The target where the serialized version will be written.</param>
        /// <param name="settings">The serialization settings to use</param>
        public static void Serialize(object o, Stream target, SerializationSettings settings = null)
        {
            var pw = new PrimitiveBinaryWriter(target);
            var ow = new ObjectWriter(pw)
            {
                Settings = settings
            };

            ow.Write(o);
        }
Example #5
0
        /// <summary>
        /// Serializes the specified object <paramref name="o"/> as a string.
        /// </summary>
        /// <param name="o">The object to serialize.</param>
        /// <param name="target">The target where the serialized version will be written.</param>
        /// <param name="settings">The serialization settings to use</param>
        public static void Serialize(object o, StringBuilder target, SerializationSettings settings = null)
        {
            var pw = new PrimitiveTextWriter(new StringWriter(target, CultureInfo.InvariantCulture));
            var ow = new ObjectWriter(pw)
            {
                Settings = settings
            };

            ow.Write(o);
        }
Example #6
0
        /// <summary>
        /// Perform a deep clone operation by serializing then deserializing an object.
        /// </summary>
        /// <typeparam name="T">The type of the object to clone.</typeparam>
        /// <param name="instance">The instance to clone.</param>
        /// <param name="settings">The settings to use with the <see cref="ObjectWriter"/></param>
        /// <returns>A deep clone of the <paramref name="instance"/>.</returns>
        public static T Clone <T>(T instance, SerializationSettings settings = null)
        {
            var ms = new List <object>(256);

            var pw = new TokenPrimitiveWriter(ms);
            var ow = new ObjectWriter(pw)
            {
                Settings = settings,
            };

            ow.Write(instance);

            var pr = new TokenPrimitiveReader(ms);
            var or = new ObjectReader(pr);

            var result = or.Read();

            return((T)result);
        }
Example #7
0
        /// <summary>
        /// Perform a deep clone operation by serializing then deserializing an object.
        /// </summary>
        /// <typeparam name="T">The type of the object to clone.</typeparam>
        /// <param name="instance">The instance to clone.</param>
        /// <param name="settings">The settings to use with the <see cref="ObjectWriter"/></param>
        /// <returns>A deep clone of the <paramref name="instance"/>.</returns>
        public static T Clone <T>(T instance, SerializationSettings settings = null)
        {
            var ms = new List <object>(256);

            var pw = new TokenPrimitiveWriter(ms);
            var ow = new Writer(pw)
            {
                Settings = new SerializationSettings
                {
                    IgnoreISerializable = settings?.IgnoreISerializable ?? true,
                    IgnoreTypeConverter = settings?.IgnoreTypeConverter ?? true,
                },
            };

            ow.Write(instance);

            var pr = new TokenPrimitiveReader(ms);
            var or = new Reader(pr);

            var result = or.Read();

            return((T)result);
        }
Example #8
0
 /// <summary>
 /// Serializes the specified object <paramref name="o"/> to a compressed gzip stream.
 /// </summary>
 /// <param name="o">The object to serialize.</param>
 /// <param name="target">The target where the serialized version will be written.</param>
 /// <param name="settings">The serialization settings to use</param>
 public static void ZipSerialize(object o, Stream stream, SerializationSettings settings = null)
 {
     using (var gzStream = new GZipStream(stream, CompressionMode.Compress, true))
         Serialize(o, new PrimitiveBinaryWriter(gzStream), settings);
 }
Example #9
0
 public static T Transmogrify <T>(object instance, SerializationSettings settings = null) => (T)Transmogrify(instance, typeof(T), settings);