blob: 596a926291fe2a975936adfcf47e96859bf6d04b [file] [log] [blame]
csharptest4dc0dfb2011-06-10 18:01:34 -05001using System;
2using System.Collections.Generic;
3using Google.ProtocolBuffers.Descriptors;
4
5namespace Google.ProtocolBuffers.Serialization
6{
7 /// <summary>
8 /// Allows writing messages to a name/value dictionary
9 /// </summary>
10 public class DictionaryWriter : AbstractWriter
11 {
12 private readonly IDictionary<string, object> _output;
13
14 /// <summary>
15 /// Constructs a writer using a new dictionary
16 /// </summary>
17 public DictionaryWriter()
18 : this(new Dictionary<string,object>())
19 { }
20
21 /// <summary>
22 /// Constructs a writer using an existing dictionary
23 /// </summary>
24 public DictionaryWriter(IDictionary<string, object> output)
25 {
26 ThrowHelper.ThrowIfNull(output, "output");
27 _output = output;
28 }
29
30 /// <summary>
31 /// Accesses the dictionary that is backing this writer
32 /// </summary>
33 public IDictionary<string, object> ToDictionary() { return _output; }
34
35 /// <summary>
36 /// Writes the message to the the formatted stream.
37 /// </summary>
38 public override void WriteMessage(IMessageLite message)
39 {
40 message.WriteTo(this);
41 }
42
43 /// <summary>
44 /// Writes a Boolean value
45 /// </summary>
46 protected override void Write(string field, bool value)
47 {
48 _output[field] = value;
49 }
50
51 /// <summary>
52 /// Writes a Int32 value
53 /// </summary>
54 protected override void Write(string field, int value)
55 {
56 _output[field] = value;
57 }
58
59 /// <summary>
60 /// Writes a UInt32 value
61 /// </summary>
62 [CLSCompliant(false)]
63 protected override void Write(string field, uint value)
64 {
65 _output[field] = value;
66 }
67
68 /// <summary>
69 /// Writes a Int64 value
70 /// </summary>
71 protected override void Write(string field, long value)
72 {
73 _output[field] = value;
74 }
75
76 /// <summary>
77 /// Writes a UInt64 value
78 /// </summary>
79 [CLSCompliant(false)]
80 protected override void Write(string field, ulong value)
81 {
82 _output[field] = value;
83 }
84
85 /// <summary>
86 /// Writes a Single value
87 /// </summary>
88 protected override void Write(string field, float value)
89 {
90 _output[field] = value;
91 }
92
93 /// <summary>
94 /// Writes a Double value
95 /// </summary>
96 protected override void Write(string field, double value)
97 {
98 _output[field] = value;
99 }
100
101 /// <summary>
102 /// Writes a String value
103 /// </summary>
104 protected override void Write(string field, string value)
105 {
106 _output[field] = value;
107 }
108
109 /// <summary>
110 /// Writes a set of bytes
111 /// </summary>
112 protected override void Write(string field, ByteString value)
113 {
114 _output[field] = value.ToByteArray();
115 }
116
117 /// <summary>
118 /// Writes a message or group as a field
119 /// </summary>
120 protected override void WriteMessageOrGroup(string field, IMessageLite message)
121 {
122 DictionaryWriter writer = new DictionaryWriter();
123 writer.WriteMessage(message);
124
125 _output[field] = writer.ToDictionary();
126 }
127
128 /// <summary>
129 /// Writes a System.Enum by the numeric and textual value
130 /// </summary>
131 protected override void WriteEnum(string field, int number, string name)
132 {
133 _output[field] = number;
134 }
135
136 /// <summary>
137 /// Writes an array of field values
138 /// </summary>
139 protected override void WriteArray(FieldType fieldType, string field, System.Collections.IEnumerable items)
140 {
141 List<object> objects = new List<object>();
142 foreach (object o in items)
143 {
144 switch (fieldType)
145 {
146 case FieldType.Group:
147 case FieldType.Message:
148 {
149 DictionaryWriter writer = new DictionaryWriter();
150 writer.WriteMessage((IMessageLite)o);
151 objects.Add(writer.ToDictionary());
152 }
153 break;
154 case FieldType.Bytes:
155 objects.Add(((ByteString)o).ToByteArray());
156 break;
157 case FieldType.Enum:
158 if (o is IEnumLite)
159 objects.Add(((IEnumLite)o).Number);
160 else
161 objects.Add((int)o);
162 break;
163 default:
164 objects.Add(o);
165 break;
166 }
167 }
168
169 _output[field] = objects.ToArray();
170 }
171 }
172}