コード例 #1
0
        public string GetFull(ICustomSerializable <Blog> serializableObject)
        {
            var blog       = serializableObject as Blog;
            var serializer = SerializerFactory.GetSerializer <Blog>();

            return(serializer.Serialize(blog));
        }
コード例 #2
0
ファイル: Serializer.cs プロジェクト: Vanmaru/Store
        public static ICustomSerializable Load(BinaryReader reader)
        {
            string FullName       = reader.ReadString();
            Type   newT           = Type.GetType(FullName);
            ICustomSerializable c = (ICustomSerializable)Activator.CreateInstance(newT, null);

            c.SetObjectData(reader);
            return(c);
        }
コード例 #3
0
ファイル: Serializer.cs プロジェクト: Vanmaru/Store
 public static void Save(string filePath, ICustomSerializable objToSerialize)
 {
     using (FileStream stream = new FileStream(filePath, FileMode.Create))
     {
         using (BinaryWriter writer = new BinaryWriter(stream))
         {
             Save(writer, objToSerialize);
         }
     }
 }
コード例 #4
0
        public string GetFullForExchange(ICustomSerializable <Blog> serializableObject)
        {
            var blog       = serializableObject as Blog;
            var serializer = SerializerFactory.GetSerializer <Blog>();

            return(serializer.Alias("Name", "Title")
                   .Alias("CommentsItem", "Comment")
                   .Alias("Date", "PublicationDate")
                   .Serialize(blog));
        }
コード例 #5
0
        public string GetHeader(ICustomSerializable <Blog> serializableObject)
        {
            var blog       = serializableObject as Blog;
            var serializer = SerializerFactory.GetSerializer <Blog>();

            return(serializer.Alias("Name", "Title")
                   .Ignore("Comments")
                   .Ignore("Date")
                   .Ignore("Author")
                   .Serialize(blog));
        }
コード例 #6
0
        bool DeserializeCustomSerializable(ref object instance, TypeSerializationArgs args)
        {
            if (instance == null)
            {
                instance = CreateInstance();
            }

            ICustomSerializable ics = instance as ICustomSerializable;

            ics.Deserialize(args.Reader);
            return(true);
        }
コード例 #7
0
 void SerializeCustomSerializable(object instance, TypeSerializationArgs args)
 {
     if (instance == null)
     {
         throw new ArgumentNullException("Cannot serialize null object with ICustomSerializable");
     }
     else
     {
         ICustomSerializable ics = instance as ICustomSerializable;
         ics.Serialize(args.Writer);
     }
 }
コード例 #8
0
        public void Add(ICustomSerializable obj, bool main = true)
        {
            bool notExists;
            long id = idGenerator.GetId(obj, out notExists);

            if (notExists)
            {
                objectDictionary.Add(id, obj.GetSerializationString(this));
            }
            if (main)
            {
                serializationString += id + ",";
            }
        }
コード例 #9
0
ファイル: Utilities.cs プロジェクト: ScriptBox21/dotnet-infer
        /// <summary>
        /// Persists an object that controls its binary serialization to a file with the specified name.
        /// </summary>
        /// <param name="obj">The object to be serialized.</param>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="fileMode">The mode used to open the file. Defaults to <see cref="FileMode.Create"/>.</param>
        /// <remarks>To load a saved learner, you can use the factory methods whose names start with LoadBackwardCompatible.</remarks>
        public static void SaveForwardCompatible(this ICustomSerializable obj, string fileName, FileMode fileMode = FileMode.Create)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (fileName == null)
            {
                throw new ArgumentNullException(nameof(fileName));
            }

            using (Stream stream = File.Open(fileName, fileMode))
            {
                obj.SaveForwardCompatible(stream);
            }
        }
コード例 #10
0
ファイル: Utilities.cs プロジェクト: ScriptBox21/dotnet-infer
        /// <summary>
        /// Persists an object that controls its binary serialization to the specified stream.
        /// </summary>
        /// <param name="obj">The object to be serialized.</param>
        /// <param name="stream">The serialization stream.</param>
        /// <remarks>To load a saved learner, you can use the factory methods whose names start with LoadBackwardCompatible.</remarks>
        public static void SaveForwardCompatible(this ICustomSerializable obj, Stream stream)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (var writer = new BinaryWriter(stream, Encoding.UTF8, true))
            {
                obj.SaveForwardCompatible(writer);
            }
        }
コード例 #11
0
ファイル: Utilities.cs プロジェクト: kant2002/infer
        /// <summary>
        /// Persists an object that controls its binary serialization to the specified stream.
        /// </summary>
        /// <param name="obj">The object to be serialized.</param>
        /// <param name="stream">The serialization stream.</param>
        /// <remarks>To load a saved learner, you can use the factory methods whose names start with LoadBackwardCompatible.</remarks>
        public static void SaveForwardCompatibleAsText(this ICustomSerializable obj, Stream stream)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            using (var writer = new WrappedTextWriter(new StreamWriter(stream)))
            {
                obj.SaveForwardCompatible(writer);
            }
        }
コード例 #12
0
        public ICustomSerializable GetObject(long id)
        {
            ICustomSerializable obj;
            bool gettingDictionaryItemWasSuccesfull = readObjects.TryGetValue(id, out obj);

            if (gettingDictionaryItemWasSuccesfull)
            {
                return(obj);
            }
            List <string> des = GetObjectStringList(id);

            Type t = Type.GetType("TP." + des[0]);
            ICustomSerializable c = (ICustomSerializable)Activator.CreateInstance(t);

            c.Deserialize(des, this);
            readObjects.Add(id, c);

            return(c);
        }
コード例 #13
0
        /// <summary>
        /// <para>Asserts that an implementation of <see cref="ICustomSerializable"/> throws
        /// <see cref="NotSupportedException"/> on serialization or deserialization.</para>
        /// </summary>
        /// <param name="cs">
        ///     <para>Instace of ICustomSerializable.</para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <para><paramref name="cs"/> is <see langword="null"/>.</para>
        /// </exception>
        public static void AssertCustomSerializableNotSupported(ICustomSerializable cs)
        {
            if (cs == null)
            {
                throw new ArgumentNullException("instance");
            }

            MemoryStream stream = new MemoryStream();

            try
            {
                Serializer.Serialize <ICustomSerializable>(stream, cs);
                Assert.Fail("NotSupportedException expected by ICustomSerializable.Serialize but not thrown.");
            }
            catch (Exception exc)
            {
                NotSupportedException nsexc = exc as NotSupportedException;
                if (nsexc != null)
                {
                }
                else
                {
                    Assert.Fail("Expected exception of type NotSupportedException but received" + exc.ToString());
                }
            }

            try
            {
                Serializer.Deserialize <ICustomSerializable>(stream, cs);
                Assert.Fail("NotSupportedException expected by ICustomSerializable.Deserialize but not thrown.");
            }
            catch (Exception exc)
            {
                NotSupportedException nsexc = exc as NotSupportedException;
                if (nsexc != null)
                {
                    return;
                }

                Assert.Fail("Expected exception of type NotSupportedException but received" + exc.ToString());
            }
        }
コード例 #14
0
        public void Serialize(Contact contact, bool overwrite, string dateFormat)
        {
            _Overwrite = overwrite;
            if (_OutputFormat == OutputFormat.Xml)
            {
                _Serializer = new SerializerToXml();
            }
            else if (_OutputFormat == OutputFormat.Csv)
            {
                _Serializer = new SerializerToCsv();
            }
            else
            {
                throw new ArgumentOutOfRangeException();
            }

            string fileName = GetNewFileName();

            using (var writer = new StreamWriter(fileName, false))
            {
                _Serializer.Serialize(writer, contact, dateFormat);
            }
        }
コード例 #15
0
 public void RegisterCustomData(ICustomSerializable customData)
 {
     throw new NotImplementedException();
 }
コード例 #16
0
 public StatisticsService()
 {
     jsonSerializer = new JsonSerializer();
 }
コード例 #17
0
ファイル: Serializer.cs プロジェクト: Vanmaru/Store
 public static void Save(BinaryWriter writer, ICustomSerializable objToSerialize)
 {
     writer.Write(objToSerialize.GetType().FullName);
     objToSerialize.GetObjectData(writer);
 }
コード例 #18
0
 public void RegisterCustomData(ICustomSerializable customData)
 {
     throw new NotImplementedException();
 }