Skip to content

Commit

Permalink
Add initial sample for writable JSON DOM
Browse files Browse the repository at this point in the history
  • Loading branch information
dodyg committed Jun 21, 2021
1 parent 20992e6 commit 1da2bfc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
@@ -1,4 +1,4 @@
# Samples for ASP.NET Core 5.0 (357)
# Samples for ASP.NET Core 5.0 (358)

While I got your attention, Palestine needs helps. Support [Palestine Children's Relief Fund](https://www.charitynavigator.org/index.cfm?bay=search.summary&orgid=6421).

Expand All @@ -10,7 +10,7 @@ I highly recommend using [Visual Studio Code](https://code.visualstudio.com/) to

[3.1 LTS](https://github.com/dodyg/practical-aspnetcore/tree/3.1-LTS/), [2.1 LTS](https://github.com/dodyg/practical-aspnetcore/tree/2.1-LTS)

## .NET 6 Preview 5 Section (18)
## .NET 6 Preview 5 Section (19)

You can access .NET 6 Preview 5 specific samples [here](projects/net6).

Expand Down
10 changes: 8 additions & 2 deletions projects/net6/README.md
@@ -1,4 +1,4 @@
# .NET 6 (18)
# .NET 6 (19)

This section has for .NET 6 new functionalities. All these samples require .NET 6 Preview 5(`6.0.100-preview.5.21302.13`) which you can obtain [here](https://dotnet.microsoft.com/download/dotnet/6.0).

Expand Down Expand Up @@ -80,4 +80,10 @@ In most cases using ```WebApplication``` isn't enough because you need to config
This section showcases creating reverse proxy using YARP package.
* [Basic Demo](yarp/basic-demo)

Basic demo with simple YARP routing.
Basic demo with simple YARP routing.

## Writable JSON DOM

* [Primitives](json/json-12)

This sample shows how to parse and access number, string and an array values from JSON string.
3 changes: 3 additions & 0 deletions projects/net6/json/json-12/README.md
@@ -0,0 +1,3 @@
# Writable DOM

This sample shows how to parse and access number, string and an array values from JSON string.
6 changes: 6 additions & 0 deletions projects/net6/json/json-12/json-12.csproj
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
39 changes: 39 additions & 0 deletions projects/net6/json/json-12/src/Program.cs
@@ -0,0 +1,39 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using System.Text.Json;
using System.Text.Json.Nodes;

WebApplication app = WebApplication.Create();

app.Run(async context =>
{
var numberNode = JsonNode.Parse(@"{""age"" : 34 }");
int age = (int) numberNode["age"];
await context.Response.WriteAsync($"{numberNode} \n");
await context.Response.WriteAsync($"Value of age is {age}\n\n\n");
var stringNode = JsonNode.Parse(@"{""name"" : ""anne""}");
string name = (string) stringNode["name"];
await context.Response.WriteAsync($"{stringNode} \n");
await context.Response.WriteAsync($"Value of name is {name}\n\n\n");
var numberArrayNode = JsonNode.Parse("[2, 4, 6]");
await context.Response.WriteAsync($"{numberArrayNode}\n");
int item0 = (int) numberArrayNode[0];
int item1 = (int) numberArrayNode[1];
int item2 = (int) numberArrayNode[2];
await context.Response.WriteAsync($"Values of array: {item0}, {item1}, {item2}\n\n");
//alternatively
JsonArray childrenAge = numberArrayNode.AsArray();
await context.Response.WriteAsync($"Size of array: {childrenAge.Count}\nValues of array: ");
foreach(JsonNode r in childrenAge)
{
await context.Response.WriteAsync($"{r}, ");
}
});

await app.RunAsync();

0 comments on commit 1da2bfc

Please sign in to comment.