Skip to content

Commit cf73c75

Browse files
committed
✨ add basic example
1 parent ad29815 commit cf73c75

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

Json5.sln

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1616
EndProject
1717
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Json5.Tests", "Json5.Tests\Json5.Tests.csproj", "{2D81813A-D9A3-49C5-9940-70500CF2F42D}"
1818
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{58D535B0-AE32-4202-8D70-B9206667D248}"
20+
EndProject
21+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicUsage", "examples\BasicUsage\BasicUsage.csproj", "{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}"
22+
EndProject
1923
Global
2024
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2125
Debug|Any CPU = Debug|Any CPU
@@ -30,8 +34,15 @@ Global
3034
{2D81813A-D9A3-49C5-9940-70500CF2F42D}.Debug|Any CPU.Build.0 = Debug|Any CPU
3135
{2D81813A-D9A3-49C5-9940-70500CF2F42D}.Release|Any CPU.ActiveCfg = Release|Any CPU
3236
{2D81813A-D9A3-49C5-9940-70500CF2F42D}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B}.Release|Any CPU.Build.0 = Release|Any CPU
3341
EndGlobalSection
3442
GlobalSection(SolutionProperties) = preSolution
3543
HideSolutionNode = FALSE
3644
EndGlobalSection
45+
GlobalSection(NestedProjects) = preSolution
46+
{E7D3925C-54A8-4B40-95A2-FA246F5A4A6B} = {58D535B0-AE32-4202-8D70-B9206667D248}
47+
EndGlobalSection
3748
EndGlobal

examples/BasicUsage/BasicUsage.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\Json5\Json5.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

examples/BasicUsage/Program.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Text.Json;
2+
3+
var json5 =
4+
"""
5+
{
6+
description: 'This is a basic example showing how to translate JSON5 to JSON, \
7+
how to parse JSON5 as a `System.Text.Json.Nodes.JsonNode`, \
8+
and how to deserialize JSON5 to a C# type.',
9+
nums: [.1, -Infinity, NaN, 2.e-3, 0b101, 0xBEEF],
10+
}
11+
""";
12+
13+
Console.WriteLine(
14+
$"""
15+
JSON5:
16+
17+
{json5}
18+
19+
Translated to JSON:
20+
21+
{Json5.Parser.Translate(json5)}
22+
23+
Parsed as JsonNode:
24+
25+
{Json5.Parser.Parse(json5)}
26+
27+
""");
28+
29+
// `JsonSerializerOptions.Web` makes property names case-insensitive and allows the
30+
// floating point literals "NaN" and "Infinity".
31+
Console.WriteLine(
32+
$"""
33+
Deserialized as record:
34+
35+
{Json5.Parser.Parse(json5).Deserialize<Foo>(JsonSerializerOptions.Web)}
36+
""");
37+
38+
public record Foo(string Description, double[] Nums) {
39+
public override string ToString()
40+
=> $"Foo {{ Description = {Description[..15]}..., Nums = [{string.Join(", ", Nums)}] }}";
41+
}

0 commit comments

Comments
 (0)