JSON, YAML and XML
Load lists and single objects from JSON, YAML or XML files into the same kind of typed record.
On this page
[DataSource("Authors", "./authors.json")]
public sealed record Author(string Id, string Name);
Pick the format with the file extension. For TOML, CSV or another format, teach Bevel with a small reader (Teach Bevel a new format). The record stays the same. Every format is strict: a missing value without a default fails the build (see Required values and Unique).
A root array gives IReadOnlyList<Author>.
[
{ "id": "khalid", "name": "Khalid Abuhakmeh" },
{ "id": "ada", "name": "Ada Sterling" }
]
[DataSource("Authors", "./authors.json")]
public sealed record Author(string Id, string Name);
A root object gives a single T. Use .yaml or .yml.
title: The Bevel Dispatch
description: Essays on ASP.NET Core.
[DataSource("Site", "./site.yaml")]
public sealed record SiteConfig(string Title, string Description);
Name the repeating element with Element. Bevel binds constructor parameters by attribute or child-element name, ignoring case.
<navigation>
<link label="Journal" href="/" location="header" />
<link label="Sitemap" href="/sitemap.xml" location="footer" />
</navigation>
[DataSource("Navigation", "./navigation.xml", Element = "link")]
public sealed record NavItem(string Label, string Href, string Location = "header");
Read it in a page
@foreach (var link in Data.Navigation) { <a href="@link.Href">@link.Label</a> }
<h1>@Data.Site.Title</h1>
Single objects (Site) are T. Lists are IReadOnlyList<T>.