Skip to content

Newtonsoft.Json support

JPVenson edited this page May 19, 2022 · 5 revisions

Morestachio offers native support for Json related types from a Newtonesoft.Json package by providing the Nuget Morestachio Json nuget package. When deserializing a Json document, you might not always have an object structure for that document or you have simply to many or dynamic Json documents. In that case you can use the JsonNetValueResolver from the Morestachio.Newtonsoft.Json project.

To enable the JsonNetValueResolver, pull the NugetPackage and set the ValueConverter by using the JsonNetValueResolverExtensions.WithJsonValueResolver extension method:

var doc = await ParserOptionsBuilder.New()
         .WithTemplate(...)
         .WithJsonValueResolver()
         .BuildAndParseAsync();

When enabled, Morestachio can use objects of type:

  • JObject
  • JValue
  • JArray

like this:

var jsonDoc = @"{ ""data"": ""hello World"" }";
var jDoc = Newtonsoft.Json.JsonConvert(jsonDoc);
doc.CreateRenderer().RenderAndStringify(jDoc);

and you can access any object or property of you json file directly in morestachio with expression: {{data}}