public string GetFull(ICustomSerializable <Blog> serializableObject) { var blog = serializableObject as Blog; var serializer = SerializerFactory.GetSerializer <Blog>(); return(serializer.Serialize(blog)); }
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); }
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); } } }
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)); }
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)); }
bool DeserializeCustomSerializable(ref object instance, TypeSerializationArgs args) { if (instance == null) { instance = CreateInstance(); } ICustomSerializable ics = instance as ICustomSerializable; ics.Deserialize(args.Reader); return(true); }
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); } }
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 + ","; } }
/// <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); } }
/// <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); } }
/// <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); } }
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); }
/// <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()); } }
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); } }
public void RegisterCustomData(ICustomSerializable customData) { throw new NotImplementedException(); }
public StatisticsService() { jsonSerializer = new JsonSerializer(); }
public static void Save(BinaryWriter writer, ICustomSerializable objToSerialize) { writer.Write(objToSerialize.GetType().FullName); objToSerialize.GetObjectData(writer); }