aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/Serialization/DictionaryWriter.cs
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-06-10 18:01:34 -0500
committerrogerk <devnull@localhost>2011-06-10 18:01:34 -0500
commit4dc0dfb1546998b15a5e98d5dde6154d463365f2 (patch)
tree2dc8961df1e5c67e215c5e119a2989ba04a58511 /src/ProtocolBuffers/Serialization/DictionaryWriter.cs
parentddb74eb6a45e758f6690f237fdf60b98d76e02fb (diff)
downloadprotobuf-4dc0dfb1546998b15a5e98d5dde6154d463365f2.tar.gz
protobuf-4dc0dfb1546998b15a5e98d5dde6154d463365f2.tar.bz2
protobuf-4dc0dfb1546998b15a5e98d5dde6154d463365f2.zip
Added initial DictionaryReader/Writer implementations
Diffstat (limited to 'src/ProtocolBuffers/Serialization/DictionaryWriter.cs')
-rw-r--r--src/ProtocolBuffers/Serialization/DictionaryWriter.cs172
1 files changed, 172 insertions, 0 deletions
diff --git a/src/ProtocolBuffers/Serialization/DictionaryWriter.cs b/src/ProtocolBuffers/Serialization/DictionaryWriter.cs
new file mode 100644
index 00000000..596a9262
--- /dev/null
+++ b/src/ProtocolBuffers/Serialization/DictionaryWriter.cs
@@ -0,0 +1,172 @@
+using System;
+using System.Collections.Generic;
+using Google.ProtocolBuffers.Descriptors;
+
+namespace Google.ProtocolBuffers.Serialization
+{
+ /// <summary>
+ /// Allows writing messages to a name/value dictionary
+ /// </summary>
+ public class DictionaryWriter : AbstractWriter
+ {
+ private readonly IDictionary<string, object> _output;
+
+ /// <summary>
+ /// Constructs a writer using a new dictionary
+ /// </summary>
+ public DictionaryWriter()
+ : this(new Dictionary<string,object>())
+ { }
+
+ /// <summary>
+ /// Constructs a writer using an existing dictionary
+ /// </summary>
+ public DictionaryWriter(IDictionary<string, object> output)
+ {
+ ThrowHelper.ThrowIfNull(output, "output");
+ _output = output;
+ }
+
+ /// <summary>
+ /// Accesses the dictionary that is backing this writer
+ /// </summary>
+ public IDictionary<string, object> ToDictionary() { return _output; }
+
+ /// <summary>
+ /// Writes the message to the the formatted stream.
+ /// </summary>
+ public override void WriteMessage(IMessageLite message)
+ {
+ message.WriteTo(this);
+ }
+
+ /// <summary>
+ /// Writes a Boolean value
+ /// </summary>
+ protected override void Write(string field, bool value)
+ {
+ _output[field] = value;
+ }
+
+ /// <summary>
+ /// Writes a Int32 value
+ /// </summary>
+ protected override void Write(string field, int value)
+ {
+ _output[field] = value;
+ }
+
+ /// <summary>
+ /// Writes a UInt32 value
+ /// </summary>
+ [CLSCompliant(false)]
+ protected override void Write(string field, uint value)
+ {
+ _output[field] = value;
+ }
+
+ /// <summary>
+ /// Writes a Int64 value
+ /// </summary>
+ protected override void Write(string field, long value)
+ {
+ _output[field] = value;
+ }
+
+ /// <summary>
+ /// Writes a UInt64 value
+ /// </summary>
+ [CLSCompliant(false)]
+ protected override void Write(string field, ulong value)
+ {
+ _output[field] = value;
+ }
+
+ /// <summary>
+ /// Writes a Single value
+ /// </summary>
+ protected override void Write(string field, float value)
+ {
+ _output[field] = value;
+ }
+
+ /// <summary>
+ /// Writes a Double value
+ /// </summary>
+ protected override void Write(string field, double value)
+ {
+ _output[field] = value;
+ }
+
+ /// <summary>
+ /// Writes a String value
+ /// </summary>
+ protected override void Write(string field, string value)
+ {
+ _output[field] = value;
+ }
+
+ /// <summary>
+ /// Writes a set of bytes
+ /// </summary>
+ protected override void Write(string field, ByteString value)
+ {
+ _output[field] = value.ToByteArray();
+ }
+
+ /// <summary>
+ /// Writes a message or group as a field
+ /// </summary>
+ protected override void WriteMessageOrGroup(string field, IMessageLite message)
+ {
+ DictionaryWriter writer = new DictionaryWriter();
+ writer.WriteMessage(message);
+
+ _output[field] = writer.ToDictionary();
+ }
+
+ /// <summary>
+ /// Writes a System.Enum by the numeric and textual value
+ /// </summary>
+ protected override void WriteEnum(string field, int number, string name)
+ {
+ _output[field] = number;
+ }
+
+ /// <summary>
+ /// Writes an array of field values
+ /// </summary>
+ protected override void WriteArray(FieldType fieldType, string field, System.Collections.IEnumerable items)
+ {
+ List<object> objects = new List<object>();
+ foreach (object o in items)
+ {
+ switch (fieldType)
+ {
+ case FieldType.Group:
+ case FieldType.Message:
+ {
+ DictionaryWriter writer = new DictionaryWriter();
+ writer.WriteMessage((IMessageLite)o);
+ objects.Add(writer.ToDictionary());
+ }
+ break;
+ case FieldType.Bytes:
+ objects.Add(((ByteString)o).ToByteArray());
+ break;
+ case FieldType.Enum:
+ if (o is IEnumLite)
+ objects.Add(((IEnumLite)o).Number);
+ else
+ objects.Add((int)o);
+ break;
+ default:
+ objects.Add(o);
+ break;
+ }
+ }
+
+ _output[field] = objects.ToArray();
+ }
+ }
+}