FluentJsonApi.AspNetCore 0.1.1

dotnet add package FluentJsonApi.AspNetCore --version 0.1.1
                    
NuGet\Install-Package FluentJsonApi.AspNetCore -Version 0.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FluentJsonApi.AspNetCore" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="FluentJsonApi.AspNetCore" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="FluentJsonApi.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add FluentJsonApi.AspNetCore --version 0.1.1
                    
#r "nuget: FluentJsonApi.AspNetCore, 0.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package FluentJsonApi.AspNetCore@0.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=FluentJsonApi.AspNetCore&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=FluentJsonApi.AspNetCore&version=0.1.1
                    
Install as a Cake Tool

FluentJsonApi.AspNetCore

FluentJsonApi.AspNetCore is a .NET library for building JSON:API compliant APIs using a fluent interface. This library simplifies the creation of JSON:API documents and resources, making it easier to adhere to the JSON:API specification.

Features

  • Fluent interface for building JSON:API documents
  • Support for defining resource types, attributes, and relationships
  • Serialization and deserialization of JSON:API documents
  • Integration with ASP.NET Core

Installation

To install FluentJsonApi.AspNetCore, add the following package reference to your project:

<PackageReference Include="FluentJsonApi.AspNetCore" Version="1.0.0" />

Usage

Defining Resources

Define your resources by creating classes and configuring them using the JsonApiBuilder.

var jsonApiBuilder = new JsonApiBuilder();
jsonApiBuilder.Entity<Article>()
    .Type("articles")
    .Id(a => a.Id)
    .Attribute(a => a.Title)
    .Attribute(a => a.Content)
    .Relationship(a => a.Author, (a, r) =>
    {
        r.Type = "people";
        r.Links = new JsonApiLinks()
        {
            Self = $"/articles/{a.Id}/relationships/author",
            Related = $"/articles/{a.Id}/author"
        };
    });

Building JSON:API Documents

Use the JsonApiDocumentBuilder to create JSON:API documents.

var documentBuilder = new JsonApiDocumentBuilder(jsonApiBuilder);
var article = new Article
{
    Id = 1,
    Title = "JSON:API",
    Content = "JSON:API is a specification for building APIs in JSON",


 Author

 = "John Doe"
};
var document = documentBuilder.BuildDocument(article);

Serializing and Deserializing

Serialize and deserialize JSON:API documents using System.Text.Json.

var json = JsonSerializer.Serialize(document, new JsonSerializerOptions
{
    DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});

var deserializedDocument = JsonSerializer.Deserialize<JsonApiDocument>(json);

Unit Testing

Unit tests for the library can be found in the FluentJsonApi.AspNetCore.Tests project. Example test:

[Fact]
public void CanBuildJsonApiDocumentCollection()
{
    var jsonApiBuilder = new JsonApiBuilder();
    jsonApiBuilder.Entity<Article>()
        .Type("articles")
        .Id(a => a.Id)
        .Attribute(a => a.Title)
        .Attribute(a => a.Content)
        .Relationship(a => a.Author, (a, r) =>
        {
            r.Type = "people";
            r.Links = new JsonApiLinks()
            {
                Self = $"/articles/{a.Id}/relationships/author",
                Related = $"/articles/{a.Id}/author"
            };
        });

    var documentBuilder = new JsonApiDocumentBuilder(jsonApiBuilder);
    var articles = new List<Article>
    {
        new() {
            Id = 1,
            Title = "JSON:API",
            Content = "JSON:API is a specification for building APIs in JSON",
            Author = "John Doe"
        },
        new() {
            Id = 2,
            Title = "REST",
            Content = "REST (Representational State Transfer) is an architectural style for distributed hypermedia systems",
            Author = "Jane Doe"
        }
    };

    var document = documentBuilder.BuildDocument(articles);
    var json = JsonSerializer.Serialize(document, new JsonSerializerOptions
    {
        DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase
    });

    var jsonDeserialized = JsonSerializer.Deserialize<JsonApiDocumentCollection>(json);
    Assert.Equal(articles.Count, jsonDeserialized.Data.Count);
}

License

This project is licensed under the MIT License. See the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Contact

For questions or feedback, please contact the project maintainers.


This README provides an overview of the FluentJsonApi.AspNetCore library, including installation instructions, usage examples, and information on unit testing, licensing, and contributing.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.1 128 12/10/2024
0.1.0 104 12/10/2024