csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 1 | #region Copyright notice and license
|
| 2 |
|
| 3 | // Protocol Buffers - Google's data interchange format
|
| 4 | // Copyright 2008 Google Inc. All rights reserved.
|
| 5 | // http://github.com/jskeet/dotnet-protobufs/
|
| 6 | // Original C++/Java/Python code:
|
| 7 | // http://code.google.com/p/protobuf/
|
| 8 | //
|
| 9 | // Redistribution and use in source and binary forms, with or without
|
| 10 | // modification, are permitted provided that the following conditions are
|
| 11 | // met:
|
| 12 | //
|
| 13 | // * Redistributions of source code must retain the above copyright
|
| 14 | // notice, this list of conditions and the following disclaimer.
|
| 15 | // * Redistributions in binary form must reproduce the above
|
| 16 | // copyright notice, this list of conditions and the following disclaimer
|
| 17 | // in the documentation and/or other materials provided with the
|
| 18 | // distribution.
|
| 19 | // * Neither the name of Google Inc. nor the names of its
|
| 20 | // contributors may be used to endorse or promote products derived from
|
| 21 | // this software without specific prior written permission.
|
| 22 | //
|
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 34 |
|
| 35 | #endregion
|
| 36 |
|
| 37 | using System;
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 38 | using System.Collections;
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 39 | using System.Collections.Generic;
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 40 | using System.IO;
|
| 41 | using System.Xml;
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 42 | using Google.ProtocolBuffers.Collections;
|
| 43 | using Google.ProtocolBuffers.Descriptors;
|
| 44 | using Google.ProtocolBuffers.FieldAccess;
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 45 | using Google.ProtocolBuffers.Serialization;
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 46 |
|
| 47 | namespace Google.ProtocolBuffers
|
| 48 | {
|
| 49 | /// <summary>
|
| 50 | /// All generated protocol message classes extend this class. It implements
|
| 51 | /// most of the IMessage interface using reflection. Users
|
| 52 | /// can ignore this class as an implementation detail.
|
| 53 | /// </summary>
|
| 54 | public abstract class GeneratedMessage<TMessage, TBuilder> : AbstractMessage<TMessage, TBuilder>
|
| 55 | where TMessage : GeneratedMessage<TMessage, TBuilder>
|
csharptest | f292523 | 2011-06-11 10:41:57 -0500 | [diff] [blame] | 56 | where TBuilder : GeneratedBuilder<TMessage, TBuilder>, new()
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 57 | {
|
| 58 | private UnknownFieldSet unknownFields = UnknownFieldSet.DefaultInstance;
|
| 59 |
|
| 60 | /// <summary>
|
| 61 | /// Returns the message as a TMessage.
|
| 62 | /// </summary>
|
| 63 | protected abstract TMessage ThisMessage { get; }
|
| 64 |
|
| 65 | internal FieldAccessorTable<TMessage, TBuilder> FieldAccessorsFromBuilder
|
| 66 | {
|
| 67 | get { return InternalFieldAccessors; }
|
| 68 | }
|
| 69 |
|
| 70 | protected abstract FieldAccessorTable<TMessage, TBuilder> InternalFieldAccessors { get; }
|
| 71 |
|
| 72 | public override MessageDescriptor DescriptorForType
|
| 73 | {
|
| 74 | get { return InternalFieldAccessors.Descriptor; }
|
| 75 | }
|
| 76 |
|
| 77 | internal IDictionary<FieldDescriptor, Object> GetMutableFieldMap()
|
| 78 | {
|
| 79 | // Use a SortedList so we'll end up serializing fields in order
|
| 80 | var ret = new SortedList<FieldDescriptor, object>();
|
| 81 | MessageDescriptor descriptor = DescriptorForType;
|
| 82 | foreach (FieldDescriptor field in descriptor.Fields)
|
| 83 | {
|
| 84 | IFieldAccessor<TMessage, TBuilder> accessor = InternalFieldAccessors[field];
|
| 85 | if (field.IsRepeated)
|
| 86 | {
|
| 87 | if (accessor.GetRepeatedCount(ThisMessage) != 0)
|
| 88 | {
|
| 89 | ret[field] = accessor.GetValue(ThisMessage);
|
| 90 | }
|
| 91 | }
|
| 92 | else if (HasField(field))
|
| 93 | {
|
| 94 | ret[field] = accessor.GetValue(ThisMessage);
|
| 95 | }
|
| 96 | }
|
| 97 | return ret;
|
| 98 | }
|
| 99 |
|
| 100 | public override bool IsInitialized
|
| 101 | {
|
| 102 | get
|
| 103 | {
|
| 104 | foreach (FieldDescriptor field in DescriptorForType.Fields)
|
| 105 | {
|
| 106 | // Check that all required fields are present.
|
| 107 | if (field.IsRequired && !HasField(field))
|
| 108 | {
|
| 109 | return false;
|
| 110 | }
|
| 111 | // Check that embedded messages are initialized.
|
| 112 | // This code is similar to that in AbstractMessage, but we don't
|
| 113 | // fetch all the field values - just the ones we need to.
|
| 114 | if (field.MappedType == MappedType.Message)
|
| 115 | {
|
| 116 | if (field.IsRepeated)
|
| 117 | {
|
| 118 | // We know it's an IList<T>, but not the exact type - so
|
| 119 | // IEnumerable is the best we can do. (C# generics aren't covariant yet.)
|
| 120 | foreach (IMessageLite element in (IEnumerable) this[field])
|
| 121 | {
|
| 122 | if (!element.IsInitialized)
|
| 123 | {
|
| 124 | return false;
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 | else
|
| 129 | {
|
| 130 | if (HasField(field) && !((IMessageLite) this[field]).IsInitialized)
|
| 131 | {
|
| 132 | return false;
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 | return true;
|
| 138 | }
|
| 139 | }
|
| 140 |
|
| 141 | public override IDictionary<FieldDescriptor, object> AllFields
|
| 142 | {
|
| 143 | get { return Dictionaries.AsReadOnly(GetMutableFieldMap()); }
|
| 144 | }
|
| 145 |
|
| 146 | public override bool HasField(FieldDescriptor field)
|
| 147 | {
|
| 148 | return InternalFieldAccessors[field].Has(ThisMessage);
|
| 149 | }
|
| 150 |
|
| 151 | public override int GetRepeatedFieldCount(FieldDescriptor field)
|
| 152 | {
|
| 153 | return InternalFieldAccessors[field].GetRepeatedCount(ThisMessage);
|
| 154 | }
|
| 155 |
|
| 156 | public override object this[FieldDescriptor field, int index]
|
| 157 | {
|
| 158 | get { return InternalFieldAccessors[field].GetRepeatedValue(ThisMessage, index); }
|
| 159 | }
|
| 160 |
|
| 161 | public override object this[FieldDescriptor field]
|
| 162 | {
|
| 163 | get { return InternalFieldAccessors[field].GetValue(ThisMessage); }
|
| 164 | }
|
| 165 |
|
| 166 | public override UnknownFieldSet UnknownFields
|
| 167 | {
|
| 168 | get { return unknownFields; }
|
| 169 | }
|
| 170 |
|
| 171 | /// <summary>
|
| 172 | /// Replaces the set of unknown fields for this message. This should
|
| 173 | /// only be used before a message is built, by the builder. (In the
|
| 174 | /// Java code it is private, but the builder is nested so has access
|
| 175 | /// to it.)
|
| 176 | /// </summary>
|
| 177 | internal void SetUnknownFields(UnknownFieldSet fieldSet)
|
| 178 | {
|
| 179 | unknownFields = fieldSet;
|
| 180 | }
|
csharptest | f292523 | 2011-06-11 10:41:57 -0500 | [diff] [blame] | 181 |
|
| 182 | public static TMessage ParseFromJson(string jsonText)
|
| 183 | {
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 184 | return JsonFormatReader.CreateInstance(jsonText)
|
csharptest | f292523 | 2011-06-11 10:41:57 -0500 | [diff] [blame] | 185 | .Merge(new TBuilder())
|
| 186 | .Build();
|
| 187 | }
|
| 188 |
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 189 | public static TMessage ParseFromJson(TextReader reader)
|
csharptest | f292523 | 2011-06-11 10:41:57 -0500 | [diff] [blame] | 190 | {
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 191 | return ParseFromJson(reader, ExtensionRegistry.Empty);
|
| 192 | }
|
| 193 |
|
| 194 | public static TMessage ParseFromJson(TextReader reader, ExtensionRegistry extensionRegistry)
|
| 195 | {
|
| 196 | return JsonFormatReader.CreateInstance(reader)
|
csharptest | f292523 | 2011-06-11 10:41:57 -0500 | [diff] [blame] | 197 | .Merge(new TBuilder(), extensionRegistry)
|
| 198 | .Build();
|
| 199 | }
|
| 200 |
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 201 | public static TMessage ParseFromXml(XmlReader reader)
|
csharptest | f292523 | 2011-06-11 10:41:57 -0500 | [diff] [blame] | 202 | {
|
csharptest | 74c5e0c | 2011-07-14 13:06:22 -0500 | [diff] [blame^] | 203 | return ParseFromXml(XmlFormatReader.DefaultRootElementName, reader, ExtensionRegistry.Empty);
|
| 204 | }
|
| 205 |
|
| 206 | public static TMessage ParseFromXml(string rootElementName, XmlReader reader)
|
| 207 | {
|
| 208 | return ParseFromXml(rootElementName, reader, ExtensionRegistry.Empty);
|
| 209 | }
|
| 210 |
|
| 211 | public static TMessage ParseFromXml(string rootElementName, XmlReader reader,
|
| 212 | ExtensionRegistry extensionRegistry)
|
| 213 | {
|
| 214 | return XmlFormatReader.CreateInstance(reader)
|
csharptest | f292523 | 2011-06-11 10:41:57 -0500 | [diff] [blame] | 215 | .Merge(rootElementName, new TBuilder(), extensionRegistry)
|
| 216 | .Build();
|
| 217 | }
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 218 | }
|
| 219 | } |