-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UtilPack.JSON.Configuration and resurrected old method. The new…
… project is from ResourcePooling repo originally.
- Loading branch information
Showing
6 changed files
with
299 additions
and
56 deletions.
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
Source/Code/UtilPack.JSON.Configuration/StringContents.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,157 @@ | ||
/* | ||
* Copyright 2019 Stanislav Muhametsin. All rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. | ||
* | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.FileProviders; | ||
using System; | ||
using System.Text; | ||
|
||
namespace UtilPack.JSON.Configuration | ||
{ | ||
internal sealed class StringContentFileProvider : IFileProvider | ||
{ | ||
public const String PATH = ":::non-existing:::"; | ||
|
||
private readonly FileInfo _fileInfo; | ||
|
||
|
||
public StringContentFileProvider( String stringContents ) | ||
: this( new FileInfo( stringContents ) ) | ||
{ | ||
|
||
} | ||
|
||
public StringContentFileProvider( Byte[] serializedContents ) | ||
: this( new FileInfo( serializedContents ) ) | ||
{ | ||
} | ||
|
||
private StringContentFileProvider( FileInfo info ) | ||
{ | ||
this._fileInfo = ArgumentValidator.ValidateNotNull( nameof( info ), info ); | ||
} | ||
|
||
|
||
|
||
public IDirectoryContents GetDirectoryContents( String subpath ) | ||
{ | ||
return NotFoundDirectoryContents.Singleton; | ||
} | ||
|
||
public IFileInfo GetFileInfo( String subpath ) | ||
{ | ||
return String.Equals( PATH, subpath, StringComparison.Ordinal ) ? | ||
this._fileInfo : | ||
null; | ||
} | ||
|
||
public Microsoft.Extensions.Primitives.IChangeToken Watch( String filter ) | ||
{ | ||
return ChangeToken.Instance; | ||
} | ||
|
||
private sealed class FileInfo : IFileInfo | ||
{ | ||
|
||
private static readonly Encoding TheEncoding = new UTF8Encoding( false, false ); | ||
|
||
private readonly Byte[] _contentsAsBytes; | ||
|
||
public FileInfo( String stringContents ) | ||
: this( TheEncoding.GetBytes( stringContents ) ) | ||
{ | ||
|
||
} | ||
|
||
public FileInfo( Byte[] serializedContents ) | ||
{ | ||
this._contentsAsBytes = serializedContents; | ||
} | ||
|
||
public Boolean Exists => true; | ||
|
||
public Int64 Length => this._contentsAsBytes.Length; | ||
|
||
public String PhysicalPath => PATH; | ||
|
||
public String Name => PATH; | ||
|
||
public DateTimeOffset LastModified => DateTimeOffset.MinValue; | ||
|
||
public Boolean IsDirectory => false; | ||
|
||
public System.IO.Stream CreateReadStream() | ||
{ | ||
return new System.IO.MemoryStream( this._contentsAsBytes, 0, this._contentsAsBytes.Length, false, false ); | ||
} | ||
} | ||
|
||
private sealed class ChangeToken : Microsoft.Extensions.Primitives.IChangeToken | ||
{ | ||
public static ChangeToken Instance = new ChangeToken(); | ||
|
||
private ChangeToken() | ||
{ | ||
|
||
} | ||
|
||
public Boolean HasChanged => false; | ||
|
||
public Boolean ActiveChangeCallbacks => true; | ||
|
||
public IDisposable RegisterChangeCallback( | ||
Action<Object> callback, | ||
Object state | ||
) | ||
{ | ||
return NoOpDisposable.Instance; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
/// <summary> | ||
/// This class contains extension methods for types defined in other assemblies. | ||
/// </summary> | ||
public static partial class JsonConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds the given string as JSON content for this <see cref="IConfigurationBuilder"/>. | ||
/// </summary> | ||
/// <param name="builder">This <see cref="IConfigurationBuilder"/>.</param> | ||
/// <param name="textualContents">The JSON contents, as <see cref="String"/>.</param> | ||
/// <returns>The <paramref name="builder"/>.</returns> | ||
public static IConfigurationBuilder AddJsonContents( this IConfigurationBuilder builder, String textualContents ) | ||
{ | ||
return ArgumentValidator.ValidateNotNullReference( builder ) | ||
.AddJsonFile( new StringContentFileProvider( textualContents ), StringContentFileProvider.PATH, false, false ); | ||
} | ||
|
||
/// <summary> | ||
/// Adds the given serialized string as JSON content for this <see cref="IConfigurationBuilder"/>. | ||
/// </summary> | ||
/// <param name="builder">This <see cref="IConfigurationBuilder"/>.</param> | ||
/// <param name="stringAsBytes">The JSON contents, as <see cref="Byte"/> array.</param> | ||
/// <returns>The <paramref name="builder"/>.</returns> | ||
public static IConfigurationBuilder AddJsonContents( this IConfigurationBuilder builder, Byte[] stringAsBytes ) | ||
{ | ||
return ArgumentValidator.ValidateNotNullReference( builder ) | ||
.AddJsonFile( new StringContentFileProvider( stringAsBytes ), StringContentFileProvider.PATH, false, false ); | ||
} | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
Source/Code/UtilPack.JSON.Configuration/UtilPack.JSON.Configuration.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,50 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../UtilPack/UtilPack.csproj" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<MicrosoftConfigurationVersion Condition=" '$(TargetFramework)' == 'net451' Or '$(TargetFramework)' == 'netstandard1.3' ">1.1.2</MicrosoftConfigurationVersion> | ||
<MicrosoftConfigurationVersion Condition=" '$(TargetFramework)' != 'net451' And '$(TargetFramework)' != 'netstandard1.3'">2.2.0</MicrosoftConfigurationVersion> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftConfigurationVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' "> | ||
<Reference Include="System.Runtime" /> | ||
<Reference Include="System.Threading.Tasks" /> | ||
</ItemGroup> | ||
|
||
|
||
<!-- Assembly information stuff --> | ||
<PropertyGroup> | ||
<VersionPrefix>1.0.0</VersionPrefix> | ||
<VersionSuffix></VersionSuffix> | ||
<AssemblyTitle>$(AssemblyName)</AssemblyTitle> | ||
<Description>Small library containing extension methods for Microsoft.Extensions.Configuiration.Json package, including methods to add JSON contents as string or byte array.</Description> | ||
</PropertyGroup> | ||
|
||
<!-- NuGet package stuff --> | ||
<PropertyGroup> | ||
<PackageId>$(AssemblyName)</PackageId> | ||
<PackageVersion>$(VersionPrefix)</PackageVersion> | ||
<PackageVersion Condition=" '$(VersionSuffix)' != ''">$(PackageVersion)-$(VersionSuffix)</PackageVersion> | ||
<PackageReleaseNotes>Initial release.</PackageReleaseNotes> | ||
<PackageTags>json utility microsoft configuration extensions json</PackageTags> | ||
<Title>UtilPack: Extensions for Microsoft.Extensions.Configuration.Json</Title> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(CIPropsFilePath)" Condition=" '$(CIPropsFilePath)' != '' and Exists('$(CIPropsFilePath)') " /> | ||
|
||
</Project> |
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
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
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
Oops, something went wrong.