csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 1 | using System;
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 2 | using System.Collections;
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 3 | using System.Collections.Generic;
|
| 4 | using Google.ProtocolBuffers.Descriptors;
|
| 5 |
|
| 6 | namespace 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()
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 19 | : this(new Dictionary<string, object>(StringComparer.Ordinal))
|
| 20 | {
|
| 21 | }
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 22 |
|
| 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>
|
csharptest | 3b70dd7 | 2011-06-11 12:22:17 -0500 | [diff] [blame] | 33 | /// Creates the dictionary instance for a child message.
|
| 34 | /// </summary>
|
| 35 | protected virtual DictionaryWriter Create()
|
| 36 | {
|
| 37 | return new DictionaryWriter();
|
| 38 | }
|
| 39 |
|
| 40 | /// <summary>
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 41 | /// Accesses the dictionary that is backing this writer
|
| 42 | /// </summary>
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 43 | public IDictionary<string, object> ToDictionary()
|
| 44 | {
|
| 45 | return _output;
|
| 46 | }
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 47 |
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 48 | /// <summary>
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 49 | /// Writes the message to the the formatted stream.
|
| 50 | /// </summary>
|
| 51 | public override void WriteMessage(IMessageLite message)
|
| 52 | {
|
| 53 | message.WriteTo(this);
|
| 54 | }
|
| 55 |
|
csharptest | c2d2c1a | 2011-09-08 20:02:11 -0500 | [diff] [blame^] | 56 |
|
| 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 |
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 69 | /// <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 | {
|
csharptest | 3b70dd7 | 2011-06-11 12:22:17 -0500 | [diff] [blame] | 148 | DictionaryWriter writer = Create();
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 149 | 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>
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 165 | protected override void WriteArray(FieldType fieldType, string field, IEnumerable items)
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 166 | {
|
| 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 | {
|
csharptest | 3b70dd7 | 2011-06-11 12:22:17 -0500 | [diff] [blame] | 175 | DictionaryWriter writer = Create();
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 176 | writer.WriteMessage((IMessageLite) o);
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 177 | objects.Add(writer.ToDictionary());
|
| 178 | }
|
| 179 | break;
|
| 180 | case FieldType.Bytes:
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 181 | objects.Add(((ByteString) o).ToByteArray());
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 182 | break;
|
| 183 | case FieldType.Enum:
|
| 184 | if (o is IEnumLite)
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 185 | {
|
| 186 | objects.Add(((IEnumLite) o).Number);
|
| 187 | }
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 188 | else
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 189 | {
|
| 190 | objects.Add((int) o);
|
| 191 | }
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 192 | break;
|
| 193 | default:
|
| 194 | objects.Add(o);
|
| 195 | break;
|
| 196 | }
|
| 197 | }
|
| 198 |
|
| 199 | _output[field] = objects.ToArray();
|
| 200 | }
|
| 201 | }
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame] | 202 | } |