public void RequiresHalConvertersInJsonSerializerOptions()
        {
            var jsonSerializerOptions = new JsonSerializerOptions();
            Func<Task<string>> action = async () => await HalJsonSerializer.SerializeAsync(new DefaultRepresentation(), new DummyModel(), jsonSerializerOptions);

            action.Should().Throw<InvalidJsonSerializerOptionsException>();
        }
Beispiel #2
0
        /// <summary>
        /// Serializes the provided resource to the <see cref="HttpResponse"/> stream.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This method will try to resolve an instance of <see cref="JsonSerializerOptions"/>
        /// from the <see cref="IServiceProvider"/>. If no instance is found then
        /// <see cref="HalJsonSerializer.DefaultSerializerOptions"/> will be used.
        /// </p>
        /// <p>
        /// When no HAL document generation has been implemented for the resource
        /// type being serialized it will fall back to basic JSON serialization using
        /// the configured <see cref="JsonSerializerOptions"/>
        /// </p>
        /// </remarks>
        /// <param name="context">The HttpContext to write the serialized object to</param>
        /// <param name="resource">The object to serialize</param>
        public static async Task HalHandler(HttpContext context, object resource)
        {
            var services                = context.RequestServices;
            var serializerOptions       = context.RequestServices.GetService <JsonSerializerOptions>() ?? HalJsonSerializer.DefaultSerializerOptions;
            var representationGenerator = services.GetRepresentationGenerator(resource.GetType());

            if (representationGenerator == null)
            {
                context.Response.ContentType = "application/json";

                await JsonSerializer.SerializeAsync(context.Response.Body, resource, resource.GetType(), serializerOptions);

                await context.Response.Body.FlushAsync();

                return;
            }

            context.Response.ContentType = "application/hal+json";

            var json = await HalJsonSerializer.SerializeAsync(representationGenerator, resource, serializerOptions);

            await context.Response.WriteAsync(json);

            await context.Response.Body.FlushAsync();
        }
        public async Task UsesRelaxedCharacterEncoding()
        {
            var json = await HalJsonSerializer.SerializeAsync(new DefaultRepresentation(), new DummyModel
            {
                Property = "+++"
            }, HalJsonSerializer.DefaultSerializerOptions);

            json.Should().Contain("+++", because: "the 'plus' unicode character should not be escaped");
        }
        public void GuardsAgainstNulls()
        {
            Func<Task<string>> nullGeneratorAction = async () => await HalJsonSerializer.SerializeAsync(null!, new DummyModel(), HalJsonSerializer.DefaultSerializerOptions);
            Func<Task<string>> nullResourceAction = async () => await HalJsonSerializer.SerializeAsync(new DefaultRepresentation(), null!, HalJsonSerializer.DefaultSerializerOptions);
            Func<Task<string>> nullJsonSerializerOptionsAction = async () => await HalJsonSerializer.SerializeAsync(new DefaultRepresentation(), new DummyModel(), null!);

            nullGeneratorAction.Should().Throw<ArgumentNullException>();
            nullResourceAction.Should().Throw<ArgumentNullException>();
            nullJsonSerializerOptionsAction.Should().Throw<ArgumentNullException>();
        }
        public async Task UsesCamelCasePropertyNaming()
        {
            var rawJson = await HalJsonSerializer.SerializeAsync(new DefaultRepresentation(), new DummyModel
            {
                Id       = 123,
                Property = "+++"
            }, HalJsonSerializer.DefaultSerializerOptions);

            var json = JsonDocument.Parse(rawJson).RootElement;

            json.TryGetProperty("Id", out _).Should().BeFalse();
            json.TryGetProperty("id", out _).Should().BeTrue();
            json.TryGetProperty("Property", out _).Should().BeFalse();
            json.TryGetProperty("property", out _).Should().BeTrue();
        }
 private static async Task<JsonElement> Serialize(IHal representationGenerator, object resource)
 {
     var json = await HalJsonSerializer.SerializeAsync(representationGenerator, resource, HalJsonSerializer.DefaultSerializerOptions);
     return JsonDocument.Parse(json).RootElement;
 }