Skip to content

Commit 62a3484

Browse files
committed
the layout works
1 parent 276a646 commit 62a3484

File tree

15 files changed

+315
-73
lines changed

15 files changed

+315
-73
lines changed

LearnJsonEverything/.editorconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[*.cs]
2+
3+
# IDE0290: Use primary constructor
4+
dotnet_diagnostic.IDE0290.severity = none

LearnJsonEverything/LearnJsonEverything.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="Blazored.LocalStorage" Version="4.5.0" />
1313
<PackageReference Include="BlazorMonaco" Version="3.2.0" />
14+
<PackageReference Include="JsonSchema.Net" Version="7.0.3" />
1415
<PackageReference Include="Markdig" Version="0.37.0" />
1516
<PackageReference Include="Markdig.SyntaxHighlighting" Version="1.1.7" />
1617
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.5" />
@@ -26,8 +27,4 @@
2627
</Content>
2728
</ItemGroup>
2829

29-
<ItemGroup>
30-
<Folder Include="wwwroot\data\" />
31-
</ItemGroup>
32-
3330
</Project>

LearnJsonEverything/Pages/Index.razor

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,48 @@
66
<div class="row mx-3 fill-remaining">
77
<div class="mh-100 scroll text-center">
88
<h1 class="display-4 mt-5 mb-4">
9-
<span class="font-monospace text-muted">json-everything</span>
9+
Learn <span class="font-monospace text-muted">json-everything</span>
1010
</h1>
11-
<p class="lead mb-5">Extended JSON support in .Net built on top of the System.Text.Json namespace</p>
12-
<Banner Content="All libraries now support .Net 8 and Native AOT!!"></Banner>
11+
<p class="lead mb-5">An interactive guide to utilizing JSON in .Net</p>
1312

1413
<Feature Title="JSON Schema"
1514
Description="@IndexContent.SchemaTagline"
16-
PlaygroundUrl="json-schema"
15+
LessonsUrl="json-schema"
1716
DocsUrl="https://docs.json-everything.net/schema/basics/"
1817
ImageUrl="img/json-schema.png">
1918
</Feature>
20-
<Feature Title="JSON Path"
19+
@* <Feature Title="JSON Path"
2120
Description="@IndexContent.PathTagline"
22-
PlaygroundUrl="json-path"
21+
LessonsUrl="json-path"
2322
DocsUrl="https://docs.json-everything.net/path/basics/"
2423
ImageUrl="img/json-path.png"
2524
Flipped="true">
2625
</Feature>
2726
<Feature Title="JSON-e"
2827
Description="@IndexContent.JsonETagline"
29-
PlaygroundUrl="json-e"
28+
LessonsUrl="json-e"
3029
DocsUrl="https://docs.json-everything.net/json-e/basics/"
3130
ImageUrl="img/json-e.png">
3231
</Feature>
3332
<Feature Title="JSON Logic"
3433
Description="@IndexContent.LogicTagline"
35-
PlaygroundUrl="json-logic"
34+
LessonsUrl="json-logic"
3635
DocsUrl="https://docs.json-everything.net/pointer/basics/"
3736
ImageUrl="img/json-logic.png"
3837
Flipped="true">
3938
</Feature>
4039
<Feature Title="JSON Patch"
4140
Description="@IndexContent.PatchTagline"
42-
PlaygroundUrl="json-patch"
41+
LessonsUrl="json-patch"
4342
DocsUrl="https://docs.json-everything.net/patch/basics/"
4443
ImageUrl="img/json-patch.png">
4544
</Feature>
4645
<Feature Title="JSON Pointer"
4746
Description="@IndexContent.PointerTagline"
48-
PlaygroundUrl="json-pointer"
47+
LessonsUrl="json-pointer"
4948
DocsUrl="https://docs.json-everything.net/pointer/basics/"
5049
ImageUrl="img/json-pointer.png"
5150
Flipped="true">
5251
</Feature>
53-
</div>
52+
*@ </div>
5453
</div>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@page "/json-schema"
2+
3+
<Teacher LessonSource="/data/lessons/schema.json"></Teacher>
4+
5+
@code {
6+
7+
}

LearnJsonEverything/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
}
99
},
1010
"profiles": {
11-
"json-everything.net": {
11+
"learn.json-everything.net": {
1212
"commandName": "Project",
1313
"dotnetRunMessages": true,
1414
"launchBrowser": true,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace LearnJsonEverything.Services;
2+
3+
public class LessonData
4+
{
5+
public int Index { get; set; }
6+
public Guid Id { get; set; }
7+
public string Title { get; set; }
8+
public string Instructions { get; set; }
9+
public string CodeTemplate { get; set; }
10+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace LearnJsonEverything.Services;
4+
5+
[JsonConverter(typeof(LessonPlanJsonConverter))]
6+
public class LessonPlan
7+
{
8+
private readonly LessonData[] _lessonData;
9+
private readonly Dictionary<Guid, int> _indexLookup;
10+
11+
public LessonPlan(LessonData[] lessonData)
12+
{
13+
_lessonData = lessonData;
14+
_indexLookup = lessonData.Select((d, i) => (d.Id, i)).ToDictionary(x => x.Id, x => x.i);
15+
}
16+
17+
public LessonData? GetPrevious(Guid? id)
18+
{
19+
if (id is null) return _lessonData[0];
20+
21+
if (!_indexLookup.TryGetValue(id.Value, out var index)) return null;
22+
23+
index = Math.Max(0, index - 1);
24+
25+
return _lessonData[index];
26+
}
27+
28+
public LessonData? GetNext(Guid? id)
29+
{
30+
if (id is null) return _lessonData[0];
31+
32+
if (!_indexLookup.TryGetValue(id.Value, out var index)) return null;
33+
34+
index = Math.Min(_lessonData.Length - 1, index + 1);
35+
36+
return _lessonData[index];
37+
}
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Text.Json;
2+
using System.Text.Json.Serialization;
3+
4+
namespace LearnJsonEverything.Services;
5+
6+
public class LessonPlanJsonConverter : JsonConverter<LessonPlan>
7+
{
8+
public override LessonPlan? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
9+
{
10+
var lessonData = JsonSerializer.Deserialize<LessonData[]>(ref reader, options)!;
11+
12+
return new LessonPlan(lessonData);
13+
}
14+
15+
public override void Write(Utf8JsonWriter writer, LessonPlan value, JsonSerializerOptions options)
16+
{
17+
throw new NotImplementedException();
18+
}
19+
}

LearnJsonEverything/Shared/Feature.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</h2>
77
<p class="lead mb-4">@Description</p>
88
<div class="hstack justify-content-center">
9-
<a class="mx-3" href="@PlaygroundUrl">
9+
<a class="mx-3" href="@LessonsUrl">
1010
<span class="btn btn-primary mt-2">Playground</span>
1111
</a>
1212
<a class="mx-3" href="@DocsUrl">
@@ -30,7 +30,7 @@
3030
[Parameter]
3131
public string ImageUrl { get; set; }
3232
[Parameter]
33-
public string PlaygroundUrl { get; set; }
33+
public string LessonsUrl { get; set; }
3434
[Parameter]
3535
public string DocsUrl { get; set; }
3636
[Parameter]
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
@using BlazorMonaco.Editor
2+
@using Json.Schema
3+
@using LearnJsonEverything.Services
4+
@using Microsoft.CodeAnalysis
5+
@using Microsoft.CodeAnalysis.CSharp
6+
@using System.Reflection
7+
@using System.Text.Json
8+
9+
@inject DataManager DataManager;
10+
@inject NavigationManager NavigationManager;
11+
@inject IJSRuntime JsRuntime;
12+
@inject HttpClient Client;
13+
14+
<div id="layout" class="row fill-remaining row-margin-reset">
15+
<div class="col-4 d-flex">
16+
<div class="grid-panel flex-grow-1">
17+
content
18+
</div>
19+
</div>
20+
<div class="col-8 d-flex">
21+
<div id="right-panel" class="row row-margin-reset flex-grow-1">
22+
<div id="workspace" class="col-12 d-flex flex-grow-1">
23+
<div class="grid-panel flex-grow-1">
24+
content
25+
</div>
26+
</div>
27+
<div id="controls" class="col-12">
28+
<div class="d-flex">
29+
<div class="btn btn-primary m-1">
30+
content
31+
</div>
32+
<div class="btn btn-primary m-1">
33+
content
34+
</div>
35+
<div class="btn btn-primary m-1">
36+
content
37+
</div>
38+
</div>
39+
</div>
40+
<div id="output" class="col-12 d-flex">
41+
<div class="grid-panel flex-grow-1">
42+
content
43+
</div>
44+
</div>
45+
</div>
46+
</div>
47+
</div>
48+
49+
@code {
50+
private StandaloneCodeEditor _codeEditor;
51+
private StandaloneCodeEditor _outputEditor;
52+
private List<MetadataReference> _references;
53+
private LessonPlan _lessons;
54+
private LessonData? _currentLesson;
55+
56+
[Parameter]
57+
public string LessonSource { get; set; }
58+
59+
private string Output { get; set; }
60+
private bool Success { get; set; }
61+
62+
private async Task TryRun()
63+
{
64+
Assembly? assembly = null;
65+
try
66+
{
67+
var source = await _codeEditor.GetValue();
68+
69+
var fullSource = $@"
70+
using System;
71+
using System.Collections.Generic;
72+
using System.Text.Json;
73+
using System.Text.Json.Nodes;
74+
using System.Text.Json.Serialization;
75+
using Json.Schema;
76+
using Json.Schema.Generation;
77+
78+
namespace JsonEverythingTemp;
79+
80+
{source}
81+
";
82+
83+
var syntaxTree = CSharpSyntaxTree.ParseText(fullSource);
84+
var assemblyPath = Path.ChangeExtension(Path.GetTempFileName(), "dll");
85+
86+
var compilation = CSharpCompilation.Create(Path.GetFileName(assemblyPath))
87+
.WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
88+
.AddReferences(_references)
89+
.AddSyntaxTrees(syntaxTree);
90+
91+
using var dllStream = new MemoryStream();
92+
using var pdbStream = new MemoryStream();
93+
using var xmlStream = new MemoryStream();
94+
var emitResult = compilation.Emit(dllStream, pdbStream, xmlStream);
95+
if (!emitResult.Success)
96+
{
97+
Console.WriteLine("You may expect a list of what compilation errors there are, but unfortunately " +
98+
"Roslyn doesn't seem to be giving that information out (or I don't know how to " +
99+
"interpret it). So instead, here are the errors in raw form. Good luck. If you " +
100+
"know what these mean, please drop a line in a GitHub issue.");
101+
await _outputEditor.SetValue("Compilation error");
102+
//var errors = string.Join("\n", emitResult.Diagnostics.Where(x => x.Severity == DiagnosticSeverity.Error)
103+
// .Select(x => GetErrorDetails(source, x)));
104+
//await _outputEditor.SetValue(errors);
105+
return;
106+
}
107+
108+
assembly = Assembly.Load(dllStream.ToArray());
109+
110+
// run the code
111+
}
112+
catch (Exception e)
113+
{
114+
await _outputEditor.SetLanguageAsync("text", JsRuntime);
115+
await _outputEditor.SetValue(e.Message);
116+
}
117+
}
118+
119+
private Task NextLesson()
120+
{
121+
_currentLesson = _lessons.GetNext(_currentLesson?.Id);
122+
return LoadLesson();
123+
}
124+
125+
private Task PreviousLesson()
126+
{
127+
_currentLesson = _lessons.GetNext(_currentLesson?.Id);
128+
return LoadLesson();
129+
}
130+
131+
private Task Reset()
132+
{
133+
return LoadLesson();
134+
}
135+
136+
private async Task LoadLesson()
137+
{
138+
139+
}
140+
141+
protected override async Task OnInitializedAsync()
142+
{
143+
// await Configure();
144+
// await LoadAssemblyReferences();
145+
await DownloadLessonPlan();
146+
147+
await LoadLesson();
148+
149+
await base.OnInitializedAsync();
150+
}
151+
152+
private async Task Configure()
153+
{
154+
await _codeEditor.SetLanguageAsync("csharp", JsRuntime);
155+
await _codeEditor.UpdateOptions(new EditorUpdateOptions { TabSize = 4 });
156+
await DataManager.Set("schema-input-language", "csharp");
157+
}
158+
159+
private async Task LoadAssemblyReferences()
160+
{
161+
var refs = AppDomain.CurrentDomain.GetAssemblies();
162+
163+
var references = new List<MetadataReference>();
164+
165+
foreach (var reference in refs.Where(x => !x.IsDynamic))
166+
{
167+
Console.WriteLine($"{reference.FullName}\n Location - {reference.Location}\n");
168+
}
169+
170+
foreach (var reference in refs.Where(x => !x.IsDynamic))
171+
{
172+
var stream = await Client.GetStreamAsync($"_framework/{reference.FullName!.Split(',')[0]}.dll");
173+
references.Add(MetadataReference.CreateFromStream(stream));
174+
}
175+
176+
_references = references;
177+
}
178+
179+
private async Task DownloadLessonPlan()
180+
{
181+
_lessons = await Client.GetFromJsonAsync<LessonPlan>(LessonSource);
182+
}
183+
}

0 commit comments

Comments
 (0)