using System; using System.Collections; using System.Collections.Generic; using Google.ProtocolBuffers.Descriptors; namespace Google.ProtocolBuffers.Serialization { /// /// Allows writing messages to a name/value dictionary /// public class DictionaryWriter : AbstractWriter { private readonly IDictionary _output; /// /// Constructs a writer using a new dictionary /// public DictionaryWriter() : this(new Dictionary(StringComparer.Ordinal)) { } /// /// Constructs a writer using an existing dictionary /// public DictionaryWriter(IDictionary output) { ThrowHelper.ThrowIfNull(output, "output"); _output = output; } /// /// Creates the dictionary instance for a child message. /// protected virtual DictionaryWriter Create() { return new DictionaryWriter(); } /// /// Accesses the dictionary that is backing this writer /// public IDictionary ToDictionary() { return _output; } /// /// Writes the message to the the formatted stream. /// public override void WriteMessage(IMessageLite message) { message.WriteTo(this); } /// /// No-op /// public override void WriteMessageStart() { } /// /// No-op /// public override void WriteMessageEnd() { } /// /// Writes a Boolean value /// protected override void Write(string field, bool value) { _output[field] = value; } /// /// Writes a Int32 value /// protected override void Write(string field, int value) { _output[field] = value; } /// /// Writes a UInt32 value /// protected override void Write(string field, uint value) { _output[field] = value; } /// /// Writes a Int64 value /// protected override void Write(string field, long value) { _output[field] = value; } /// /// Writes a UInt64 value /// protected override void Write(string field, ulong value) { _output[field] = value; } /// /// Writes a Single value /// protected override void Write(string field, float value) { _output[field] = value; } /// /// Writes a Double value /// protected override void Write(string field, double value) { _output[field] = value; } /// /// Writes a String value /// protected override void Write(string field, string value) { _output[field] = value; } /// /// Writes a set of bytes /// protected override void Write(string field, ByteString value) { _output[field] = value.ToByteArray(); } /// /// Writes a message or group as a field /// protected override void WriteMessageOrGroup(string field, IMessageLite message) { DictionaryWriter writer = Create(); writer.WriteMessage(message); _output[field] = writer.ToDictionary(); } /// /// Writes a System.Enum by the numeric and textual value /// protected override void WriteEnum(string field, int number, string name) { _output[field] = number; } /// /// Writes an array of field values /// protected override void WriteArray(FieldType fieldType, string field, IEnumerable items) { List objects = new List(); foreach (object o in items) { switch (fieldType) { case FieldType.Group: case FieldType.Message: { DictionaryWriter writer = Create(); 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(); } } }