Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2015 Google Inc. All rights reserved. |
| 4 | // https://developers.google.com/protocol-buffers/ |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without |
| 7 | // modification, are permitted provided that the following conditions are |
| 8 | // met: |
| 9 | // |
| 10 | // * Redistributions of source code must retain the above copyright |
| 11 | // notice, this list of conditions and the following disclaimer. |
| 12 | // * Redistributions in binary form must reproduce the above |
| 13 | // copyright notice, this list of conditions and the following disclaimer |
| 14 | // in the documentation and/or other materials provided with the |
| 15 | // distribution. |
| 16 | // * Neither the name of Google Inc. nor the names of its |
| 17 | // contributors may be used to endorse or promote products derived from |
| 18 | // this software without specific prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | #endregion |
| 32 | |
| 33 | using System; |
| 34 | using System.IO; |
| 35 | |
| 36 | namespace Google.Protobuf |
| 37 | { |
| 38 | /// <summary> |
| 39 | /// A general message parser, typically used by reflection-based code as all the methods |
| 40 | /// return simple <see cref="IMessage"/>. |
| 41 | /// </summary> |
| 42 | public class MessageParser |
| 43 | { |
| 44 | private Func<IMessage> factory; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 45 | // TODO: When we use a C# 7.1 compiler, make this private protected. |
| 46 | internal bool DiscardUnknownFields { get; } |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 47 | |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 48 | internal MessageParser(Func<IMessage> factory, bool discardUnknownFields) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 49 | { |
| 50 | this.factory = factory; |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 51 | DiscardUnknownFields = discardUnknownFields; |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /// <summary> |
| 55 | /// Creates a template instance ready for population. |
| 56 | /// </summary> |
| 57 | /// <returns>An empty message.</returns> |
| 58 | internal IMessage CreateTemplate() |
| 59 | { |
| 60 | return factory(); |
| 61 | } |
| 62 | |
| 63 | /// <summary> |
| 64 | /// Parses a message from a byte array. |
| 65 | /// </summary> |
| 66 | /// <param name="data">The byte array containing the message. Must not be null.</param> |
| 67 | /// <returns>The newly parsed message.</returns> |
| 68 | public IMessage ParseFrom(byte[] data) |
| 69 | { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 70 | IMessage message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 71 | message.MergeFrom(data, DiscardUnknownFields); |
| 72 | return message; |
| 73 | } |
| 74 | |
| 75 | /// <summary> |
| 76 | /// Parses a message from a byte array slice. |
| 77 | /// </summary> |
| 78 | /// <param name="data">The byte array containing the message. Must not be null.</param> |
| 79 | /// <param name="offset">The offset of the slice to parse.</param> |
| 80 | /// <param name="length">The length of the slice to parse.</param> |
| 81 | /// <returns>The newly parsed message.</returns> |
| 82 | public IMessage ParseFrom(byte[] data, int offset, int length) |
| 83 | { |
| 84 | IMessage message = factory(); |
| 85 | message.MergeFrom(data, offset, length, DiscardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 86 | return message; |
| 87 | } |
| 88 | |
| 89 | /// <summary> |
| 90 | /// Parses a message from the given byte string. |
| 91 | /// </summary> |
| 92 | /// <param name="data">The data to parse.</param> |
| 93 | /// <returns>The parsed message.</returns> |
| 94 | public IMessage ParseFrom(ByteString data) |
| 95 | { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 96 | IMessage message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 97 | message.MergeFrom(data, DiscardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 98 | return message; |
| 99 | } |
| 100 | |
| 101 | /// <summary> |
| 102 | /// Parses a message from the given stream. |
| 103 | /// </summary> |
| 104 | /// <param name="input">The stream to parse.</param> |
| 105 | /// <returns>The parsed message.</returns> |
| 106 | public IMessage ParseFrom(Stream input) |
| 107 | { |
| 108 | IMessage message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 109 | message.MergeFrom(input, DiscardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 110 | return message; |
| 111 | } |
| 112 | |
| 113 | /// <summary> |
| 114 | /// Parses a length-delimited message from the given stream. |
| 115 | /// </summary> |
| 116 | /// <remarks> |
| 117 | /// The stream is expected to contain a length and then the data. Only the amount of data |
| 118 | /// specified by the length will be consumed. |
| 119 | /// </remarks> |
| 120 | /// <param name="input">The stream to parse.</param> |
| 121 | /// <returns>The parsed message.</returns> |
| 122 | public IMessage ParseDelimitedFrom(Stream input) |
| 123 | { |
| 124 | IMessage message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 125 | message.MergeDelimitedFrom(input, DiscardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 126 | return message; |
| 127 | } |
| 128 | |
| 129 | /// <summary> |
| 130 | /// Parses a message from the given coded input stream. |
| 131 | /// </summary> |
| 132 | /// <param name="input">The stream to parse.</param> |
| 133 | /// <returns>The parsed message.</returns> |
| 134 | public IMessage ParseFrom(CodedInputStream input) |
| 135 | { |
| 136 | IMessage message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 137 | MergeFrom(message, input); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 138 | return message; |
| 139 | } |
| 140 | |
| 141 | /// <summary> |
| 142 | /// Parses a message from the given JSON. |
| 143 | /// </summary> |
| 144 | /// <param name="json">The JSON to parse.</param> |
| 145 | /// <returns>The parsed message.</returns> |
| 146 | /// <exception cref="InvalidJsonException">The JSON does not comply with RFC 7159</exception> |
| 147 | /// <exception cref="InvalidProtocolBufferException">The JSON does not represent a Protocol Buffers message correctly</exception> |
| 148 | public IMessage ParseJson(string json) |
| 149 | { |
| 150 | IMessage message = factory(); |
| 151 | JsonParser.Default.Merge(message, json); |
| 152 | return message; |
| 153 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 154 | |
| 155 | // TODO: When we're using a C# 7.1 compiler, make this private protected. |
| 156 | internal void MergeFrom(IMessage message, CodedInputStream codedInput) |
| 157 | { |
| 158 | bool originalDiscard = codedInput.DiscardUnknownFields; |
| 159 | try |
| 160 | { |
| 161 | codedInput.DiscardUnknownFields = DiscardUnknownFields; |
| 162 | message.MergeFrom(codedInput); |
| 163 | } |
| 164 | finally |
| 165 | { |
| 166 | codedInput.DiscardUnknownFields = originalDiscard; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /// <summary> |
| 171 | /// Creates a new message parser which optionally discards unknown fields when parsing. |
| 172 | /// </summary> |
| 173 | /// <param name="discardUnknownFields">Whether or not to discard unknown fields when parsing.</param> |
| 174 | /// <returns>A newly configured message parser.</returns> |
| 175 | public MessageParser WithDiscardUnknownFields(bool discardUnknownFields) => |
| 176 | new MessageParser(factory, discardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /// <summary> |
| 180 | /// A parser for a specific message type. |
| 181 | /// </summary> |
| 182 | /// <remarks> |
| 183 | /// <p> |
| 184 | /// This delegates most behavior to the |
| 185 | /// <see cref="IMessage.MergeFrom"/> implementation within the original type, but |
| 186 | /// provides convenient overloads to parse from a variety of sources. |
| 187 | /// </p> |
| 188 | /// <p> |
| 189 | /// Most applications will never need to create their own instances of this type; |
| 190 | /// instead, use the static <c>Parser</c> property of a generated message type to obtain a |
| 191 | /// parser for that type. |
| 192 | /// </p> |
| 193 | /// </remarks> |
| 194 | /// <typeparam name="T">The type of message to be parsed.</typeparam> |
| 195 | public sealed class MessageParser<T> : MessageParser where T : IMessage<T> |
| 196 | { |
| 197 | // Implementation note: all the methods here *could* just delegate up to the base class and cast the result. |
| 198 | // The current implementation avoids a virtual method call and a cast, which *may* be significant in some cases. |
| 199 | // Benchmarking work is required to measure the significance - but it's only a few lines of code in any case. |
| 200 | // The API wouldn't change anyway - just the implementation - so this work can be deferred. |
| 201 | private readonly Func<T> factory; |
| 202 | |
| 203 | /// <summary> |
| 204 | /// Creates a new parser. |
| 205 | /// </summary> |
| 206 | /// <remarks> |
| 207 | /// The factory method is effectively an optimization over using a generic constraint |
| 208 | /// to require a parameterless constructor: delegates are significantly faster to execute. |
| 209 | /// </remarks> |
| 210 | /// <param name="factory">Function to invoke when a new, empty message is required.</param> |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 211 | public MessageParser(Func<T> factory) : this(factory, false) |
| 212 | { |
| 213 | } |
| 214 | |
| 215 | internal MessageParser(Func<T> factory, bool discardUnknownFields) : base(() => factory(), discardUnknownFields) |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 216 | { |
| 217 | this.factory = factory; |
| 218 | } |
| 219 | |
| 220 | /// <summary> |
| 221 | /// Creates a template instance ready for population. |
| 222 | /// </summary> |
| 223 | /// <returns>An empty message.</returns> |
| 224 | internal new T CreateTemplate() |
| 225 | { |
| 226 | return factory(); |
| 227 | } |
| 228 | |
| 229 | /// <summary> |
| 230 | /// Parses a message from a byte array. |
| 231 | /// </summary> |
| 232 | /// <param name="data">The byte array containing the message. Must not be null.</param> |
| 233 | /// <returns>The newly parsed message.</returns> |
| 234 | public new T ParseFrom(byte[] data) |
| 235 | { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 236 | T message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 237 | message.MergeFrom(data, DiscardUnknownFields); |
| 238 | return message; |
| 239 | } |
| 240 | |
| 241 | /// <summary> |
| 242 | /// Parses a message from a byte array slice. |
| 243 | /// </summary> |
| 244 | /// <param name="data">The byte array containing the message. Must not be null.</param> |
| 245 | /// <param name="offset">The offset of the slice to parse.</param> |
| 246 | /// <param name="length">The length of the slice to parse.</param> |
| 247 | /// <returns>The newly parsed message.</returns> |
| 248 | public new T ParseFrom(byte[] data, int offset, int length) |
| 249 | { |
| 250 | T message = factory(); |
| 251 | message.MergeFrom(data, offset, length, DiscardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 252 | return message; |
| 253 | } |
| 254 | |
| 255 | /// <summary> |
| 256 | /// Parses a message from the given byte string. |
| 257 | /// </summary> |
| 258 | /// <param name="data">The data to parse.</param> |
| 259 | /// <returns>The parsed message.</returns> |
| 260 | public new T ParseFrom(ByteString data) |
| 261 | { |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 262 | T message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 263 | message.MergeFrom(data, DiscardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 264 | return message; |
| 265 | } |
| 266 | |
| 267 | /// <summary> |
| 268 | /// Parses a message from the given stream. |
| 269 | /// </summary> |
| 270 | /// <param name="input">The stream to parse.</param> |
| 271 | /// <returns>The parsed message.</returns> |
| 272 | public new T ParseFrom(Stream input) |
| 273 | { |
| 274 | T message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 275 | message.MergeFrom(input, DiscardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 276 | return message; |
| 277 | } |
| 278 | |
| 279 | /// <summary> |
| 280 | /// Parses a length-delimited message from the given stream. |
| 281 | /// </summary> |
| 282 | /// <remarks> |
| 283 | /// The stream is expected to contain a length and then the data. Only the amount of data |
| 284 | /// specified by the length will be consumed. |
| 285 | /// </remarks> |
| 286 | /// <param name="input">The stream to parse.</param> |
| 287 | /// <returns>The parsed message.</returns> |
| 288 | public new T ParseDelimitedFrom(Stream input) |
| 289 | { |
| 290 | T message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 291 | message.MergeDelimitedFrom(input, DiscardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 292 | return message; |
| 293 | } |
| 294 | |
| 295 | /// <summary> |
| 296 | /// Parses a message from the given coded input stream. |
| 297 | /// </summary> |
| 298 | /// <param name="input">The stream to parse.</param> |
| 299 | /// <returns>The parsed message.</returns> |
| 300 | public new T ParseFrom(CodedInputStream input) |
| 301 | { |
| 302 | T message = factory(); |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 303 | MergeFrom(message, input); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 304 | return message; |
| 305 | } |
| 306 | |
| 307 | /// <summary> |
| 308 | /// Parses a message from the given JSON. |
| 309 | /// </summary> |
| 310 | /// <param name="json">The JSON to parse.</param> |
| 311 | /// <returns>The parsed message.</returns> |
| 312 | /// <exception cref="InvalidJsonException">The JSON does not comply with RFC 7159</exception> |
| 313 | /// <exception cref="InvalidProtocolBufferException">The JSON does not represent a Protocol Buffers message correctly</exception> |
| 314 | public new T ParseJson(string json) |
| 315 | { |
| 316 | T message = factory(); |
| 317 | JsonParser.Default.Merge(message, json); |
| 318 | return message; |
| 319 | } |
Austin Schuh | 40c1652 | 2018-10-28 20:27:54 -0700 | [diff] [blame^] | 320 | |
| 321 | /// <summary> |
| 322 | /// Creates a new message parser which optionally discards unknown fields when parsing. |
| 323 | /// </summary> |
| 324 | /// <param name="discardUnknownFields">Whether or not to discard unknown fields when parsing.</param> |
| 325 | /// <returns>A newly configured message parser.</returns> |
| 326 | public new MessageParser<T> WithDiscardUnknownFields(bool discardUnknownFields) => |
| 327 | new MessageParser<T>(factory, discardUnknownFields); |
Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame] | 328 | } |
| 329 | } |