Black.Beard.Jslt 1.0.111

There is a newer version of this package available.
See the version list below for details.
dotnet add package Black.Beard.Jslt --version 1.0.111
NuGet\Install-Package Black.Beard.Jslt -Version 1.0.111
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Black.Beard.Jslt" Version="1.0.111" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Black.Beard.Jslt --version 1.0.111
#r "nuget: Black.Beard.Jslt, 1.0.111"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Black.Beard.Jslt as a Cake Addin
#addin nuget:?package=Black.Beard.Jslt&version=1.0.111

// Install Black.Beard.Jslt as a Cake Tool
#tool nuget:?package=Black.Beard.Jslt&version=1.0.111

jslt (json Stylesheet Language Transformations)

Build status

Implementation of jslt language in DOTNET. Use a template for transform Json To another json. Consider the following template. note this template is a json that describe the structure of the target json. If the template is empty, the process return the initial source json.

Case 1

For the template document

    { "n" : "name1" }

The result will be.

    { "name" : "name1" }

In this case the result is exactly what you have in the template, because you have not used any operations of transformation.

Case 2

In this case, the value is a json path. Json path is a query language for JSON, similar to XPath for XML. The implementation of JsonPath is did by newtonsoft. [SelectToken] by the method (https://www.newtonsoft.com/json/help/html/SelectToken.htm).

    { "name" : "$.n" }

The result will be an object with a property named "name" and the value will be the properties "n" at the root of the source json. The value '$..n' is a valid json path implemented by newtonsoft.

Case 3

you can use functions for extend the process.

Like that

    "property name": .mymethod( "$.property", arg2, ...)

'mymethod' is the name of the service you want to call. The sdk provide another keys like sum or distinct. the list is available here. If you write your own method, you must register the methods before in the configuration. the arguments must be any json part (see the directives for register your extension).

A sample for call the method

// Source
{ "prices": [{"n" : 1}, {"n" : 2}, {"n" : 3}] }

// Template
{ "prices": .sum("$..n") } // sum method is a service registered in the services list.

// Result
{ "prices":  6 }

Note

if the string start with '$' the value is automatically convert in json path. if the method expect a string you must cast the value.

{ "prices": .method("$..n" @string) } 

cast

for cast a value you must use this syntax '@type'

  • @uri
  • @time
  • @datetime
  • @string
  • @guid
  • @integer
  • @decimal

Custom services

the customs services are used to extend the feature of the Sdk. You can create your own customs services.

That is the skeleton

[JsltExtensionMethod("method1")] // name of the method in the template
public static JToken ExtendTheSdkMethod1(RuntimeContext ctx)
{
    return new JValue("result");
}

I give you the method loadjson like sample

[JsltExtensionMethod("loadjson")] // name of the method in the template
[JsltExtensionMethodParameter("sourcePath", "directory source path")] // Provide intellisense in the code editor.
public static JToken ExecuteLoadSource(RuntimeContext ctx, string sourcePath)
{

    // Use the system for resolve the file with path relative to the current template script.
    var file = ctx.Configuration.ResolveFile(sourcePath);

    if (file.Exists)
        return file.FullName
            .LoadContentFromFile()
            .ConvertToJson();
        else
        {
            ctx.Diagnostics.AddDiagnostic(Parser.SeverityEnum.Warning, string.Empty, new Parser.TokenLocation(), "file.FullName", $"file '{file.FullName}' not found");
        }

    return JValue.CreateNull();

}

Note, that you can add any parameter.

Disclaimer

If you use a easy treatement, the template is a valid json structure. If you want do more the json syntax become verbose. It is for this reason I have extended the json syntax.

when

the method when is very usefull. it is a switch case.

{
    "prices": .when("$.prop1" @string) 
    {
        "case1": { /* structure to inject if the value of '$.prop1' is equal to 'case1' */ }
    }
} 

How to use

Command line

You can use the command line json.exe. Documentation of json cli.

By code

// Intialization of the configuration
var configuration = new TranformJsonAstConfiguration()
{
    OutputPath = Environment.CurrentDirectory,
};

// add a custom service : Note the services in the sdk are already registered
configuration.Services.ServiceDiscovery.AddService("serviceName", typeof(service));
// if you want to implement your service : use and implemente the interface Bb.Json.Jslt.Services.ITransformJsonService                

TemplateTransformProvider Templateprovider = new TemplateTransformProvider(configuration);

//Build the template translator
StringBuilder sbPayloadTemplate = new StringBuilder(@"payload template");
JsltTemplate template = Templateprovider.GetTemplate(sbPayloadTemplate, false, "name of the template file");


// now the source json
// from text
var source1 = SourceJson.GetFromText("payload");
// from file
var source2 = SourceJson.GetFromFile("filename");
// from json
var source3 = SourceJson.GetFromJson(new JObject());

// Create the sources object with the primary source of data
var src = new Sources(source1);
// you can add additional source of datas
src.Add(source2);
src.Add(source3);

RuntimeContext ctx = template.Transform(src);
var result = ctx.TokenResult;

JSONPath notation

A JSONPath expression specifies a path to an element (or a set of elements) in a JSON structure. Paths can use the dot notation: The implementation is provided by newtonsoft.

Documentation of json path

Custom services embedded in the Sdk

Documentation of the Services

Directives of compilation

You can manage any directives

Sample

"$directives":
{
   "culture":"FR-fr",
   "assemblies":["assembly name referenced in the gac"],
   "functions":["path of the csharp file"],
   "packages":["path of the package on nuget.org"],
   "imports": ["path of the assembly flie"],
   "output": 
   {
       "filter": "$.datas", // It is json path for filter just one part of the output document
       "Mode": .to_block()  // Behavior the output serialization
   }
}   

culture

Set the culture of the process. The Culture specifies a unique name for each culture, based on RFC 4646. The name is a combination of an ISO 639 two-letter lowercase culture code associated with a language and an ISO 3166 two-letter uppercase subculture code associated with a country or region. In addition, for apps that target .NET Framework 4 or later and are running under Windows 10 or later, culture names that correspond to valid BCP-47 language tags are supported.

imports

Take a list of assemblies files. the path is relative to the json template file.

assemblies

Take a list of assemblies name referenced in the GAC.

Functions

Take a list of c# source code file. the path is relative to the json template file. The file contains Csharp source code like this class see the DistinctService like sample

Packages

You can use

"$directives":
{
   "packages": ["path of the assembly file on nuget"],
}

// or 

"$directives":
{
   "packages": [ ["https://www.nuget.org/api/v2/package/", "path of the assembly file"] ],
}

output

the output manage the serialization

Filter

Select just a part of the output document

Mode

Mode is a function that serialize the output document.

to_block()

If the output is an array all lines are serialized (one object by line)

to_json(bool indented, bool ignoreNullAndEmptyValues)

serialize the output in classical serialization

You can write your own serialize service. The returned type must be a StringBuilder


[JsltExtensionMethod("to_block", ForOutput = true)]
public static StringBuilder ExecuteToBlock(RuntimeContext ctx)
{

    var source = ctx.TokenResult;
    var result = new StringBuilder();

    if (source is JObject o)
        result.AppendLine(o.ToString(Newtonsoft.Json.Formatting.None));

    else if (source is JArray a)    
        foreach (var item in a)
            result.AppendLine(item.ToString(Newtonsoft.Json.Formatting.None));
    
    return result;

}

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Black.Beard.Jslt:

Package Downloads
Black.Beard.Jslt.Services

support extended method(loading excel, html, multicsv, sql).

Black.Beard.Jslt.Symbol

Implementation of jslt language in DOTNET. Use a template for transform Json document to another json document.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.350 68 5/2/2024
1.0.349 60 5/2/2024
1.0.348 68 5/2/2024
1.0.347 52 5/2/2024
1.0.346 92 4/7/2024
1.0.345 87 4/7/2024
1.0.344 62 4/5/2024
1.0.343 59 4/5/2024
1.0.342 74 4/5/2024
1.0.341 76 4/5/2024
1.0.340 82 4/4/2024
1.0.339 86 4/4/2024
1.0.338 94 4/3/2024
1.0.337 100 4/3/2024
1.0.336 93 4/3/2024
1.0.335 78 4/3/2024
1.0.334 84 4/2/2024
1.0.333 71 4/2/2024
1.0.332 78 4/2/2024
1.0.331 86 4/2/2024
1.0.330 82 4/2/2024
1.0.329 75 4/2/2024
1.0.328 91 4/1/2024
1.0.327 74 4/1/2024
1.0.325 72 4/1/2024
1.0.324 85 4/1/2024
1.0.323 79 4/1/2024
1.0.322 75 3/31/2024
1.0.321 73 3/31/2024
1.0.319 83 3/31/2024
1.0.318 78 3/31/2024
1.0.317 228 3/19/2024
1.0.316 75 3/19/2024
1.0.315 173 3/15/2024
1.0.314 92 3/15/2024
1.0.313 118 3/13/2024
1.0.312 96 3/13/2024
1.0.311 91 3/13/2024
1.0.310 85 3/13/2024
1.0.309 129 3/12/2024
1.0.308 120 3/12/2024
1.0.306 256 3/5/2024
1.0.305 88 3/5/2024
1.0.304 107 3/4/2024
1.0.303 90 3/4/2024
1.0.302 87 3/4/2024
1.0.301 83 3/4/2024
1.0.300 115 3/3/2024
1.0.299 79 3/3/2024
1.0.298 242 2/26/2024
1.0.297 68 2/26/2024
1.0.296 91 2/25/2024
1.0.295 83 2/25/2024
1.0.294 100 2/24/2024
1.0.293 85 2/24/2024
1.0.292 86 2/24/2024
1.0.291 89 2/24/2024
1.0.290 81 2/24/2024
1.0.289 87 2/24/2024
1.0.288 177 2/21/2024
1.0.287 94 2/21/2024
1.0.286 1,793 12/12/2023
1.0.285 97 12/12/2023
1.0.259 1,508 10/17/2023
1.0.258 99 10/17/2023
1.0.257 211 10/13/2023
1.0.256 103 10/13/2023
1.0.255 202 10/10/2023
1.0.254 111 10/10/2023
1.0.253 157 9/19/2023
1.0.252 104 9/19/2023
1.0.251 121 9/13/2023
1.0.250 117 9/13/2023
1.0.245 150 7/26/2023
1.0.244 138 7/26/2023
1.0.243 150 7/19/2023
1.0.242 141 7/19/2023
1.0.241 117 7/11/2023
1.0.240 129 7/11/2023
1.0.239 129 7/7/2023
1.0.238 116 7/7/2023
1.0.237 142 7/6/2023
1.0.236 133 7/6/2023
1.0.235 127 7/6/2023
1.0.234 135 7/6/2023
1.0.232 124 7/5/2023
1.0.231 131 7/5/2023
1.0.230 132 7/5/2023
1.0.229 143 7/5/2023
1.0.228 135 7/5/2023
1.0.227 134 7/5/2023
1.0.226 131 7/4/2023
1.0.225 126 7/4/2023
1.0.224 158 7/3/2023
1.0.223 132 7/3/2023
1.0.222 127 6/30/2023
1.0.221 115 6/30/2023
1.0.220 139 6/30/2023
1.0.219 127 6/30/2023
1.0.218 155 6/17/2023
1.0.217 137 6/17/2023
1.0.214 139 6/6/2023
1.0.213 131 6/6/2023
1.0.212 123 6/6/2023
1.0.211 129 6/6/2023
1.0.206 124 6/5/2023
1.0.205 122 6/5/2023
1.0.204 146 6/2/2023
1.0.203 107 6/2/2023
1.0.202 141 6/2/2023
1.0.201 139 6/2/2023
1.0.190 542 5/27/2022
1.0.189 497 5/27/2022
1.0.188 508 5/23/2022
1.0.187 515 5/23/2022
1.0.186 473 5/18/2022
1.0.185 482 5/18/2022
1.0.184 524 5/18/2022
1.0.183 517 5/18/2022
1.0.182 516 5/17/2022
1.0.181 512 5/17/2022
1.0.180 509 5/11/2022
1.0.179 519 5/11/2022
1.0.178 529 5/6/2022
1.0.177 533 5/6/2022
1.0.176 553 4/2/2022
1.0.175 536 4/2/2022
1.0.174 521 3/24/2022
1.0.173 509 3/24/2022
1.0.172 479 3/23/2022
1.0.171 500 3/23/2022
1.0.170 500 3/23/2022
1.0.169 495 3/23/2022
1.0.168 532 3/21/2022
1.0.167 524 3/21/2022
1.0.166 522 3/19/2022
1.0.165 545 3/19/2022
1.0.164 521 3/17/2022
1.0.163 523 3/17/2022
1.0.162 514 3/17/2022
1.0.161 526 3/17/2022
1.0.160 512 3/16/2022
1.0.159 531 3/16/2022
1.0.158 536 3/7/2022
1.0.157 534 3/7/2022
1.0.156 524 3/6/2022
1.0.155 522 3/6/2022
1.0.154 532 3/6/2022
1.0.153 530 3/6/2022
1.0.152 524 3/6/2022
1.0.151 536 3/6/2022
1.0.150 521 3/6/2022
1.0.149 544 3/6/2022
1.0.148 545 3/5/2022
1.0.147 536 3/5/2022
1.0.146 500 3/5/2022
1.0.145 628 3/5/2022
1.0.144 624 3/5/2022
1.0.143 638 3/5/2022
1.0.142 633 3/5/2022
1.0.140 534 3/5/2022
1.0.139 513 3/5/2022
1.0.137 517 3/5/2022
1.0.136 497 3/5/2022
1.0.135 389 3/5/2022
1.0.134 395 3/5/2022
1.0.133 528 3/5/2022
1.0.132 533 3/5/2022
1.0.131 512 3/5/2022
1.0.130 534 3/5/2022
1.0.128 512 3/5/2022
1.0.126 397 3/5/2022
1.0.125 415 3/4/2022
1.0.124 415 3/4/2022
1.0.123 409 3/4/2022
1.0.122 422 3/4/2022
1.0.120 429 3/4/2022
1.0.119 413 3/4/2022
1.0.118 407 3/3/2022
1.0.117 394 3/3/2022
1.0.116 392 3/3/2022
1.0.115 420 3/3/2022
1.0.114 427 2/21/2022
1.0.113 407 2/21/2022
1.0.112 437 2/19/2022
1.0.111 404 2/18/2022
1.0.110 408 2/18/2022
1.0.109 405 2/18/2022
1.0.107 401 2/17/2022
1.0.106 779 2/17/2022
1.0.105 447 1/18/2022
1.0.104 434 1/18/2022
1.0.103 283 12/20/2021
1.0.102 278 12/20/2021
1.0.101 265 12/20/2021
1.0.100 264 12/20/2021
1.0.99 245 12/20/2021
1.0.98 239 12/20/2021
1.0.96 268 12/18/2021
1.0.94 304 12/18/2021
1.0.93 283 12/18/2021
1.0.92 297 12/18/2021
1.0.0 405 3/5/2022