Пример #1
0
    public void Json_WithNoArgs_ResultHasCorrectValues()
    {
        // Arrange
        var data = default(object);

        // Act
        var result = TypedResults.Json(data);

        // Assert
        Assert.Null(result.Value);
        Assert.Null(result.JsonSerializerOptions);
        Assert.Null(result.ContentType);
        Assert.Null(result.StatusCode);
    }
Пример #2
0
    public void Json_WithAllArgs_ResultHasCorrectValues()
    {
        // Arrange
        var data        = new { };
        var options     = new JsonSerializerOptions();
        var contentType = "application/custom+json";
        var statusCode  = StatusCodes.Status208AlreadyReported;

        // Act
        var result = TypedResults.Json(data, options, contentType, statusCode);

        // Assert
        Assert.Equal(data, result.Value);
        Assert.Equal(options, result.JsonSerializerOptions);
        Assert.Equal(contentType, result.ContentType);
        Assert.Equal(statusCode, result.StatusCode);
    }
Пример #3
0
 /// <summary>
 /// Creates a <see cref="IResult"/> that serializes the specified <paramref name="data"/> object to JSON.
 /// </summary>
 /// <param name="data">The object to write as JSON.</param>
 /// <param name="options">The serializer options to use when serializing the value.</param>
 /// <param name="contentType">The content-type to set on the response.</param>
 /// <param name="statusCode">The status code to set on the response.</param>
 /// <returns>The created <see cref="JsonHttpResult{TValue}"/> that serializes the specified <paramref name="data"/>
 /// as JSON format for the response.</returns>
 /// <remarks>Callers should cache an instance of serializer settings to avoid
 /// recreating cached data with each call.</remarks>
 public static IResult Json(object?data, JsonSerializerOptions?options = null, string?contentType = null, int?statusCode = null)
 => TypedResults.Json(data, options, contentType, statusCode);
Пример #4
0
    /// <summary>
    /// Creates a <see cref="IResult"/> that serializes the specified <paramref name="data"/> object to JSON.
    /// </summary>
    /// <param name="data">The object to write as JSON.</param>
    /// <param name="options">The serializer options to use when serializing the value.</param>
    /// <param name="contentType">The content-type to set on the response.</param>
    /// <param name="statusCode">The status code to set on the response.</param>
    /// <returns>The created <see cref="JsonHttpResult{TValue}"/> that serializes the specified <paramref name="data"/>
    /// as JSON format for the response.</returns>
    /// <remarks>Callers should cache an instance of serializer settings to avoid
    /// recreating cached data with each call.</remarks>
#pragma warning disable RS0026 // Do not add multiple public overloads with optional parameters
    public static IResult Json <TValue>(TValue?data, JsonSerializerOptions?options = null, string?contentType = null, int?statusCode = null)
#pragma warning restore RS0026 // Do not add multiple public overloads with optional parameters
    => TypedResults.Json(data, options, contentType, statusCode);