JTran 4.1.0

dotnet add package JTran --version 4.1.0
NuGet\Install-Package JTran -Version 4.1.0
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="JTran" Version="4.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JTran --version 4.1.0
#r "nuget: JTran, 4.1.0"
#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 JTran as a Cake Addin
#addin nuget:?package=JTran&version=4.1.0

// Install JTran as a Cake Tool
#tool nuget:?package=JTran&version=4.1.0

JTran

JTran is a .Net Standard Library for doing JSON to JSON transformations.

JTran is heavily influenced by XSLT but whereas XSLT does XML to XML transformations, JTran does JSON to JSON transformations.

Getting started

Installing via NuGet
Install-Package JTran

A transform is a JSON file that contains JTran processing instructions. To transform a source JSON document you provide the source JSON and the transform:

public class JTranSample
{
    public string Transform(string transform, string source)
    {
        var transformer = new JTran.Transformer(transform);
        var context     = new TransformContext { Arguments = new Dictionary<string, object>() };

        return transformer.Transform(source, context);
    }
}

You can input and output directly from/to streams

public class JTranSample2
{
    public void Transform(string transform, Stream input, Stream output)
    {
        var transformer = new JTran.Transformer(transform);
        var context     = new TransformContext { Arguments = new Dictionary<string, object>() };

        transformer.Transform(input, output, context);
    }
}

You can input directly from a POCO or a list of POCOS

public class JTranSample3
{
    public void TransformSchool(string transform, School input, Stream output)
    {
        var transformer = new JTran.Transformer(transform);

        transformer.Transform(input, output);
    }

    public void TransformEmployees(string transform, List<Employee> input, Stream output)
    {
        var transformer = new JTran.Transformer(transform);

        transformer.Transform(input, output);
    }
}

You can output to multiple json documents, e.g. files by using the IStreamFactory. Note that your JTran must output to an array, e.g. use "[]" in a #foreach. Each object in that array is output as a separate document.

public class JTranSample4
{
    public void TransformToFiles(string transform, Stream data)
    {
        var transformer = new JTran.Transformer(transform);

        // FileStreamFactory is provided by the JTran library but you can implement your own class. 
        //   See the MongoDBTests project for an example.
        var output = new FileStreamFactory((index)=> $"c:\\documents\jtran\file_{index}.json"); // Pass in a lambda to name each file

        transformer.Transform(data, output);
    }
}

Return a POCO

public class JTranSample5
{
    public List<Student> Transform(string transform, Stream input)
    {
        var transformer = new JTran.Transformer(transform);
        using var output = new MemoryStream();

        transformer.Transform(input, output);

        return output.ToObject<List<Student>>();
    }
}
TransformerContext

The TransformerContext provides a way of extending a default transform

public class JTranSample
{
    public string Transform(string transform, string source)
    {
        var transformer = new JTran.Transformer(transform);
        var context     = new TransformContext { Arguments = new Dictionary<string, object>() };

        return transformer.Transform(source, context);
    }
}

<br /> <small>IDictionary<string, object>?</small> <b>Arguments</b><br /><br /> This is a dictionary to pass in a set of arguments. This dictionary is called at runtime and since it's an interface you could implement a custom dictionary that returns values from a secret store or configuration store, e.g. KeyVault or Microsoft.Extensions.IConfiguration<br /><br />

<small>IDictionary<string, IDocumentRepository></small> <b>DocumentRepositories</b><br /><br /> This is a dictionary of document repositories. These are how calls to the document() function are resolved. The repo name as the first argument in that function is the key in the dictionary you provide here.

<small>bool</small> <b>AllowDeferredLoading</b><br /><br /> When the input source document is a json array (starts with "[") by default the transform will not load the json until it starts to get processed, e.g. thru a #foreach loop and will only load one item at at time. This is to allow super large json documents that would otherwise cause memory issues. However in certain cases this may cause performance issues. For instance if you jtran code is accessing items out of order, e.g. "#(@[42])". Setting this value to false will cause the entire json source to be parsed and loaded at once.

<small>IReadOnlyDictionary<string, object></small> <b>OutputArguments</b><br /><br /> This dictionary is a readonly value. It where any output variables set in the transform, e.g. "#outputvariable(Name, 'Fred')". Once the transform is complete this dictionary will be filled with output variables

<small>Action<string, object>?</small> <b>OnOutputArgument</b><br /><br /> Allows a lambda expression to passed in that will be called immediately as soon as an output variable is set in the transform.

<br>

<small>Note: The transformer would benefit from caching so it would be better to inject the transformer object as a singleton in your dependency injection code.</small>

<br>

<strong>Language Reference</strong>

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.1.0 91 4/28/2024
4.0.0 104 4/1/2024
3.4.2 503 12/28/2023
3.4.1 414 12/11/2023
3.4.0 110 12/7/2023
3.3.3 490 11/5/2023
3.3.2 251 10/25/2023
3.3.1 117 10/24/2023
3.2.0 113 8/3/2023
3.1.6 1,300 6/26/2023
3.1.5 248 6/5/2023
3.1.4 745 3/6/2023
3.1.3 440 2/6/2023
3.1.2 269 2/6/2023
3.1.1 899 10/18/2022
3.1.0 506 8/18/2022
3.0.0 19,126 2/19/2022
2.4.0 615 12/6/2020
2.3.1 419 11/2/2020
2.3.0 400 10/25/2020
2.2.1 392 10/21/2020
2.2.0 455 10/21/2020
2.1.1 470 10/14/2020
2.1.0 494 10/11/2020
2.0.4 491 10/7/2020
2.0.3 476 10/6/2020
2.0.2 533 10/6/2020
2.0.1 477 10/5/2020
2.0.0 519 10/5/2020
1.3.0 437 10/1/2020
1.2.0 586 7/26/2020
1.1.0 484 6/21/2020
1.0.2 518 6/15/2020
1.0.1 454 6/14/2020
1.0.0 431 6/2/2020