Skip to content

Commit 1b72081

Browse files
committed
added json-e lessons
1 parent 13c2b7c commit 1b72081

File tree

12 files changed

+223
-13
lines changed

12 files changed

+223
-13
lines changed

LearnJsonEverything.LessonEditor/MainWindow.xaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
<Window.Resources>
1616
<hosts:SchemaHost x:Key="SchemaHost"/>
1717
<hosts:PathHost x:Key="PathHost"/>
18+
<hosts:JsonEHost x:Key="JsonEHost"/>
1819
</Window.Resources>
1920
<TabControl>
20-
<TabItem Header="Schema">
21+
<TabItem Header="JSON Schema">
2122
<controls:Editor FileName="schema.json" LessonHost="{StaticResource SchemaHost}"/>
2223
</TabItem>
23-
<TabItem Header="Path">
24+
<TabItem Header="JSON Path">
2425
<controls:Editor FileName="path.json" LessonHost="{StaticResource PathHost}"/>
2526
</TabItem>
27+
<TabItem Header="JSON-e">
28+
<controls:Editor FileName="json-e.json" LessonHost="{StaticResource JsonEHost}"/>
29+
</TabItem>
2630
</TabControl>
2731
</Window>

LearnJsonEverything.LessonEditor/ReferenceLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Json.JsonE;
12
using Json.More;
23
using Json.Path;
34
using Json.Schema;
@@ -16,6 +17,7 @@ static ReferenceLoader()
1617
Load<JsonSchema>();
1718
Load<MinimumAttribute>();
1819
Load<JsonPath>();
20+
Load<JsonFunction>();
1921
}
2022

2123
private static void Load<T>(){}

LearnJsonEverything.Tests/ProvidedSolutionTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,22 @@ public void Path(LessonData lesson)
6767
Assert.That(result, Does.StartWith(Iconography.SuccessIcon));
6868
}
6969
}
70+
71+
public static IEnumerable<TestCaseData> JsonELessons => GetLessons("json-e.json");
72+
73+
[TestCaseSource(nameof(JsonELessons))]
74+
public void JsonE(LessonData lesson)
75+
{
76+
var results = new JsonEHost().Run(lesson);
77+
78+
foreach (var result in results)
79+
{
80+
Console.WriteLine(result);
81+
}
82+
83+
foreach (var result in results)
84+
{
85+
Assert.That(result, Does.StartWith(Iconography.SuccessIcon));
86+
}
87+
}
7088
}

LearnJsonEverything.Tests/ReferenceLoader.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Json.JsonE;
12
using Json.More;
23
using Json.Path;
34
using Json.Schema;
@@ -16,6 +17,7 @@ static ReferenceLoader()
1617
Load<JsonSchema>();
1718
Load<MinimumAttribute>();
1819
Load<JsonPath>();
20+
Load<JsonFunction>();
1921
}
2022

2123
private static void Load<T>(){}

LearnJsonEverything/LearnJsonEverything.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
<ItemGroup>
1515
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
1616
<PackageReference Include="BlazorMonaco" Version="3.2.0" />
17-
<PackageReference Include="JsonPath.Net" Version="1.1.1" />
18-
<PackageReference Include="JsonSchema.Net" Version="7.0.4" />
17+
<PackageReference Include="JsonE.Net" Version="2.1.2" />
18+
<PackageReference Include="JsonPath.Net" Version="1.1.2" />
19+
<PackageReference Include="JsonSchema.Net" Version="7.1.2" />
1920
<PackageReference Include="JsonSchema.Net.Generation" Version="4.3.0.2" />
2021
<PackageReference Include="Markdig" Version="0.37.0" />
2122
<PackageReference Include="Markdig.SyntaxHighlighting" Version="1.1.7" />
22-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.5" />
23-
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.5" PrivateAssets="all" />
24-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.9.2" />
23+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.7" />
24+
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="8.0.7" PrivateAssets="all" />
25+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.10.0" />
2526
</ItemGroup>
2627

2728
<ItemGroup>

LearnJsonEverything/Pages/JsonE.razor

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@page "/json-e"
2+
@using LearnJsonEverything.Services.Hosts
3+
4+
<Teacher LessonSource="/data/lessons/json-e.json" Host="@_host"></Teacher>
5+
6+
@code {
7+
private readonly ILessonHost _host = new PathHost();
8+
}

LearnJsonEverything/Services/CompilationHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static class CompilationHelpers
1515
private static readonly string[] EnsuredAssemblies =
1616
[
1717
"Json.More",
18+
"JsonE.Net",
1819
"JsonPath.Net",
1920
"JsonPointer.Net",
2021
"JsonSchema.Net",
@@ -76,7 +77,7 @@ public static (ILessonRunner<T>?, string[]) GetRunner<T>(LessonData lesson)
7677

7778
Console.WriteLine($"Compiling...\n\n{fullSource}");
7879

79-
var syntaxTree = CSharpSyntaxTree.ParseText(fullSource);
80+
var syntaxTree = CSharpSyntaxTree.ParseText(fullSource, new CSharpParseOptions(LanguageVersion.Latest));
8081
var assemblyPath = Path.ChangeExtension(Path.GetTempFileName(), "dll");
8182

8283
var compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Text.Json.Nodes;
2+
using Json.More;
3+
4+
namespace LearnJsonEverything.Services.Hosts;
5+
6+
public class JsonEHost : ILessonHost
7+
{
8+
public string[] Run(LessonData lesson)
9+
{
10+
var (runner, errors) = CompilationHelpers.GetRunner<JsonNode?>(lesson);
11+
12+
if (runner is null) return errors;
13+
14+
var results = new List<string>();
15+
16+
var correct = true;
17+
foreach (var test in lesson.Tests)
18+
{
19+
var expectedResult = test!["result"];
20+
JsonNode? result = null;
21+
// need to capture this first because some of the lessons add functions, which don't print so well.
22+
var printedTest = test.Print();
23+
try
24+
{
25+
result = runner.Run(test.AsObject());
26+
}
27+
catch
28+
{
29+
// ignore
30+
}
31+
32+
var localResult = expectedResult.IsEquivalentTo(result);
33+
correct &= localResult;
34+
results.Add($"{(localResult ? Iconography.SuccessIcon : Iconography.ErrorIcon)} {printedTest}");
35+
}
36+
37+
lesson.Achieved |= correct;
38+
39+
return [.. results];
40+
}
41+
}

LearnJsonEverything/Services/SerializationHelpers.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.CodeAnalysis.CSharp;
44
using System.Text.Json.Nodes;
55
using System.Text.Json.Serialization;
6+
using Json.JsonE;
67
using Json.More;
78
using Json.Schema;
89

@@ -29,6 +30,7 @@ public static string ToLiteral(this string valueTextForCompiler)
2930

3031
[JsonSerializable(typeof(JsonSchema))]
3132
[JsonSerializable(typeof(EvaluationResults))]
33+
[JsonSerializable(typeof(JsonFunction))]
3234
[JsonSerializable(typeof(JsonNode))]
3335
[JsonSerializable(typeof(JsonObject))]
3436
[JsonSerializable(typeof(JsonArray))]

LearnJsonEverything/Shared/NavMenu.razor

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
<div class="feature-description text-muted-light">Query JSON data - "XPath for JSON"</div>
1414
</div>
1515
</NavLink>
16-
<NavLink class="col mx-3 text-center btn-primary header-btn disabled" href="json-e">
17-
<div class="stamp">
18-
COMING SOON
19-
</div>
16+
<NavLink class="col mx-3 text-center btn-primary header-btn" href="json-e">
2017
<div>
2118
<div>JSON-e</div>
2219
<div class="feature-description text-muted-light">Templating and transformation of JSON data</div>

0 commit comments

Comments
 (0)