blob: 96175a7e57a6d64e729181e090967dad0f385efd [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
56 /// <summary>
57 /// Writes a Boolean value
58 /// </summary>
59 protected override void Write(string field, bool value)
60 {
61 _output[field] = value;
62 }
63
64 /// <summary>
65 /// Writes a Int32 value
66 /// </summary>
67 protected override void Write(string field, int value)
68 {
69 _output[field] = value;
70 }
71
72 /// <summary>
73 /// Writes a UInt32 value
74 /// </summary>
75 [CLSCompliant(false)]
76 protected override void Write(string field, uint value)
77 {
78 _output[field] = value;
79 }
80
81 /// <summary>
82 /// Writes a Int64 value
83 /// </summary>
84 protected override void Write(string field, long value)
85 {
86 _output[field] = value;
87 }
88
89 /// <summary>
90 /// Writes a UInt64 value
91 /// </summary>
92 [CLSCompliant(false)]
93 protected override void Write(string field, ulong value)
94 {
95 _output[field] = value;
96 }
97
98 /// <summary>
99 /// Writes a Single value
100 /// </summary>
101 protected override void Write(string field, float value)
102 {
103 _output[field] = value;
104 }
105
106 /// <summary>
107 /// Writes a Double value
108 /// </summary>
109 protected override void Write(string field, double value)
110 {
111 _output[field] = value;
112 }
113
114 /// <summary>
115 /// Writes a String value
116 /// </summary>
117 protected override void Write(string field, string value)
118 {
119 _output[field] = value;
120 }
121
122 /// <summary>
123 /// Writes a set of bytes
124 /// </summary>
125 protected override void Write(string field, ByteString value)
126 {
127 _output[field] = value.ToByteArray();
128 }
129
130 /// <summary>
131 /// Writes a message or group as a field
132 /// </summary>
133 protected override void WriteMessageOrGroup(string field, IMessageLite message)
134 {
csharptest3b70dd72011-06-11 12:22:17 -0500135 DictionaryWriter writer = Create();
csharptest4dc0dfb2011-06-10 18:01:34 -0500136 writer.WriteMessage(message);
137
138 _output[field] = writer.ToDictionary();
139 }
140
141 /// <summary>
142 /// Writes a System.Enum by the numeric and textual value
143 /// </summary>
144 protected override void WriteEnum(string field, int number, string name)
145 {
146 _output[field] = number;
147 }
148
149 /// <summary>
150 /// Writes an array of field values
151 /// </summary>
csharptest74c5e0c2011-07-14 13:06:22 -0500152 protected override void WriteArray(FieldType fieldType, string field, IEnumerable items)
csharptest4dc0dfb2011-06-10 18:01:34 -0500153 {
154 List<object> objects = new List<object>();
155 foreach (object o in items)
156 {
157 switch (fieldType)
158 {
159 case FieldType.Group:
160 case FieldType.Message:
161 {
csharptest3b70dd72011-06-11 12:22:17 -0500162 DictionaryWriter writer = Create();
csharptest74c5e0c2011-07-14 13:06:22 -0500163 writer.WriteMessage((IMessageLite) o);
csharptest4dc0dfb2011-06-10 18:01:34 -0500164 objects.Add(writer.ToDictionary());
165 }
166 break;
167 case FieldType.Bytes:
csharptest74c5e0c2011-07-14 13:06:22 -0500168 objects.Add(((ByteString) o).ToByteArray());
csharptest4dc0dfb2011-06-10 18:01:34 -0500169 break;
170 case FieldType.Enum:
171 if (o is IEnumLite)
csharptest74c5e0c2011-07-14 13:06:22 -0500172 {
173 objects.Add(((IEnumLite) o).Number);
174 }
csharptest4dc0dfb2011-06-10 18:01:34 -0500175 else
csharptest74c5e0c2011-07-14 13:06:22 -0500176 {
177 objects.Add((int) o);
178 }
csharptest4dc0dfb2011-06-10 18:01:34 -0500179 break;
180 default:
181 objects.Add(o);
182 break;
183 }
184 }
185
186 _output[field] = objects.ToArray();
187 }
188 }
csharptest74c5e0c2011-07-14 13:06:22 -0500189}