Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 1, 2025

Adds configurable JsonWriterOptions to TemplateOptions and TemplateContext, allowing fine-grained control over JSON serialization (indentation, encoding, depth limits) in the json filter.

Changes

  • TemplateOptions: Added JsonWriterOptions property with default value (non-indented)
  • TemplateContext: Added JsonWriterOptions property initialized from TemplateOptions in constructor
  • MiscFilters.Json: Now uses context.JsonWriterOptions directly without argument support
  • Tests: Added coverage for options from both TemplateOptions and TemplateContext, and enum serialization

Breaking Change

⚠️ The json filter no longer accepts arguments. Previously, you could pass a boolean argument to control indentation (e.g., {{ data | json: true }}). This functionality has been removed. Configure output formatting via JsonWriterOptions in TemplateOptions or TemplateContext instead.

Usage

var options = new TemplateOptions
{
    JsonWriterOptions = new JsonWriterOptions
    {
        Indented = true,
        MaxDepth = 32,
        Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
    }
};

var context = new TemplateContext(options);
// All json filter calls now use these configured options
{{ data | json }}

By default, JsonWriterOptions produces non-indented (compact) JSON output.

Original prompt

Feature: Make JsonWriterOptions reusable and configurable via TemplateOptions and TemplateContext, and update documentation.

  • Add JsonWriterOptions property to TemplateOptions, with XML doc comments styled like similar properties.
  • Add JsonWriterOptions property to TemplateContext, initialized from TemplateOptions in constructor, also with XML doc comments.
  • Update MiscFilters.Json filter to use context.JsonWriterOptions instead of context.Options.JsonWriterOptions (preserving argument override for Indented).
  • Add and update tests in MiscFiltersTests to verify options from TemplateOptions and TemplateContext are respected, and that enums can be serialized appropriately.
  • If other properties in TemplateOptions or TemplateContext have XML docs, match style and placement.

This pull request was created as a result of the following prompt from Copilot chat.

Feature: Make JsonWriterOptions reusable and configurable via TemplateOptions and TemplateContext, and update documentation.

  • Add JsonWriterOptions property to TemplateOptions, with XML doc comments styled like similar properties.
  • Add JsonWriterOptions property to TemplateContext, initialized from TemplateOptions in constructor, also with XML doc comments.
  • Update MiscFilters.Json filter to use context.JsonWriterOptions instead of context.Options.JsonWriterOptions (preserving argument override for Indented).
  • Add and update tests in MiscFiltersTests to verify options from TemplateOptions and TemplateContext are respected, and that enums can be serialized appropriately.
  • If other properties in TemplateOptions or TemplateContext have XML docs, match style and placement.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Make JsonWriterOptions reusable and configurable via TemplateOptions Add JsonWriterOptions to TemplateOptions and TemplateContext Nov 1, 2025
Copilot AI requested a review from sebastienros November 1, 2025 22:36
Copilot finished work on behalf of sebastienros November 1, 2025 22:36
@sebastienros
Copy link
Owner

@copilot change the behavior of the json filter so it doesn't take an argument. Instead it will use the context option as it is. By default the JsonWriterOptions should not be indented, this might be a breaking change.

Copy link
Contributor Author

Copilot AI commented Nov 1, 2025

@copilot change the behavior of the json filter so it doesn't take an argument. Instead it will use the context option as it is. By default the JsonWriterOptions should not be indented, this might be ...

Done in commit a9eb47a. The json filter now uses context.JsonWriterOptions directly without any argument support. JsonWriterOptions defaults to non-indented output (Indented=false). Removed the test that was checking argument override behavior.

Copilot finished work on behalf of sebastienros November 1, 2025 22:43
/// <summary>
/// Gets or sets the <see cref="JsonWriterOptions"/> used by the <c>json</c> filter.
/// </summary>
public JsonWriterOptions JsonWriterOptions { get; set; } = new JsonWriterOptions();
Copy link
Owner

@sebastienros sebastienros Nov 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot The initialization is unnecessary because the value is set in the constructor from the TemplateOptions

@sebastienros sebastienros marked this pull request as ready for review November 1, 2025 23:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants