Ejemplo n.º 1
0
        /// <summary>
        /// Formats the specified message as JSON.
        /// </summary>
        /// <param name="message">The message to format.</param>
        /// <param name="writer">The TextWriter to write the formatted message to.</param>
        /// <returns>The formatted message.</returns>
        public void Format(IMessage message, TextWriter writer)
        {
            ProtoPreconditions.CheckNotNull(message, nameof(message));
            ProtoPreconditions.CheckNotNull(writer, nameof(writer));

            if (message.Descriptor.IsWellKnownType)
            {
                WriteWellKnownTypeValue(writer, message.Descriptor, message);
            }
            else
            {
                WriteMessage(writer, message);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructs a <see cref="ByteString"/> from data in the given stream, synchronously.
        /// </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>
        /// <returns>A ByteString with content read from the given stream.</returns>
        public static ByteString FromStream(Stream stream)
        {
            ProtoPreconditions.CheckNotNull(stream, nameof(stream));
            int capacity     = stream.CanSeek ? checked ((int)(stream.Length - stream.Position)) : 0;
            var memoryStream = new MemoryStream(capacity);

            stream.CopyTo(memoryStream);
#if NETSTANDARD1_1 || NETSTANDARD2_0
            byte[] bytes = memoryStream.ToArray();
#else
            // Avoid an extra copy if we can.
            byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray();
#endif
            return(AttachBytes(bytes));
        }
Ejemplo n.º 3
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 async static Task <ByteString> FromStreamAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
        {
            ProtoPreconditions.CheckNotNull(stream, nameof(stream));
            int capacity     = stream.CanSeek ? checked ((int)(stream.Length - stream.Position)) : 0;
            var memoryStream = new MemoryStream(capacity);
            // We have to specify the buffer size here, as there's no overload accepting the cancellation token
            // alone. But it's documented to use 81920 by default if not specified.
            await stream.CopyToAsync(memoryStream, 81920, cancellationToken);

#if NETSTANDARD1_1 || NETSTANDARD2_0
            byte[] bytes = memoryStream.ToArray();
#else
            // Avoid an extra copy if we can.
            byte[] bytes = memoryStream.Length == memoryStream.Capacity ? memoryStream.GetBuffer() : memoryStream.ToArray();
#endif
            return(AttachBytes(bytes));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Copies the arrays in the registry set to the specified array at the specified index
        /// </summary>
        /// <param name="array">The array to copy to</param>
        /// <param name="arrayIndex">The array index to start at</param>
        void ICollection <Extension> .CopyTo(Extension[] array, int arrayIndex)
        {
            ProtoPreconditions.CheckNotNull(array, nameof(array));
            if (arrayIndex < 0 || arrayIndex >= array.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(arrayIndex));
            }
            if (array.Length - arrayIndex < Count)
            {
                throw new ArgumentException("The provided array is shorter than the number of elements in the registry");
            }

            for (int i = 0; i < array.Length; i++)
            {
                Extension extension = array[i];
                extensions.Add(new ObjectIntPair <Type>(extension.TargetType, extension.FieldNumber), extension);
            }
        }
Ejemplo n.º 5
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));
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Creates a new formatted with the given settings.
 /// </summary>
 /// <param name="settings">The settings.</param>
 public JsonFormatter(Settings settings)
 {
     this.settings = ProtoPreconditions.CheckNotNull(settings, nameof(settings));
 }
Ejemplo n.º 7
0
 /// <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()));
 }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
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)));
        }