DictionaryWriter.cs 5.72 KB
Newer Older
1
using System;
2
using System.Collections;
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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()
19 20 21
            : this(new Dictionary<string, object>(StringComparer.Ordinal))
        {
        }
22 23 24 25 26 27 28 29 30 31

        /// <summary>
        /// Constructs a writer using an existing dictionary
        /// </summary>
        public DictionaryWriter(IDictionary<string, object> output)
        {
            ThrowHelper.ThrowIfNull(output, "output");
            _output = output;
        }

32 33 34 35 36 37 38 39
        /// <summary>
        /// Creates the dictionary instance for a child message.
        /// </summary>
        protected virtual DictionaryWriter Create()
        {
            return new DictionaryWriter();
        }

40 41 42
        /// <summary>
        /// Accesses the dictionary that is backing this writer
        /// </summary>
43 44 45 46
        public IDictionary<string, object> ToDictionary()
        {
            return _output;
        }
47

48
        /// <summary>
49 50 51 52 53 54 55
        /// Writes the message to the the formatted stream.
        /// </summary>
        public override void WriteMessage(IMessageLite message)
        {
            message.WriteTo(this);
        }

56 57 58 59

        /// <summary>
        /// No-op
        /// </summary>
60
        public override void WriteMessageStart()
61 62 63 64 65
        { }

        /// <summary>
        /// No-op
        /// </summary>
66
        public override void WriteMessageEnd()
67 68
        { }

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        /// <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>
        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>
        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)
        {
146
            DictionaryWriter writer = Create();
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            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>
163
        protected override void WriteArray(FieldType fieldType, string field, IEnumerable items)
164 165 166 167 168 169 170 171 172
        {
            List<object> objects = new List<object>();
            foreach (object o in items)
            {
                switch (fieldType)
                {
                    case FieldType.Group:
                    case FieldType.Message:
                        {
173
                            DictionaryWriter writer = Create();
174
                            writer.WriteMessage((IMessageLite) o);
175 176 177 178
                            objects.Add(writer.ToDictionary());
                        }
                        break;
                    case FieldType.Bytes:
179
                        objects.Add(((ByteString) o).ToByteArray());
180 181 182
                        break;
                    case FieldType.Enum:
                        if (o is IEnumLite)
183 184 185
                        {
                            objects.Add(((IEnumLite) o).Number);
                        }
186
                        else
187 188 189
                        {
                            objects.Add((int) o);
                        }
190 191 192 193 194 195 196 197 198 199
                        break;
                    default:
                        objects.Add(o);
                        break;
                }
            }

            _output[field] = objects.ToArray();
        }
    }
200
}