Skip to content

Commit

Permalink
Merge pull request #923 from WildernessLabs/complex_json
Browse files Browse the repository at this point in the history
Add complex MicroJson sample
  • Loading branch information
adrianstevens authored Mar 14, 2024
2 parents cb4d1cd + 36664ee commit f2a74f3
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
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>
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();
}
}
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; }
}
}
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
}
7 changes: 7 additions & 0 deletions Source/Meadow.Foundation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MicroJson_Sample", "Meadow.
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MicroJson_ArrayList_Sample", "Meadow.Foundation.Libraries_and_Frameworks\Serialization.MicroJson\Samples\MicroJson_ArrayList_Sample\MicroJson_ArrayList_Sample.csproj", "{9F5DD1C6-4A3E-4170-BE18-3FAC3A1E0D48}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MicroJson_Complex_Sample", "Meadow.Foundation.Libraries_and_Frameworks\Serialization.MicroJson\Samples\MicroJson_Complex_Sample\MicroJson_Complex_Sample.csproj", "{6FD1D41A-1A80-4E04-B7FF-5EDCE6A526AC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -3567,6 +3569,10 @@ Global
{9F5DD1C6-4A3E-4170-BE18-3FAC3A1E0D48}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9F5DD1C6-4A3E-4170-BE18-3FAC3A1E0D48}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9F5DD1C6-4A3E-4170-BE18-3FAC3A1E0D48}.Release|Any CPU.Build.0 = Release|Any CPU
{6FD1D41A-1A80-4E04-B7FF-5EDCE6A526AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6FD1D41A-1A80-4E04-B7FF-5EDCE6A526AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6FD1D41A-1A80-4E04-B7FF-5EDCE6A526AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6FD1D41A-1A80-4E04-B7FF-5EDCE6A526AC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -4305,6 +4311,7 @@ Global
{CA32D6C5-DD19-4787-8EE1-B92415FEF903} = {B9C2605C-AC98-4BF1-8E3F-8F0F23A694C9}
{5F905795-94F9-43F3-A3F8-AD667C55A730} = {CA32D6C5-DD19-4787-8EE1-B92415FEF903}
{9F5DD1C6-4A3E-4170-BE18-3FAC3A1E0D48} = {CA32D6C5-DD19-4787-8EE1-B92415FEF903}
{6FD1D41A-1A80-4E04-B7FF-5EDCE6A526AC} = {CA32D6C5-DD19-4787-8EE1-B92415FEF903}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AF7CA16F-8C38-4546-87A2-5DAAF58A1520}
Expand Down

0 comments on commit f2a74f3

Please sign in to comment.