Skip to content

Commit 11de76c

Browse files
author
AlexD
committed
Added json-based example project
1 parent ee54e5c commit 11de76c

5 files changed

Lines changed: 82 additions & 8 deletions

File tree

AsyncAPI.sln

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LEGO.AsyncAPI.E2E.Tests", "
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LEGO.AsyncAPI.Readers", "src\LEGO.AsyncAPI.Readers\LEGO.AsyncAPI.Readers.csproj", "{7D9C6FBA-4B6F-48A0-B3F5-E7357021F8F9}"
1313
EndProject
14-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LEGO.AsyncAPI.Readers.YamlExample", "LEGO.AsyncAPI.Readers.YamlExample\LEGO.AsyncAPI.Readers.YamlExample.csproj", "{9DEC41AA-E164-4AFA-B154-53E22BCDB51B}"
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LEGO.AsyncAPI.Readers.YamlExample", "examples\LEGO.AsyncAPI.Readers.YamlExample\LEGO.AsyncAPI.Readers.YamlExample.csproj", "{D6314FDA-A897-4CEF-8EB1-DC46510BEB7C}"
15+
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LEGO.AsyncAPI.Readers.JsonExample", "examples\LEGO.AsyncAPI.Readers.JsonExample\LEGO.AsyncAPI.Readers.JsonExample.csproj", "{DACCA48F-6BA9-4552-B5A4-2D7ECAD328BE}"
1517
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -35,10 +37,14 @@ Global
3537
{7D9C6FBA-4B6F-48A0-B3F5-E7357021F8F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
3638
{7D9C6FBA-4B6F-48A0-B3F5-E7357021F8F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
3739
{7D9C6FBA-4B6F-48A0-B3F5-E7357021F8F9}.Release|Any CPU.Build.0 = Release|Any CPU
38-
{9DEC41AA-E164-4AFA-B154-53E22BCDB51B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39-
{9DEC41AA-E164-4AFA-B154-53E22BCDB51B}.Debug|Any CPU.Build.0 = Debug|Any CPU
40-
{9DEC41AA-E164-4AFA-B154-53E22BCDB51B}.Release|Any CPU.ActiveCfg = Release|Any CPU
41-
{9DEC41AA-E164-4AFA-B154-53E22BCDB51B}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{D6314FDA-A897-4CEF-8EB1-DC46510BEB7C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{D6314FDA-A897-4CEF-8EB1-DC46510BEB7C}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{D6314FDA-A897-4CEF-8EB1-DC46510BEB7C}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{D6314FDA-A897-4CEF-8EB1-DC46510BEB7C}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{DACCA48F-6BA9-4552-B5A4-2D7ECAD328BE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{DACCA48F-6BA9-4552-B5A4-2D7ECAD328BE}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{DACCA48F-6BA9-4552-B5A4-2D7ECAD328BE}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{DACCA48F-6BA9-4552-B5A4-2D7ECAD328BE}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE
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>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\src\LEGO.AsyncAPI.Readers\LEGO.AsyncAPI.Readers.csproj" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace LEGO.AsyncAPI.Readers.JsonExample
2+
{
3+
using System;
4+
using System.Text;
5+
using YamlDotNet.Serialization;
6+
7+
public class Program
8+
{
9+
static async Task Main(string[] args)
10+
{
11+
var httpClient = new HttpClient
12+
{
13+
BaseAddress = new Uri("https://raw.githubusercontent.com/asyncapi/spec/")
14+
};
15+
16+
var stream = await httpClient.GetStreamAsync("master/examples/streetlights-kafka.yml");
17+
18+
var streetlightKafkaYamlSpec = await new StreamReader(stream, Encoding.UTF8).ReadToEndAsync();
19+
20+
var streetlightKafkaJsonSpec = ConvertYamlToJson(streetlightKafkaYamlSpec);
21+
22+
var openApiDocument = new AsyncApiStringReader().Read(streetlightKafkaJsonSpec, out var diagnostic);
23+
24+
if (diagnostic.Error != null)
25+
{
26+
Console.WriteLine($"Error during spec parsing: {diagnostic.Error}");
27+
}
28+
else
29+
{
30+
Console.WriteLine("Kafka Starlight JSON spec successfully parsed into AsyncApiDocument object");
31+
Console.WriteLine($"Api version: {openApiDocument.Asyncapi}");
32+
Console.WriteLine($"Number of channels: {openApiDocument.Channels.Count}");
33+
}
34+
}
35+
36+
private static string ConvertYamlToJson(string input)
37+
{
38+
var deserializer = new Deserializer();
39+
var serializer = new SerializerBuilder().JsonCompatible().Build();
40+
var yamlObject = deserializer.Deserialize<object>(RemoveNonAscii(input));
41+
return serializer.Serialize(yamlObject);
42+
}
43+
44+
private static string RemoveNonAscii(string input) =>
45+
Encoding.ASCII.GetString(
46+
Encoding.Convert(
47+
Encoding.UTF8,
48+
Encoding.GetEncoding(
49+
Encoding.ASCII.EncodingName,
50+
new EncoderReplacementFallback(string.Empty),
51+
new DecoderExceptionFallback()),
52+
Encoding.UTF8.GetBytes(input)));
53+
}
54+
}

LEGO.AsyncAPI.Readers.YamlExample/LEGO.AsyncAPI.Readers.YamlExample.csproj renamed to examples/LEGO.AsyncAPI.Readers.YamlExample/LEGO.AsyncAPI.Readers.YamlExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<ProjectReference Include="..\src\LEGO.AsyncAPI.Readers\LEGO.AsyncAPI.Readers.csproj" />
11+
<ProjectReference Include="..\..\src\LEGO.AsyncAPI.Readers\LEGO.AsyncAPI.Readers.csproj" />
1212
</ItemGroup>
1313

1414
</Project>

LEGO.AsyncAPI.Readers.YamlExample/Program.cs renamed to examples/LEGO.AsyncAPI.Readers.YamlExample/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ static async Task Main(string[] args)
2121

2222
if (diagnostic.Error != null)
2323
{
24-
Console.WriteLine("Error during spec parsing: ", diagnostic.Error);
24+
Console.WriteLine($"Error during spec parsing: {diagnostic.Error}");
2525
}
2626
else
2727
{
28-
Console.WriteLine("Kafka Starlight spec successfully parsed into AsyncApiDocument object");
28+
Console.WriteLine("Kafka Starlight YAML spec successfully parsed into AsyncApiDocument object");
2929
Console.WriteLine($"Api version: {openApiDocument.Asyncapi}");
3030
Console.WriteLine($"Number of channels: {openApiDocument.Channels.Count}");
3131
}

0 commit comments

Comments
 (0)