Helper methods for throwing exceptions when preconditions are not met.
This class is used internally and by generated code; it is not particularly expected to be used from application code, although nothing prevents it from being used that way.
Beispiel #1
0
 /// <summary>
 /// Creates a new <see cref="Settings"/> object based on this one, but with the specified type registry.
 /// </summary>
 /// <param name="typeRegistry">The new type registry. Must not be null.</param>
 public Settings WithTypeRegistry(TypeRegistry typeRegistry) =>
 new Settings(
     RecursionLimit,
     ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry)),
     IgnoreUnknownFields);
Beispiel #2
0
 private Settings(int recursionLimit, TypeRegistry typeRegistry, bool ignoreUnknownFields)
 {
     RecursionLimit      = recursionLimit;
     TypeRegistry        = ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry));
     IgnoreUnknownFields = ignoreUnknownFields;
 }
Beispiel #3
0
 /// <summary>
 /// Creates a new formatted with the given settings.
 /// </summary>
 /// <param name="settings">The settings.</param>
 public JsonParser(Settings settings)
 {
     this.settings = ProtoPreconditions.CheckNotNull(settings, nameof(settings));
 }
Beispiel #4
0
 /// <summary>
 /// Creates a new <see cref="Settings"/> object with the specified recursion limit and type registry.
 /// </summary>
 /// <param name="recursionLimit">The maximum depth of messages to parse</param>
 /// <param name="typeRegistry">The type registry used to parse <see cref="Any"/> messages</param>
 public Settings(int recursionLimit, TypeRegistry typeRegistry)
 {
     RecursionLimit = recursionLimit;
     TypeRegistry   = ProtoPreconditions.CheckNotNull(typeRegistry, "typeRegistry");
 }
 /// <summary>
 /// Converts the given message into a byte string in protobuf encoding.
 /// </summary>
 /// <param name="message">The message to convert.</param>
 /// <returns>The message data as a byte string.</returns>
 public static ByteString ToByteString(this IMessage message)
 {
     ProtoPreconditions.CheckNotNull(message, "message");
     return(ByteString.AttachBytes(message.ToByteArray()));
 }
Beispiel #6
0
 /// <summary>
 /// Constructs a <see cref="ByteString"/> from data in the given stream, asynchronously.
 /// </summary>
 /// <remarks>If successful, <paramref name="stream"/> will be read completely, from the position
 /// at the start of the call.</remarks>
 /// <param name="stream">The stream to copy into a ByteString.</param>
 /// <param name="cancellationToken">The cancellation token to use when reading from the stream, if any.</param>
 /// <returns>A ByteString with content read from the given stream.</returns>
 public static Task <ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default)
 {
     ProtoPreconditions.CheckNotNull(stream, nameof(stream));
     return(ByteStringAsync.FromStreamAsyncCore(stream, cancellationToken));
 }
Beispiel #7
0
 public void Set(byte[] buffer)
 {
     Set(null, ProtoPreconditions.CheckNotNull(buffer, "buffer"), 0, buffer.Length);
 }
Beispiel #8
0
 /// <summary>
 /// Converts a message to JSON for diagnostic purposes with no extra context.
 /// </summary>
 /// <remarks>
 /// <para>
 /// This differs from calling <see cref="Format(IMessage)"/> on the default JSON
 /// formatter in its handling of <see cref="Any"/>. As no type registry is available
 /// in <see cref="object.ToString"/> calls, the normal way of resolving the type of
 /// an <c>Any</c> message cannot be applied. Instead, a JSON property named <c>@value</c>
 /// is included with the base64 data from the <see cref="Any.Value"/> property of the message.
 /// </para>
 /// <para>The value returned by this method is only designed to be used for diagnostic
 /// purposes. It may not be parsable by <see cref="JsonParser"/>, and may not be parsable
 /// by other Protocol Buffer implementations.</para>
 /// </remarks>
 /// <param name="message">The message to format for diagnostic purposes.</param>
 /// <returns>The diagnostic-only JSON representation of the message</returns>
 public static string ToDiagnosticString(IMessage message)
 {
     ProtoPreconditions.CheckNotNull(message, nameof(message));
     return(diagnosticFormatter.Format(message));
 }
Beispiel #9
0
        // Note that the checks are performed such that we don't end up checking obviously-valid things
        // like non-null references for arrays we've just created.

        /// <summary>
        /// Creates a new CodedInputStream reading data from the given byte array.
        /// </summary>
        public CodedInputStream(byte[] buffer) : this(null, ProtoPreconditions.CheckNotNull(buffer, "buffer"), 0, buffer.Length)
        {
        }
Beispiel #10
0
        /// <summary>
        /// Removes the specified extension from the set
        /// </summary>
        /// <param name="item">The extension</param>
        /// <returns><c>true</c> if the extension was removed, otherwise <c>false</c></returns>
        public bool Remove(Extension item)
        {
            ProtoPreconditions.CheckNotNull(item, nameof(item));

            return(extensions.Remove(new ObjectIntPair <Type>(item.TargetType, item.FieldNumber)));
        }
Beispiel #11
0
        /// <summary>
        /// Adds the specified extension to the registry
        /// </summary>
        public void Add(Extension extension)
        {
            ProtoPreconditions.CheckNotNull(extension, nameof(extension));

            extensions.Add(new ObjectIntPair <Type>(extension.TargetType, extension.FieldNumber), extension);
        }
 /// <summary>
 /// Creates a new <see cref="Settings"/> object based on this one, but with the specified type registry.
 /// </summary>
 /// <param name="typeRegistry">The new type registry. Must not be null.</param>
 public Settings WithTypeRegistry(TypeRegistry typeRegistry)
 {
     return(new Settings(RecursionLimit, ProtoPreconditions.CheckNotNull(typeRegistry, "typeRegistry"), IgnoreUnknownFields));
 }
 /// <summary>
 /// Creates a new <see cref="Settings"/> object with the specified formatting of default values
 /// and type registry.
 /// </summary>
 /// <param name="formatDefaultValues"><c>true</c> if default values (0, empty strings etc) should be formatted; <c>false</c> otherwise.</param>
 /// <param name="typeRegistry">The <see cref="TypeRegistry"/> to use when formatting <see cref="Any"/> messages.</param>
 public Settings(bool formatDefaultValues, TypeRegistry typeRegistry)
 {
     FormatDefaultValues = formatDefaultValues;
     TypeRegistry        = ProtoPreconditions.CheckNotNull(typeRegistry, nameof(typeRegistry));
 }
Beispiel #14
0
        /// <summary>
        /// Adds the specified extensions to the registry
        /// </summary>
        public void Add(params Extension[] newExtensions)
        {
            ProtoPreconditions.CheckNotNull(newExtensions, nameof(newExtensions));

            Add((IEnumerable <Extension>)newExtensions);
        }
Beispiel #15
0
 /// <summary>
 /// Parses <paramref name="json"/> into a new message.
 /// </summary>
 /// <typeparam name="T">The type of message to create.</typeparam>
 /// <param name="json">The JSON to parse.</param>
 /// <exception cref="InvalidJsonException">The JSON does not comply with RFC 7159</exception>
 /// <exception cref="InvalidProtocolBufferException">The JSON does not represent a Protocol Buffers message correctly</exception>
 public T Parse <T>(string json) where T : IMessage, new()
 {
     ProtoPreconditions.CheckNotNull(json, nameof(json));
     return(Parse <T>(new StringReader(json)));
 }
Beispiel #16
0
 /// <summary>
 /// Creates a new <see cref="CodedInputStream"/> reading data from the given stream.
 /// </summary>
 /// <param name="input">The stream to read from.</param>
 /// <param name="leaveOpen"><c>true</c> to leave <paramref name="input"/> open when the returned
 /// <c cref="CodedInputStream"/> is disposed; <c>false</c> to dispose of the given stream when the
 /// returned object is disposed.</param>
 public CodedInputStream(Stream input, bool leaveOpen) : this(ProtoPreconditions.CheckNotNull(input, "input"), new byte[BufferSize], 0, 0)
 {
     this.leaveOpen = leaveOpen;
 }
Beispiel #17
0
 /// <summary>
 /// Parses <paramref name="json"/> into a new message.
 /// </summary>
 /// <param name="json">The JSON to parse.</param>
 /// <param name="descriptor">Descriptor of message type to parse.</param>
 /// <exception cref="InvalidJsonException">The JSON does not comply with RFC 7159</exception>
 /// <exception cref="InvalidProtocolBufferException">The JSON does not represent a Protocol Buffers message correctly</exception>
 public IMessage Parse(string json, MessageDescriptor descriptor)
 {
     ProtoPreconditions.CheckNotNull(json, nameof(json));
     ProtoPreconditions.CheckNotNull(descriptor, nameof(descriptor));
     return(Parse(new StringReader(json), descriptor));
 }
 /// <summary>
 /// Creates a new CodedInputStream reading data from the given stream.
 /// </summary>
 public CodedInputStream(Stream input) : this(input, new byte[BufferSize], 0, 0)
 {
     ProtoPreconditions.CheckNotNull(input, "input");
 }