-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #923 from WildernessLabs/complex_json
Add complex MicroJson sample
- Loading branch information
Showing
5 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
.../Serialization.MicroJson/Samples/MicroJson_Complex_Sample/MicroJson_Complex_Sample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Remove="weather.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="weather.json" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Driver\MicroJson.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
33 changes: 33 additions & 0 deletions
33
...raries_and_Frameworks/Serialization.MicroJson/Samples/MicroJson_Complex_Sample/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Meadow.Foundation.Serialization; | ||
using System; | ||
using System.IO; | ||
using System.Reflection; | ||
using WifiWeather.DTOs; | ||
|
||
namespace MicroJson_Complex_Sample; | ||
|
||
internal class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello, MicroJson - Complex Json"); | ||
|
||
var jsonData = LoadResource("weather.json"); | ||
|
||
var weather = MicroJson.Deserialize<WeatherReadingDTO>(jsonData); | ||
|
||
Console.WriteLine($"Temperature is: {weather.main.temp - 273.15:N1}C"); | ||
} | ||
|
||
static byte[] LoadResource(string filename) | ||
{ | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
var resourceName = $"MicroJson_Complex_Sample.{filename}"; | ||
|
||
using Stream stream = assembly.GetManifestResourceStream(resourceName); | ||
using var ms = new MemoryStream(); | ||
|
||
stream?.CopyTo(ms); | ||
return ms.ToArray(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
..._Frameworks/Serialization.MicroJson/Samples/MicroJson_Complex_Sample/WeatherReadingDTO.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
namespace WifiWeather.DTOs | ||
{ | ||
public class WeatherReadingDTO | ||
{ | ||
public Coordinates coord { get; set; } | ||
public Weather[] weather { get; set; } | ||
public WeatherValues main { get; set; } | ||
public int visibility { get; set; } | ||
public Wind wind { get; set; } | ||
public Clouds clouds { get; set; } | ||
public int dt { get; set; } | ||
public System sys { get; set; } | ||
public long timezone { get; set; } | ||
public int id { get; set; } | ||
public string name { get; set; } | ||
public int cod { get; set; } | ||
} | ||
|
||
public class Coordinates | ||
{ | ||
public double lon { get; set; } | ||
public double lat { get; set; } | ||
} | ||
|
||
public class Weather | ||
{ | ||
public int id { get; set; } | ||
public string nain { get; set; } | ||
public string description { get; set; } | ||
public string icon { get; set; } | ||
} | ||
|
||
public class WeatherValues | ||
{ | ||
public double temp { get; set; } | ||
public double feels_like { get; set; } | ||
public double temp_min { get; set; } | ||
public double temp_max { get; set; } | ||
public int pressure { get; set; } | ||
public int humidity { get; set; } | ||
} | ||
|
||
public class Wind | ||
{ | ||
public decimal speed { get; set; } | ||
public int deg { get; set; } | ||
public double gust { get; set; } | ||
} | ||
|
||
public class Clouds | ||
{ | ||
public int all { get; set; } | ||
} | ||
|
||
public class System | ||
{ | ||
public int Type { get; set; } | ||
public int Id { get; set; } | ||
public string country { get; set; } | ||
public long sunrise { get; set; } | ||
public long sunset { get; set; } | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...ries_and_Frameworks/Serialization.MicroJson/Samples/MicroJson_Complex_Sample/weather.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
{ | ||
"coord": { | ||
"lon": -123.1193, | ||
"lat": 49.2497 | ||
}, | ||
"weather": [ | ||
{ | ||
"id": 803, | ||
"main": "Clouds", | ||
"description": "broken clouds", | ||
"icon": "04d" | ||
} | ||
], | ||
"base": "stations", | ||
"main": { | ||
"temp": 279.01, | ||
"feels_like": 276, | ||
"temp_min": 277.58, | ||
"temp_max": 279.94, | ||
"pressure": 1028, | ||
"humidity": 84 | ||
}, | ||
"visibility": 10000, | ||
"wind": { | ||
"speed": 4.12, | ||
"deg": 90 | ||
}, | ||
"clouds": { | ||
"all": 75 | ||
}, | ||
"dt": 1710347018, | ||
"sys": { | ||
"type": 2, | ||
"id": 2011597, | ||
"country": "CA", | ||
"sunrise": 1710340135, | ||
"sunset": 1710382486 | ||
}, | ||
"timezone": -25200, | ||
"id": 6173331, | ||
"name": "Vancouver", | ||
"cod": 200 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters