aboutsummaryrefslogtreecommitdiffhomepage
path: root/csharp/src/ProtocolBuffers.Serialization/DictionaryWriter.cs
blob: 8cc8ed6b87afca2f54cec13395c0d77c43749cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections;
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>(StringComparer.Ordinal))
        {
        }

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

        /// <summary>
        /// Creates the dictionary instance for a child message.
        /// </summary>
        protected virtual DictionaryWriter Create()
        {
            return new DictionaryWriter();
        }

        /// <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>
        /// No-op
        /// </summary>
        public override void WriteMessageStart()
        { }

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

        /// <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)
        {
            DictionaryWriter writer = Create();
            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, IEnumerable items)
        {
            List<object> objects = new List<object>();
            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();
        }
    }
}