Ejemplo n.º 1
0
        /// <summary>
        /// Serializes a resource to a hal+json string using the provided <paramref name="representationGenerator"/>
        /// instance.
        /// </summary>
        /// <param name="representationGenerator">The <see cref="IHal"/> instance for generating a resource representation</param>
        /// <param name="resource">The object to serialize</param>
        /// <param name="serializerOptions"></param>
        /// <returns>A hal+json string</returns>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="InvalidJsonSerializerOptionsException">Thrown if the provided <paramref name="serializerOptions"/> does not have the required converters</exception>
        public static async Task <string> SerializeAsync(IHal representationGenerator, object resource, JsonSerializerOptions serializerOptions)
        {
            if (representationGenerator == null)
            {
                throw new ArgumentNullException(nameof(representationGenerator));
            }
            if (resource == null)
            {
                throw new ArgumentNullException(nameof(resource));
            }
            if (serializerOptions == null)
            {
                throw new ArgumentNullException(nameof(serializerOptions));
            }

            if (!HalJsonConvertersPresent(serializerOptions))
            {
                var converters = string.Join(", ", RequiredConverters.Select(x => x.Name));
                throw new InvalidJsonSerializerOptionsException($"The JSON converters [{converters}] are required to serialize to hal+json");
            }

            var representation = await representationGenerator.RepresentationOfAsync(resource);

            var json = JsonSerializer.Serialize(representation, serializerOptions);

            return(json);
        }
Ejemplo n.º 2
0
            protected internal override async ValueTask WriteBody(Stream stream, CancellationToken cancellationToken)
            {
                var representation = await _hal.RepresentationOfAsync(_resource);

                await stream.WriteAsync(Encoding.UTF8.GetBytes("<html><body>"), cancellationToken);

                await stream.WriteAsync(Encoding.UTF8.GetBytes(await Render(typeof(Links), representation)),
                                        cancellationToken);

                await stream.WriteAsync(Encoding.UTF8.GetBytes(await Render(_hal.GetType(), representation)),
                                        cancellationToken);

                await stream.WriteAsync(Encoding.UTF8.GetBytes("</body></html>"), cancellationToken);
            }
Ejemplo n.º 3
0
        protected override async ValueTask WriteBody(Stream stream, CancellationToken cancellationToken = default)
        {
            var representation = await _hal.RepresentationOfAsync(_resource);

            await JsonSerializer.SerializeAsync(stream, representation, SerializerOptions, cancellationToken);
        }