Skip to content

Exception while doing calculations in the template with JsonObject as a model #760

Description

@denisbredikhin

Hello,

I have following lines in my template:

"TotalGuaranteePrice": {% assign total_price = 0 %} {% for guarantee in RiscObject[0].GaranteeObject %} {% assign total_price = total_price | plus: guarantee.Price %} {% endfor %} {{ total_price }},

As a model in the context I use JsonObject:

var data = await JsonObject.ParseAsync(stream); var context = new Fluid.TemplateContext(data);
When I try to render, I get the following error:
System.InvalidCastException: 'Unable to cast object of type 'System.Text.Json.Nodes.JsonValuePrimitive1[System.Text.Json.JsonElement]' to type 'System.IConvertible'.'`

I can solve it by adding the following:
options.ValueConverters.Add((value) => value is JsonValue jsonValue && jsonValue.GetValueKind()==JsonValueKind.Number ? jsonValue.GetValue<decimal>() : null);

Can you please implement standard support of JsonValue in calculations?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions