Exemplo n.º 1
0
 /// <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="options">Options to use that can influence the serialization process.</param>
 public static void Serialize(Stream stream, KVObject data, KVSerializerOptions options = null)
 {
     using (var writer = new KVTextWriter(stream, options ?? KVSerializerOptions.DefaultOptions))
     {
         writer.WriteObject(data);
     }
 }
Exemplo n.º 2
0
 /// <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);
     }
 }
Exemplo n.º 3
0
        public KVTokenReader(TextReader textReader, KVSerializerOptions options)
        {
            Require.NotNull(textReader, nameof(textReader));
            Require.NotNull(options, nameof(options));

            this.textReader = textReader;
            this.options    = options;
        }
Exemplo n.º 4
0
        /// <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);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Deserializes an object from a textual KeyValues representation.
        /// </summary>
        /// <param name="text">The text to deserialize.</param>
        /// <param name="options">Options to use that can influence the deserialization process.</param>
        /// <returns>A <see cref="KVObject"/> representing the KeyValues structure in the stream.</returns>
        /// <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 static TObject Deserialize <TObject>(string text, KVSerializerOptions options = null)
        {
            Require.NotNull(text, nameof(text));

            var @object     = Deserialize(text, options ?? KVSerializerOptions.DefaultOptions);
            var typedObject = ObjectCopier.MakeObject <TObject>(@object);

            return(typedObject);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Deserializes an object from a textual KeyValues representation.
        /// </summary>
        /// <param name="text">The text to deserialize.</param>
        /// <param name="options">Options to use that can influence the deserialization process.</param>
        /// <returns>A <see cref="KVObject"/> representing the KeyValues structure in the stream.</returns>
        public static KVObject Deserialize(string text, KVSerializerOptions options = null)
        {
            Require.NotNull(text, nameof(text));

            using (var reader = new KVTextReader(new StringReader(text), options ?? KVSerializerOptions.DefaultOptions))
            {
                return(reader.ReadObject());
            }
        }
Exemplo n.º 7
0
        public KVTextWriter(Stream stream, KVSerializerOptions options)
        {
            Require.NotNull(stream, nameof(stream));
            Require.NotNull(options, nameof(options));

            this.options = options;
            writer = new StreamWriter(stream, Encoding.UTF8, bufferSize: 1024, leaveOpen: true);
            writer.NewLine = "\n";
        }
Exemplo n.º 8
0
        public KVTextWriter(Stream stream, KVSerializerOptions options)
        {
            Require.NotNull(stream, nameof(stream));
            Require.NotNull(options, nameof(options));

            this.options   = options;
            writer         = new StreamWriter(stream, Encoding.UTF8, bufferSize: 1024, leaveOpen: true);
            writer.NewLine = "\n";
        }
Exemplo n.º 9
0
        public KVTextReader(TextReader textReader, KVSerializerOptions options)
        {
            Require.NotNull(textReader, nameof(textReader));
            Require.NotNull(options, nameof(options));

            this.options       = options;
            conditionEvaluator = new KVConditionEvaluator(options.Conditions);
            tokenReader        = new KVTokenReader(textReader, options);
            stateMachine       = new KVTextReaderStateMachine();
        }
Exemplo n.º 10
0
        /// <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());
        }
Exemplo n.º 11
0
        /// <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(typeof(TData), data, name);

            Serialize(stream, kvObjectTree, options);
        }
Exemplo n.º 12
0
        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));

            default:
                throw new ArgumentOutOfRangeException(nameof(format), format, "Invalid serialization format.");
            }
        }
Exemplo n.º 13
0
        IVisitingReader MakeReader(Stream stream, IParsingVisitationListener listener, KVSerializerOptions options)
        {
            Require.NotNull(stream, nameof(stream));
            Require.NotNull(listener, nameof(listener));
            Require.NotNull(options, nameof(options));

            switch (format)
            {
            case KVSerializationFormat.KeyValues1Text:
                return(new KV1TextReader(new StreamReader(stream), listener, options));

            case KVSerializationFormat.KeyValues1Binary:
                return(new KV1BinaryReader(stream, listener));

            case KVSerializationFormat.KeyValues2Binary:
                return(new KV2BinaryReader(stream, listener));

            default:
                throw new ArgumentOutOfRangeException(nameof(format), format, "Invalid serialization format.");
            }
        }