Skip to content

Commit

Permalink
Added Microsoft.Maui.Extensions project and the JsonUriStringConverte…
Browse files Browse the repository at this point in the history
…r & JsonLocaleConverter JsonConverter classes and the UriOrString class.
  • Loading branch information
dgmjr committed Dec 27, 2023
1 parent b401c71 commit 7156781
Show file tree
Hide file tree
Showing 18 changed files with 390 additions and 22 deletions.
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"dotnet-test-explorer.testProjectPath": "**/*Tests.@(csproj|vbproj|fsproj)"
}
"dotnet-test-explorer.testProjectPath": "**/*Tests.@(csproj|vbproj|fsproj)",
"sonarlint.connectedMode.project": {
"connectionId": "dgmjr-io",
"projectKey": "dgmjr-io_Dgmjr.System.Extensions"
}
}
21 changes: 21 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
* Directory.Build.props
*
* Created: 2023-01-11-05:08:45
* Modified: 2023-01-11-11:48:02
*
* Author: David G. Moore, Jr. <[email protected]>
*
* Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved
* License: MIT (https://opensource.org/licenses/MIT)
-->

<Project>
<Import Project="Sdk.props" Sdk="DgmjrSdk" Condition="'$(DgmjrSdkProps)' == '' And '$(IsInnerBuild)' != 'true'" />
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
<DgmjrSdkProps>$(MSBuildThisFileDirectory)Sdk.props</DgmjrSdkProps>
<!--<InheritedDirectoryBuildProps>$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))</InheritedDirectoryBuildProps>-->
</PropertyGroup>
<!--<Import Project="$(InheritedDirectoryBuildProps)" Condition="Exists('$(InheritedDirectoryBuildProps)')" />-->
</Project>
20 changes: 20 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--
* Directory.Build.targets
*
* Created: 2023-01-11-05:08:45
* Modified: 2023-01-11-11:47:51
*
* Author: David G. Moore, Jr. <[email protected]>
*
* Copyright © 2022-2023 David G. Moore, Jr., All Rights Reserved
* License: MIT (https://opensource.org/licenses/MIT)
-->

<Project>
<Import Project="Sdk.targets" Sdk="DgmjrSdk" Condition="'$(DgmjrSdkTargets)' == '' And '$(IsInnerBuild)' != 'true'" />
<PropertyGroup>
<DgmjrSdkTargets>$(MSBuildThisFileDirectory)Sdk.targets</DgmjrSdkTargets>
<!--<InheritedDirectoryBuildTargets>$([MSBuild]::GetPathOfFileAbove('Directory.Build.targets', '$(MSBuildThisFileDirectory)../'))</InheritedDirectoryBuildTargets>-->
</PropertyGroup>
<!--<Import Project="$(InheritedDirectoryBuildTargets)" Condition="Exists('$(InheritedDirectoryBuildTargets)')" />-->
</Project>
8 changes: 8 additions & 0 deletions maui/Dgmjr.Microsoft.Maui.Extensions.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Extension methods for Microsoft MAUI framework</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Maui.Graphics" />
</ItemGroup>
</Project>
42 changes: 42 additions & 0 deletions maui/Dgmjr.Microsoft.Maui.Extensions.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Microsoft Visual Studio Solution File, Format Version 12.00
#
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B283EBC2-E01F-412D-9339-FD56EF114549}"
ProjectSection(SolutionItems) = preProject
..\Directory.Build.props = ..\Directory.Build.props
..\Directory.Build.targets = ..\Directory.Build.targets
..\..\..\..\global.json = ..\..\..\..\global.json
..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\Packages\Versions.Local.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.Microsoft.Maui.Extensions", "Dgmjr.Microsoft.Maui.Extensions.csproj", "{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Local|Any CPU = Local|Any CPU
Debug|Any CPU = Debug|Any CPU
Testing|Any CPU = Testing|Any CPU
Staging|Any CPU = Staging|Any CPU
Production|Any CPU = Production|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Local|Any CPU.ActiveCfg = Local|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Local|Any CPU.Build.0 = Local|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Testing|Any CPU.Build.0 = Testing|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Staging|Any CPU.ActiveCfg = Staging|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Staging|Any CPU.Build.0 = Staging|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Production|Any CPU.ActiveCfg = Local|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Production|Any CPU.Build.0 = Local|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D2AEFA3C-FCDD-4126-98C0-BB0BD5D9D071}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3C15FE54-9A1C-43C9-8C8D-7AB7FA71BD87}
EndGlobalSection
EndGlobal
36 changes: 36 additions & 0 deletions maui/IImageExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Microsoft.Maui.Graphics;

using Microsoft.Maui.Graphics.Platform;
using System.Reflection;

public static class IImageExtensions
{
public static Size GetDimensions(this IImage image, int maxDimension = int.MaxValue)
{
var width = image.Width;
var height = image.Height;
if (width > maxDimension || height > maxDimension)
{
var ratio = width / (double)height;
if (ratio > 1)
{
width = maxDimension;
height = (int)(width / ratio);
}
else
{
height = maxDimension;
width = (int)(height * ratio);
}
}
return new Size(width, height);
}

public static IImage ToImage(this Stream stream) => PlatformImage.FromStream(stream);

public static Uri ToDataUri(this IImage image, string mimeType = "image/png")
{
var bytes = image.AsBytes();
return new Uri($"data:{mimeType};base64,{Convert.ToBase64String(bytes)}");
}
}
35 changes: 35 additions & 0 deletions maui/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
date: 2023-07-13T05:44:46:00.048Z
description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda...
keywords:
- IP
- copyright
- license
- mit
permissions:
- commercial-use
- modifications
- distribution
- private-use
conditions:
- include-copyright
limitations:
- liability
- warranty
lastmod: 2023-08-29T17:13:51:00.216Z
license: MIT
slug: mit-license
title: MIT License
type: license
---

# MIT License

## Copyright © 2022-2023 [David G. Moore, Jr.](mailto:[email protected] "Send David an email") ([@dgmjr](https://github.com/dgmjr "Contact david on GitHub")), All Rights Reserved

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

21 changes: 21 additions & 0 deletions maui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Microsoft MAUI Extensions
lastmod: 2023-23-22T18:23:41.1955-05:00Z
date: 2023-23-22T18:23:41.1956-05:00Z
license: MIT
slug: Dgmjr.Microsoft.Maui.Extensions-readme
version:
authors:
- dgmjr;
description: Extension methods for Microsoft MAUI framework
keywords:
- Dgmjr.Microsoft.Maui.Extensions
- dgmjr
- dgmjr-io
type: readme
---

# Microsoft MAUI Extensions

This package contains extension methods for the Microsoft MAUI framework.

2 changes: 2 additions & 0 deletions src/Dgmjr.System.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Memory" />
<PackageReference Include="System.Text.Json" Contition="$(DefineConstants.Contains('NETSTANDARD'))" />
<PackageReference Include="OneOf" />
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions src/Dgmjr.System.Extensions.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
<Using Include="System.Collections.Generic.CaseInsensitiveKeyDictionary%3cstring%3e" Alias="CaseInsensitiveStringKeyDictionary" />
<Using Include="System.Int128" Alias="vlong /* very long */" Condition="$(DefineConstants.Contains('NET7_0_OR_GREATER'))" />
<Using Include="System.UInt128" Alias="uvlong /* unsigned very long */" Condition="$(DefineConstants.Contains('NET7_0_OR_GREATER'))" />
<Using Include="System.Text.Json.JsonTokenType" Alias="JTokenType" />
<Using Include="System.Text.Json.Serialization.JsonConverterAttribute" Alias="JConverterAttribute" />
<Using Include="System.Text.Json.Serialization.JsonConverter" Alias="JConverter" />
<Using Include="System.Text.Json.JsonSerializerOptions" Alias="Jso" />
<Using Include="System.Text.Json.Serialization" />
<Using Include="System.Text.Json" />
<Using Include="System.Diagnostics.CodeAnalysis.StringSyntaxAttribute" Alias="StringSyntax" Condition="$(DefineConstants.Contains('NET7_0_OR_GREATER'))" />
</ItemGroup>
<ItemGroup Condition="!$(TargetFramework.StartsWith('netstandard'))">
<PackageReference Include="System.Collections.Immutable"/>
Expand Down
30 changes: 15 additions & 15 deletions src/Dgmjr.System.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
#
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{B283EBC2-E01F-412D-9339-FD56EF114549}"
ProjectSection(SolutionItems) = preProject
..\..\..\..\Directory.Build.props = ..\..\..\..\Directory.Build.props
..\..\..\..\Directory.Build.targets = ..\..\..\..\Directory.Build.targets
..\Directory.Build.props = ..\Directory.Build.props
..\Directory.Build.targets = ..\Directory.Build.targets
..\..\..\..\global.json = ..\..\..\..\global.json
..\..\..\..\Packages\Versions.Local.props = ..\..\..\..\Packages\Versions.Local.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.System.Extensions", "Dgmjr.System.Extensions.csproj", "{270D98D8-CC4A-4CE1-813E-9DDD04A79430}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dgmjr.System.Extensions", "Dgmjr.System.Extensions.csproj", "{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -20,18 +20,18 @@ Global
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Local|Any CPU.ActiveCfg = Local|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Local|Any CPU.Build.0 = Local|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Debug|Any CPU.Build.0 = Debug|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Testing|Any CPU.Build.0 = Testing|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Staging|Any CPU.ActiveCfg = Staging|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Staging|Any CPU.Build.0 = Staging|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Production|Any CPU.ActiveCfg = Local|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Production|Any CPU.Build.0 = Local|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Release|Any CPU.ActiveCfg = Release|Any CPU
{270D98D8-CC4A-4CE1-813E-9DDD04A79430}.Release|Any CPU.Build.0 = Release|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Local|Any CPU.ActiveCfg = Local|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Local|Any CPU.Build.0 = Local|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Testing|Any CPU.ActiveCfg = Testing|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Testing|Any CPU.Build.0 = Testing|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Staging|Any CPU.ActiveCfg = Staging|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Staging|Any CPU.Build.0 = Staging|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Production|Any CPU.ActiveCfg = Local|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Production|Any CPU.Build.0 = Local|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CCFFCEFF-3582-4656-882E-FEE5A4568B3D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions src/LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
date: 2023-07-13T05:44:46.048Z
date: 2023-07-13T05:44:46:00.048Z
description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda...
keywords:
- IP
Expand All @@ -13,10 +13,10 @@ permissions:
- private-use
conditions:
- include-copyright
liimitations:
limitations:
- liability
- warranty
lastmod: 2023-08-29T17:13:51.216Z
lastmod: 2023-08-29T17:13:51:00.216Z
license: MIT
slug: mit-license
title: MIT License
Expand Down
41 changes: 41 additions & 0 deletions src/System.Globalization/JsonLocaleConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace System.Globalization;

using System.Text.Json.Serialization;
using System.Text.Json;
using Jso = System.Text.Json.JsonSerializerOptions;

public class JsonLocaleConverter
: System.Text.Json.Serialization.JsonConverter<System.Globalization.CultureInfo>
{
public override System.Globalization.CultureInfo Read(
ref Utf8JsonReader reader,
type typeToConvert,
Jso options
)
{
if (reader.TokenType == JsonTokenType.String)
{
try
{
return System.Globalization.CultureInfo.CreateSpecificCulture(reader.GetString());
}
catch
{
return System.Globalization.CultureInfo.InvariantCulture;
}
}
else
{
return System.Globalization.CultureInfo.InvariantCulture;
}
}

public override void Write(
Utf8JsonWriter writer,
System.Globalization.CultureInfo value,
Jso options
)
{
writer.WriteStringValue(value.Name);
}
}
24 changes: 22 additions & 2 deletions src/System.IO/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* License: MIT (https://opensource.org/licenses/MIT)
*/

using System.Threading.Tasks;

namespace System.IO;

using System.Threading.Tasks;

public static class StreamExtensions
{
/// <summary>
Expand All @@ -30,4 +30,24 @@ public static class StreamExtensions
/// <returns> the contents of the <see cref="Stream" /> as a <see langword="string" /> to the end</returns>
public static Task<string> ReadToEndAsync(this Stream s) =>
new StreamReader(s).ReadToEndAsync();

/// <summary>
/// Reads <inheritdoc cref="ReadAllBytes" path="/returns" />
/// </summary>
/// <param name="stream">The stream to read from</param>
/// <returns>all <see langword="byte" />s from the <paramref name="stream" /></returns>
public static byte[] ReadAllBytes(this Stream stream)
{
using var memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
return memoryStream.ToArray();
}

/// <summary>
/// Reads <inheritdoc cref="ReadAllBytesAsync" path="/returns" />
/// </summary>
/// <param name="stream">The stream to read from</param>
/// <returns>all <see langword="byte" />s from the <paramref name="stream" /></returns>
public static async Task<byte[]> ReadAllBytesAsync(this Stream stream) =>
await Task.Run(() => stream.ReadAllBytes());
}
22 changes: 22 additions & 0 deletions src/System/JsonIntegerToTimeSpanConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace System;

using System.Text.Json.Serialization;
using System.Text.Json;
using Jso = System.Text.Json.JsonSerializerOptions;

public class JsonIntegerToTimeSpanConverter : JsonConverter<duration>
{
public override duration Read(ref Utf8JsonReader reader, type typeToConvert, Jso options)
{
if (reader.TokenType != JTokenType.Number)
{
throw new JsonException();
}
return duration.FromSeconds(reader.GetInt64());
}

public override void Write(Utf8JsonWriter writer, duration value, Jso options)
{
writer.WriteNumberValue(value.TotalSeconds);
}
}
Loading

0 comments on commit 7156781

Please sign in to comment.