All Projects → fiyazbinhasan → CoreFormatters

fiyazbinhasan / CoreFormatters

Licence: other
.NET Core Custom Formatter for Yaml

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to CoreFormatters

DevOpsExamples
A repo to show you how to use a private NuGet feed, such as Telerik, to restore packages in Azure DevOps, GitHub Actions, GitLab CI and AppCenter.
Stars: ✭ 16 (-23.81%)
Mutual labels:  aspnetcore
BlazorServerWithDocker
Companion code sample for my blog post - Containerising a Blazor Server App
Stars: ✭ 16 (-23.81%)
Mutual labels:  aspnetcore
counsel-jq
Traverse complex JSON and YAML structures with live feedback
Stars: ✭ 99 (+371.43%)
Mutual labels:  yaml
Config
PHP library for simple configuration management
Stars: ✭ 39 (+85.71%)
Mutual labels:  yaml
crystalizer
(De)serialize any Crystal object - out of the box. Supports JSON, YAML and Byte format.
Stars: ✭ 32 (+52.38%)
Mutual labels:  yaml
hk-cache-manager
Simple wrapper for Redis Cache with Stackoverflow.Redis & AspNetCore aim
Stars: ✭ 17 (-19.05%)
Mutual labels:  aspnetcore
goodconf
Transparently load variables from environment or JSON/YAML file.
Stars: ✭ 80 (+280.95%)
Mutual labels:  yaml
website
Prometheus monitoring mixins
Stars: ✭ 91 (+333.33%)
Mutual labels:  yaml
Obsidian-Markdown-Parser
This repository will give you tools to parse and fetch useful informations of your notes in your Obsidian vault.
Stars: ✭ 32 (+52.38%)
Mutual labels:  yaml
yamlful
YAML-based HTTP client code generation
Stars: ✭ 77 (+266.67%)
Mutual labels:  yaml
AspNetCore.Mvc.FluentActions
Fluent Actions for ASP.NET Core MVC are abstractions of regular MVC actions that are converted into MVC actions during startup.
Stars: ✭ 17 (-19.05%)
Mutual labels:  aspnetcore
gsheet to arb
Import translations (ARB/Dart) from Google Sheets
Stars: ✭ 21 (+0%)
Mutual labels:  yaml
python-yamlordereddictloader
(DEPRECATED) YAML loader and dumper for PyYAML allowing to keep keys order.
Stars: ✭ 25 (+19.05%)
Mutual labels:  yaml
yaask
Make your yaml configurable with interactive configurations!
Stars: ✭ 15 (-28.57%)
Mutual labels:  yaml
yaml-front-matter
A to the point yaml front matter parser
Stars: ✭ 200 (+852.38%)
Mutual labels:  yaml
JqueryDataTablesServerSideDemo
Jquery DataTables with Asp.Net Core server side multi column sorting and searching Demo Project.
Stars: ✭ 43 (+104.76%)
Mutual labels:  aspnetcore
pyaml env
Parse YAML configuration with environment variables in Python
Stars: ✭ 36 (+71.43%)
Mutual labels:  yaml
rbac-react-redux-aspnetcore
A starter template for creating JWT token from ASP.NET Core API project and applying that JWT token authentication on React application
Stars: ✭ 54 (+157.14%)
Mutual labels:  aspnetcore
examples
Example Prismatic components and integrations
Stars: ✭ 23 (+9.52%)
Mutual labels:  yaml
openapiclientgen
Generate C# and TypeScript client codes from Open API / Swagger definitions
Stars: ✭ 31 (+47.62%)
Mutual labels:  yaml

.NET Core Custom Formatters

This repository used to contain custom formatters for pdf and excel. They are removed from main branch as the libraries are deprecated. Please check previous commits if you are interested in those.

.Net Core gives you some formatters out of the box. This official documentation link described them briefly,

https://docs.microsoft.com/en-us/aspnet/core/mvc/models/formatting

We have two abstract classes provided by the framework, InputFormmeter and OutputFormatter. Basically you would want to use these classes to make your own formatters. But there are other two abstract classes that extend from those two formatters. TextInputFormatter and TextOuputFormatter can work with response that are simple string representations of data formats.

When using the Yaml output formatter you would get the response (returned value of the controller's action) out of the current HttpContext and Serialize them into raw Yaml response text and send them back to the client. Pretty much same goes for the input formatter. In this case, you would Deserialize the Yaml content from the client's request and use them in a generic form. Another important thing is, you have to explicitly set media type header for these formatters. Doing that will activate these formatters whenever a client defines a Accept header (for output) and Content-Type (for input) with that specific media type format (application/x-yaml).

If you don’t want to use those headers while calling your controller’s actions you can explicitly define the type of formatter that should be used while getting or posting content. For example, the [Produces(application/x-yaml)] will return the response in Yaml format whether you define a Accept header or not. Again using the [Consumes(application/x-yaml)] attribute would only accept Yaml content whether you define the Content-Type or not.

Yaml Output Formatter

public class YamlOutputFormatter : TextOutputFormatter
{
  private readonly Serializer _serializer;

  public YamlOutputFormatter(Serializer serializer)
  {
    _serializer = serializer;

    SupportedEncodings.Add(Encoding.UTF8);
    SupportedEncodings.Add(Encoding.Unicode);
    SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationYaml);
    SupportedMediaTypes.Add(MediaTypeHeaderValues.TextYaml);
  }

  public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
  {
    if (context == null)
    {
        throw new ArgumentNullException(nameof(context));
    }

    if (selectedEncoding == null)
    {
        throw new ArgumentNullException(nameof(selectedEncoding));
    }

    var response = context.HttpContext.Response;
    using (var writer = context.WriterFactory(response.Body, selectedEncoding))
    {
        WriteObject(writer, context.Object);

        await writer.FlushAsync();
    }
  }

  private void WriteObject(TextWriter writer, object value)
  {
    if (writer == null)
    {
        throw new ArgumentNullException(nameof(writer));
    }

    _serializer.Serialize(writer, value);
  }
}

Pretty much same goes for the input formatter. In this case, you would Deserialize the Yaml content from the client's request and use them in a generic form.

Yaml Input Formatter

public class YamlInputFormatter : TextInputFormatter
{
  private readonly Deserializer _deserializer;

  public YamlInputFormatter(Deserializer deserializer)
  {
    _deserializer = deserializer;

    SupportedEncodings.Add(Encoding.UTF8);
    SupportedEncodings.Add(Encoding.Unicode);
    SupportedMediaTypes.Add(MediaTypeHeaderValues.ApplicationYaml);
    SupportedMediaTypes.Add(MediaTypeHeaderValues.TextYaml);
  }

  public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context, Encoding encoding)
  {
    if (context == null)
    {
        throw new ArgumentNullException(nameof(context));
    }

    if (encoding == null)
    {
        throw new ArgumentNullException(nameof(encoding));
    }

    var request = context.HttpContext.Request;

    using (var streamReader = context.ReaderFactory(request.Body, encoding))
    {
        var type = context.ModelType;

        try
        {
            var model = _deserializer.Deserialize(streamReader, type);
            return InputFormatterResult.SuccessAsync(model);
        }
        catch (Exception)
        {
            return InputFormatterResult.FailureAsync();
        }
    }
  }
}

MediaTypeHeaderValues a simple class where I've setup all the media type headers for my application.

internal class MediaTypeHeaderValues
{
  public static readonly MediaTypeHeaderValue ApplicationYaml
      = MediaTypeHeaderValue.Parse("application/x-yaml").CopyAsReadOnly();

  public static readonly MediaTypeHeaderValue TextYaml
      = MediaTypeHeaderValue.Parse("text/yaml").CopyAsReadOnly();
}

Notice that the YamlInputFormatter’s constructor is accepting a Deserializer where YamlOutputFormatter’s constructor is accepting a Serializer. We build the Serializer and Deserializer with some options tweaking while configuring the formatters in the Startup.cs’s ConfigureServices method.

public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
  services.AddMvc(options=>
  {
    options.InputFormatters.Add(new YamlInputFormatter(new DeserializerBuilder().WithNamingConvention(namingConvention: new CamelCaseNamingConvention()).Build()));
    options.OutputFormatters.Add(new YamlOutputFormatter(new SerializerBuilder().WithNamingConvention(namingConvention: new CamelCaseNamingConvention()).Build()));
    options.FormatterMappings.SetMediaTypeMappingForFormat("yaml", MediaTypeHeaderValues.ApplicationYaml);
  });
}

A simple GET request with Accept header set to application/x-yaml

alt text

A simple POST request with Content-Type header set to application/x-yaml

alt text

Formatter mapper lets you call a Action with a specific format directly through the Url. Setting up a [HttpGet("/api/[controller].{format}")] attribute will return the action result in the format defined in the browser’s url.

[FormatFilter]
[HttpGet]
[HttpGet("/api/[controller].{format}")]
public IEnumerable<Geek> Get()
{
    return new List<Geek>()
    {
        new Geek() { Id = 1, Name = "Fiyaz", Expertise="Javascript", Rating = 3.0M },
        new Geek() { Id = 2, Name = "Rick", Expertise = ".Net", Rating = 5.0M }
    };
}

alt text

Dependencies

I’m using the YamlDotNet library from Antoine Aubry for Yaml’s serializing and desirializing process.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].