/// <summary> /// Serializes a KeyValue object into stream. /// </summary> /// <param name="stream">The stream to serialize into.</param> /// <param name="data">The data to serialize.</param> /// <param name="options">Options to use that can influence the serialization process.</param> public void Serialize(Stream stream, KvObject data, KvSerializerOptions options = null) { using (var serializer = MakeSerializer(stream, options ?? KvSerializerOptions.DefaultOptions)) { var visitor = new KvObjectVisitor(serializer); visitor.Visit(data); } }
/// <summary> /// Deserializes an object from a KeyValues representation in a stream. /// </summary> /// <param name="stream">The stream to deserialize from.</param> /// <param name="options">Options to use that can influence the deserialization process.</param> /// <returns>A <typeparamref name="TObject" /> instance representing the KeyValues structure in the stream.</returns> /// <typeparam name="TObject">The type of object to deserialize.</typeparam>; public TObject Deserialize <TObject>(Stream stream, KvSerializerOptions options = null) { Require.NotNull(stream, nameof(stream)); var @object = Deserialize(stream, options ?? KvSerializerOptions.DefaultOptions); var typedObject = ObjectCopier.MakeObject <TObject>(@object); return(typedObject); }
/// <summary> /// Deserializes a KeyValue object from a stream. /// </summary> /// <param name="stream">The stream to deserialize from.</param> /// <param name="options">Options to use that can influence the deserialization process.</param> /// <returns>A <see cref="KvObject"/> representing the KeyValues structure encoded in the stream.</returns> public KvObject Deserialize(Stream stream, KvSerializerOptions options = null) { Require.NotNull(stream, nameof(stream)); var builder = new KvObjectBuilder(); using (var reader = MakeReader(stream, builder, options ?? KvSerializerOptions.DefaultOptions)) reader.ReadObject(); return(builder.GetObject()); }
/// <summary> /// Serializes a KeyValue object into stream in plain text.. /// </summary> /// <param name="stream">The stream to serialize into.</param> /// <param name="data">The data to serialize.</param> /// <param name="name">The top-level object name</param> /// <param name="options">Options to use that can influence the serialization process.</param> /// <typeparam name="TData">The type of object to serialize.</typeparam> public void Serialize <TData>(Stream stream, TData data, string name, KvSerializerOptions options = null) { Require.NotNull(stream, nameof(stream)); if (data == null) { throw new ArgumentNullException(nameof(data)); } Require.NotNull(name, nameof(name)); var kvObjectTree = ObjectCopier.FromObject(data, name); Serialize(stream, kvObjectTree, options); }
private IVisitationListener MakeSerializer(Stream stream, KvSerializerOptions options) { Require.NotNull(stream, nameof(stream)); Require.NotNull(options, nameof(options)); switch (_format) { case KvSerializationFormat.KeyValues1Text: return(new Kv1TextSerializer(stream, options)); case KvSerializationFormat.KeyValues1Binary: return(new Kv1BinarySerializer(stream, options)); default: throw new ArgumentOutOfRangeException(nameof(_format), _format, "Invalid serialization format."); } }