AzureZeng.MSBuildTasks.Json2Resources
1.0.1
dotnet add package AzureZeng.MSBuildTasks.Json2Resources --version 1.0.1
NuGet\Install-Package AzureZeng.MSBuildTasks.Json2Resources -Version 1.0.1
<PackageReference Include="AzureZeng.MSBuildTasks.Json2Resources" Version="1.0.1" />
<PackageVersion Include="AzureZeng.MSBuildTasks.Json2Resources" Version="1.0.1" />
<PackageReference Include="AzureZeng.MSBuildTasks.Json2Resources" />
paket add AzureZeng.MSBuildTasks.Json2Resources --version 1.0.1
#r "nuget: AzureZeng.MSBuildTasks.Json2Resources, 1.0.1"
#:package AzureZeng.MSBuildTasks.Json2Resources@1.0.1
#addin nuget:?package=AzureZeng.MSBuildTasks.Json2Resources&version=1.0.1
#tool nuget:?package=AzureZeng.MSBuildTasks.Json2Resources&version=1.0.1
Json2Resource
This NuGet package converts JSON resource file(s) to standard .NET resource (.resources) files and embeds
them into the target assembly automatically during build.
Currently, this package is enough for normal usage. Maybe more features are needed to be implemented in the future.
WARNING: This package may have some breaking changes in the future.
Usage
After installing this package, add JSON resource file(s) as JsonResource item type:
<ItemGroup>
<JsonResource Include="Test.json"/>
</ItemGroup>
The JSON manifest is converted to a .resources file and embedded into the assembly automatically.
The intermediate .resources files are generated under the IntermediateOutputPath and are removed when the
project is cleaned.
How it works
JsonResourcePropEvalevaluates eachJsonResourceitem and computes itsLogicalName(RootNamespace+ directory-as-namespace + file name, with a.resourcessuffix) andIntermediatePath(obj/{LogicalName}.resources). It also scans every manifest for external files referenced through theFilePrefixkeys.CompileJsonResourcesconverts each manifest into a.resourcesfile. See JSON_RES_FORMAT.md for the manifest format.CompileAndAddJsonResourcesadds the generated files asEmbeddedResourceitems, so they are compiled into the assembly.
Supported properties
| Property Name | Description | Accepted Values | Default Value |
|---|---|---|---|
| FilePrefix | The prefix of resource keys whose value is loaded from an external file | Any string | $file: |
| LogicalName | The logical resource name in the assembly | Any | Auto-determined by task |
| ContextPath | Base directory for resolving relative external file paths in the manifest | FileDir, or any directory |
Project directory |
Notes:
LogicalNamedefaults toRootNamespace+ the manifest's relative path (directories become namespace segments, e.g.Data/Test.jsonunder root namespaceMyAppyieldsMyApp.Data.Test). SettingLogicalNameexplicitly overrides the whole name (the.resourcessuffix is appended automatically).ContextPathis used only when resolving relative paths in external-file ($file:) values. If it is set toFileDir, the base directory becomes the directory that contains the JSON manifest file; otherwise it is interpreted as a directory, defaulting to the project directory.
Reading the resources at runtime
The resources are embedded with their logical names and can be loaded with ResourceManager:
var rm = new ResourceManager("MyApp.Data.Test", typeof(SomeTypeInTheAssembly).Assembly);
string text = rm.GetString("SubKeys.Key1"); // nested keys are flattened with dots
byte[] binary = (byte[])rm.GetObject("Image"); // external file, binary mode (prefix is stripped)
Stream stream = rm.GetStream("Log"); // external file, stream mode (prefix is stripped)
Incremental builds
The CompileJsonResources target tracks both the JSON resource files and the external files referenced by
FilePrefix keys as incremental-build inputs. Changes to either trigger regeneration, so the embedded
resources never go stale.
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.