Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TextDisplayMenu to use MicroJson #933

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

using System.Text.Json.Serialization;

namespace Meadow.Foundation.Displays.UI
{
/// <summary>
Expand All @@ -11,37 +9,31 @@ public class MenuItem
/// <summary>
/// Sub items in the menu
/// </summary>
[JsonPropertyName("sub")]
public MenuItem[]? SubItems { get; set; }

/// <summary>
/// The text on the menu item
/// </summary>
[JsonPropertyName("text")]
public string Text { get; set; }

/// <summary>
/// The optional command when the item is selected
/// </summary>
[JsonPropertyName("command")]
public string Command { get; set; }

/// <summary>
/// The menu item type
/// </summary>
[JsonPropertyName("type")]
public string Type { get; set; }

/// <summary>
/// The menu item id
/// </summary>
[JsonPropertyName("id")]
public string Id { get; set; }

/// <summary>
/// The menu item value
/// </summary>
[JsonPropertyName("value")]
public object? Value { get; set; }

/// <summary>
Expand All @@ -54,6 +46,19 @@ public class MenuItem
/// </summary>
public bool IsEditable => Value != null;

/// <summary>
/// Creates a new MenuItem object
/// </summary>
public MenuItem()
{
Text = string.Empty;
Command = string.Empty;
Id = string.Empty;
Type = string.Empty;
Value = null;
SubItems = null;
}

/// <summary>
/// Creates a new MenuItem object
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<ItemGroup>
<None Include=".\Readme.md" Pack="true" PackagePath="" />
<None Include="..\..\..\icon.png" Pack="true" PackagePath="" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
<ProjectReference Include="..\..\Graphics.MicroGraphics\Driver\Graphics.MicroGraphics.csproj" />
<ProjectReference Include="..\..\Serialization.MicroJson\Driver\Serialization.MicroJson.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Meadow.Foundation.Displays.UI.InputTypes;
using Meadow.Foundation.Serialization;
using Meadow.Peripherals.Displays;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text.Json;

namespace Meadow.Foundation.Displays.UI
{
Expand Down Expand Up @@ -72,7 +72,7 @@ public TextDisplayMenu(ITextDisplay display, MenuItem[] menuItems, bool showBack
{
var menuString = System.Text.Encoding.Default.GetString(menuJson);

return JsonSerializer.Deserialize<MenuItem[]>(menuString);
return MicroJson.Deserialize<MenuItem[]>(menuString);
}

void Init(ITextDisplay display, MenuPage menuPage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<ProjectReference Include="..\..\..\..\..\..\Meadow.Core\Source\implementations\f7\Meadow.F7\Meadow.F7.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="app.build.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="meadow.config.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deploy:
NoLink: [ TextDisplayMenu ]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<Company>Wilderness Labs, Inc</Company>
Expand All @@ -20,6 +20,9 @@
<ProjectReference Include="..\..\..\..\Meadow.Foundation.Peripherals\Displays.Ssd130x\Driver\Displays.Ssd130x.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="app.build.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="meadow.config.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deploy:
NoLink: [ TextDisplayMenu ]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<Company>Wilderness Labs, Inc</Company>
Expand All @@ -20,6 +20,9 @@
<ProjectReference Include="..\..\..\..\Meadow.Foundation.Peripherals\Displays.Ssd130x\Driver\Displays.Ssd130x.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="app.build.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="meadow.config.yaml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deploy:
NoLink: [ TextDisplayMenu ]
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
{
var table = DeserializeString(json) as Hashtable;

return DeserializeHashtableToDictionary(table, type)

Check warning on line 142 in Source/Meadow.Foundation.Libraries_and_Frameworks/Serialization.MicroJson/Driver/MicroJson.TypeSafe.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'hashtable' in 'IDictionary? MicroJson.DeserializeHashtableToDictionary(Hashtable hashtable, Type dictionaryType)'.
?? throw new NotSupportedException($"Type '{type.Name}' not supported");
}
throw new NotSupportedException($"Type '{type.Name}' not supported");
Expand Down Expand Up @@ -278,6 +278,10 @@
var dto = DateTimeOffset.Parse(values[v].ToString());
prop.SetValue(instance, dto);
}
else if (propType == typeof(object))
{
prop.SetValue(instance, DeserializeDynamic(values[v]));
}
else
{
throw new NotSupportedException($"Unable to deserialize type '{propType}'");
Expand Down Expand Up @@ -314,4 +318,16 @@

return true;
}

private static object? DeserializeDynamic(object jsonValue)
{
return jsonValue switch
{
string stringValue => stringValue,
double doubleValue => doubleValue,
long longValue => longValue,
bool boolValue => boolValue,
_ => jsonValue,// Directly return the value if it doesn't need special handling
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Driver\MicroJson.csproj" />
<ProjectReference Include="..\..\Driver\Serialization.MicroJson.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Driver\MicroJson.csproj" />
<ProjectReference Include="..\..\Driver\Serialization.MicroJson.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<EmbeddedResource Include="menu.json" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Driver\MicroJson.csproj" />
<ProjectReference Include="..\..\Driver\Serialization.MicroJson.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\..\..\..\Meadow.Contracts\Source\Meadow.Contracts\Meadow.Contracts.csproj" />
<ProjectReference Include="..\..\Driver\MicroJson.csproj" />
<ProjectReference Include="..\..\Driver\Serialization.MicroJson.csproj" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions Source/Meadow.Foundation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1445,9 +1445,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scd30", "Scd30", "{BAACC3E2
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{A3EF6DB1-971C-4891-853A-369652E36959}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sensors.Environmental.Scd30", "Meadow.Foundation.Peripherals\Sensors.Environmental.Scd30\Driver\Sensors.Environmental.Scd30.csproj", "{EACC5CD9-C3EB-4BB3-A137-C0F950592519}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sensors.Environmental.Scd30", "Meadow.Foundation.Peripherals\Sensors.Environmental.Scd30\Driver\Sensors.Environmental.Scd30.csproj", "{EACC5CD9-C3EB-4BB3-A137-C0F950592519}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scd30_Sample", "Meadow.Foundation.Peripherals\Sensors.Environmental.Scd30\Samples\Scd30_Sample\Scd30_Sample.csproj", "{43B00BB5-2F6A-437A-A969-B4ADF2E9F439}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Scd30_Sample", "Meadow.Foundation.Peripherals\Sensors.Environmental.Scd30\Samples\Scd30_Sample\Scd30_Sample.csproj", "{43B00BB5-2F6A-437A-A969-B4ADF2E9F439}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Pct2075", "Pct2075", "{7471C6BF-1995-4E56-91E9-86DAAA46C386}"
EndProject
Expand All @@ -1473,7 +1473,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xpt2046_Sample", "Meadow.Fo
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Serialization.MicroJson", "Serialization.MicroJson", "{B9C2605C-AC98-4BF1-8E3F-8F0F23A694C9}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MicroJson", "Meadow.Foundation.Libraries_and_Frameworks\Serialization.MicroJson\Driver\MicroJson.csproj", "{D8E5BFC6-EE60-4E2D-BDF9-A4FC349A5F44}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serialization.MicroJson", "Meadow.Foundation.Libraries_and_Frameworks\Serialization.MicroJson\Driver\Serialization.MicroJson.csproj", "{D8E5BFC6-EE60-4E2D-BDF9-A4FC349A5F44}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Driver", "Driver", "{9543AB72-69FA-4C42-90F2-56EF19C3D57A}"
EndProject
Expand Down
Loading