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 |
|
| 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 | {
|
csharptest | 3b70dd7 | 2011-06-11 12:22:17 -0500 | [diff] [blame] | 135 | DictionaryWriter writer = Create();
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 136 | 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>
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 152 | protected override void WriteArray(FieldType fieldType, string field, IEnumerable items)
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 153 | {
|
| 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 | {
|
csharptest | 3b70dd7 | 2011-06-11 12:22:17 -0500 | [diff] [blame] | 162 | DictionaryWriter writer = Create();
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 163 | writer.WriteMessage((IMessageLite) o);
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 164 | objects.Add(writer.ToDictionary());
|
| 165 | }
|
| 166 | break;
|
| 167 | case FieldType.Bytes:
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 168 | objects.Add(((ByteString) o).ToByteArray());
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 169 | break;
|
| 170 | case FieldType.Enum:
|
| 171 | if (o is IEnumLite)
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 172 | {
|
| 173 | objects.Add(((IEnumLite) o).Number);
|
| 174 | }
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 175 | else
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 176 | {
|
| 177 | objects.Add((int) o);
|
| 178 | }
|
csharptest | 4dc0dfb | 2011-06-10 18:01:34 -0500 | [diff] [blame] | 179 | break;
|
| 180 | default:
|
| 181 | objects.Add(o);
|
| 182 | break;
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | _output[field] = objects.ToArray();
|
| 187 | }
|
| 188 | }
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 189 | } |