mikebridge / Liquid.NET

.Net Port of the Liquid template language
MIT License
61 stars 15 forks source link

Liquid template convert to json from object #75

Closed pranavdhundhia closed 5 years ago

pranavdhundhia commented 5 years ago

` List ProductList = new List { new Orders {OrderId = 1,ProductName="Some name",Quantity =30}, new Orders {OrderId = 1,ProductName="Some name1",Quantity =30}, new Orders {OrderId = 1,ProductName="Some name2",Quantity =30} };

        ctx.DefineLocalVariable("content", ProductList.ToLiquid());

        // Note that you will still get a best-guess LiquidTemplate, even if you encounter errors.
        var liquidTemplate = LiquidTemplate.Create(@"[
            {% for sales in content %}
                        {
                            ""SalesId"": ""{{sales.OrderId}}"",
               ""ProductName"": ""{{sales.ProductName}}"",
               ""Quantity"": ""{{sales.Quantity}}""
                        },
            {% endfor %}
            ]").OnParsingError(errors.Add)
            .LiquidTemplate;`

Parsing Liquid object to json . No conversion done.

pranavdhundhia commented 5 years ago

Got it resolved by adding latest version(Liquid.NET -Version 0.10.0) Previously was using 0.9.0 `[Fact] public void Test_JsonToJsonParser() { var errors = new List(); ITemplateContext ctx = new TemplateContext();

        List<Sales> SalesList = new List<Sales>
   {
        new Sales {SalesId = 1,ProductName="Some name",Quantity =30},
       new Sales {SalesId = 2,ProductName="Some name1",Quantity =30},
       new Sales {SalesId = 3,ProductName="Some name2",Quantity =30}
    };
        List<Orders> ProductList = new List<Orders>
           {
                new Orders {OrderId = 1,ProductName="Some name",Quantity =30},
               new Orders {OrderId = 2,ProductName="Some name1",Quantity =30},
               new Orders {OrderId = 3,ProductName="Some name2",Quantity =30}
            };
        ctx.DefineLocalVariable("content", ProductList.ToLiquid());

        // Note that you will still get a best-guess LiquidTemplate, even if you encounter errors.
        var liquidTemplate = LiquidTemplate.Create(@"[
            {% for sales in content %}
                        {
                            ""SalesId"": {{sales.orderid}},
               ""ProductName"": ""{{sales.productname}}"",
               ""Quantity"": {{sales.quantity}}
                        },
            {% endfor %}
            ]").OnParsingError(errors.Add)
            .LiquidTemplate;

        var resultdata = liquidTemplate.Render(ctx).Result;

        IEnumerable<Sales> salesresult  = JsonConvert.DeserializeObject<List<Sales>>(resultdata);

        Assert.Equal(salesresult.Count(), SalesList.Count());
    }`
pranavdhundhia commented 5 years ago

Got it resolved by adding latest version(Liquid.NET -Version 0.10.0) Previously was using 0.9.0