JamesNK/Newtonsoft.Json · error · ArgumentException

Indentation value must be greater than 0.

Error message

Indentation value must be greater than 0.

What it means

Thrown by the JsonTextWriter.Indentation setter when the supplied value is less than zero. Indentation controls how many IndentChar characters are emitted per hierarchy level under Formatting.Indented. Negative indentation is nonsensical, so the setter rejects it with ArgumentException. Note the message says 'greater than 0' but the guard is `< 0`, so zero is actually accepted (meaning flat output) despite the wording.

Source

Thrown at Src/Newtonsoft.Json/JsonTextWriter.cs:97

                {
                    throw new ArgumentNullException(nameof(value));
                }

                _arrayPool = value;
            }
        }

        /// <summary>
        /// Gets or sets how many <see cref="JsonTextWriter.IndentChar"/>s to write for each level in the hierarchy when <see cref="JsonWriter.Formatting"/> is set to <see cref="Formatting.Indented"/>.
        /// </summary>
        public int Indentation
        {
            get => _indentation;
            set
            {
                if (value < 0)
                {
                    throw new ArgumentException("Indentation value must be greater than 0.");
                }

                _indentation = value;
            }
        }

        /// <summary>
        /// Gets or sets which character to use to quote attribute values.
        /// </summary>
        public char QuoteChar
        {
            get => _quoteChar;
            set
            {
                if (value != '"' && value != '\'')
                {
                    throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and "".");
                }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Use a non-negative integer (0 disables visible indentation; typical is 2).
  2. Validate config: writer.Indentation = Math.Max(0, configured);.
  3. If you only need pretty-printing, set Formatting = Formatting.Indented and leave Indentation at default (2).

Example fix

// before
writer.Indentation = -1;

// after
writer.Indentation = 2;
Defensive patterns

Strategy: validation

Validate before calling

writer.Indentation = Math.Max(0, configuredIndentation);

Type guard

static bool IsValidIndentation(int v) => v >= 0;

Prevention

When it happens

Trigger: Assigning writer.Indentation = -1; computing indentation from config/arithmetic that can go negative.

Common situations: Config-driven indentation without validation; subtractive math producing a negative; misreading the message and assuming 0 is illegal.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/e6c77096a6f6f73c. Report an issue: GitHub.