blob: 4c3b01167fb4bcfe30a0d8398227ef49cd44d768 [file] [log] [blame]
csharptest4dc0dfb2011-06-10 18:01:34 -05001using System;
csharptest74c5e0c2011-07-14 13:06:22 -05002using System.Collections;
csharptest4dc0dfb2011-06-10 18:01:34 -05003using System.Collections.Generic;
4using Google.ProtocolBuffers.Descriptors;
5
6namespace Google.ProtocolBuffers.Serialization
7{
8 /// <summary>
9 /// Allows writing messages to a name/value dictionary
10 /// </summary>
11 public class DictionaryWriter : AbstractWriter
12 {
13 private readonly IDictionary<string, object> _output;
14
15 /// <summary>
16 /// Constructs a writer using a new dictionary
17 /// </summary>
18 public DictionaryWriter()
csharptest74c5e0c2011-07-14 13:06:22 -050019 : this(new Dictionary<string, object>(StringComparer.Ordinal))
20 {
21 }
csharptest4dc0dfb2011-06-10 18:01:34 -050022
23 /// <summary>
24 /// Constructs a writer using an existing dictionary
25 /// </summary>
26 public DictionaryWriter(IDictionary<string, object> output)
27 {
28 ThrowHelper.ThrowIfNull(output, "output");
29 _output = output;
30 }
31
32 /// <summary>
csharptest3b70dd72011-06-11 12:22:17 -050033 /// Creates the dictionary instance for a child message.
34 /// </summary>
35 protected virtual DictionaryWriter Create()
36 {
37 return new DictionaryWriter();
38 }
39
40 /// <summary>
csharptest4dc0dfb2011-06-10 18:01:34 -050041 /// Accesses the dictionary that is backing this writer
42 /// </summary>
csharptest74c5e0c2011-07-14 13:06:22 -050043 public IDictionary<string, object> ToDictionary()
44 {
45 return _output;
46 }
csharptest4dc0dfb2011-06-10 18:01:34 -050047
csharptest74c5e0c2011-07-14 13:06:22 -050048 /// <summary>
csharptest4dc0dfb2011-06-10 18:01:34 -050049 /// Writes the message to the the formatted stream.
50 /// </summary>
51 public override void WriteMessage(IMessageLite message)
52 {
53 message.WriteTo(this);
54 }
55
csharptestc2d2c1a2011-09-08 20:02:11 -050056
57 /// <summary>
58 /// No-op
59 /// </summary>
60 public override void StartMessage()
61 { }
62
63 /// <summary>
64 /// No-op
65 /// </summary>
66 public override void EndMessage()
67 { }
68
csharptest4dc0dfb2011-06-10 18:01:34 -050069 /// <summary>
70 /// Writes a Boolean value
71 /// </summary>
72 protected override void Write(string field, bool value)
73 {
74 _output[field] = value;
75 }
76
77 /// <summary>
78 /// Writes a Int32 value
79 /// </summary>
80 protected override void Write(string field, int value)
81 {
82 _output[field] = value;
83 }
84
85 /// <summary>
86 /// Writes a UInt32 value
87 /// </summary>
88 [CLSCompliant(false)]
89 protected override void Write(string field, uint value)
90 {
91 _output[field] = value;
92 }
93
94 /// <summary>
95 /// Writes a Int64 value
96 /// </summary>
97 protected override void Write(string field, long value)
98 {
99 _output[field] = value;
100 }
101
102 /// <summary>
103 /// Writes a UInt64 value
104 /// </summary>
105 [CLSCompliant(false)]
106 protected override void Write(string field, ulong value)
107 {
108 _output[field] = value;
109 }
110
111 /// <summary>
112 /// Writes a Single value
113 /// </summary>
114 protected override void Write(string field, float value)
115 {
116 _output[field] = value;
117 }
118
119 /// <summary>
120 /// Writes a Double value
121 /// </summary>
122 protected override void Write(string field, double value)
123 {
124 _output[field] = value;
125 }
126
127 /// <summary>
128 /// Writes a String value
129 /// </summary>
130 protected override void Write(string field, string value)
131 {
132 _output[field] = value;
133 }
134
135 /// <summary>
136 /// Writes a set of bytes
137 /// </summary>
138 protected override void Write(string field, ByteString value)
139 {
140 _output[field] = value.ToByteArray();
141 }
142
143 /// <summary>
144 /// Writes a message or group as a field
145 /// </summary>
146 protected override void WriteMessageOrGroup(string field, IMessageLite message)
147 {
csharptest3b70dd72011-06-11 12:22:17 -0500148 DictionaryWriter writer = Create();
csharptest4dc0dfb2011-06-10 18:01:34 -0500149 writer.WriteMessage(message);
150
151 _output[field] = writer.ToDictionary();
152 }
153
154 /// <summary>
155 /// Writes a System.Enum by the numeric and textual value
156 /// </summary>
157 protected override void WriteEnum(string field, int number, string name)
158 {
159 _output[field] = number;
160 }
161
162 /// <summary>
163 /// Writes an array of field values
164 /// </summary>
csharptest74c5e0c2011-07-14 13:06:22 -0500165 protected override void WriteArray(FieldType fieldType, string field, IEnumerable items)
csharptest4dc0dfb2011-06-10 18:01:34 -0500166 {
167 List<object> objects = new List<object>();
168 foreach (object o in items)
169 {
170 switch (fieldType)
171 {
172 case FieldType.Group:
173 case FieldType.Message:
174 {
csharptest3b70dd72011-06-11 12:22:17 -0500175 DictionaryWriter writer = Create();
csharptest74c5e0c2011-07-14 13:06:22 -0500176 writer.WriteMessage((IMessageLite) o);
csharptest4dc0dfb2011-06-10 18:01:34 -0500177 objects.Add(writer.ToDictionary());
178 }
179 break;
180 case FieldType.Bytes:
csharptest74c5e0c2011-07-14 13:06:22 -0500181 objects.Add(((ByteString) o).ToByteArray());
csharptest4dc0dfb2011-06-10 18:01:34 -0500182 break;
183 case FieldType.Enum:
184 if (o is IEnumLite)
csharptest74c5e0c2011-07-14 13:06:22 -0500185 {
186 objects.Add(((IEnumLite) o).Number);
187 }
csharptest4dc0dfb2011-06-10 18:01:34 -0500188 else
csharptest74c5e0c2011-07-14 13:06:22 -0500189 {
190 objects.Add((int) o);
191 }
csharptest4dc0dfb2011-06-10 18:01:34 -0500192 break;
193 default:
194 objects.Add(o);
195 break;
196 }
197 }
198
199 _output[field] = objects.ToArray();
200 }
201 }
csharptest74c5e0c2011-07-14 13:06:22 -0500202}