Repository: PowerShell/PowerShell
File: test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.Tests.ps1 (PowerShell)

Code before:
            $json = ConvertTo-Json -InputObject $arr -Compress
            $json | Should -BeExactly '[[[1,2],[3,4]],[[5,6],[7,8]]]'
        }

        It 'Should serialize jagged array correctly via InputObject' {
            $arr = @(@(1), @(2, 3), @(4, 5, 6))
            $json = ConvertTo-Json -InputObject $arr -Compress
            $json | Should -BeExactly '[[1],[2,3],[4,5,6]]'
        }

        It 'Should serialize array containing empty arrays correctly' {
            $arr = @(@(), @(1), @())
            $json = ConvertTo-Json -InputObject $arr -Compress
            $json | Should -BeExactly '[[],[1],[]]'
        }

        It 'Should serialize deeply nested array with Depth limit' {
            $arr = ,(,(,(,(1))))
            $json = ConvertTo-Json -InputObject $arr -Compress -Depth 2
            $json | Should -BeExactly '[[["1"]]]'
        }

        It 'Should serialize deeply nested array with sufficient Depth' {
            $arr = ,(,(,(,(1))))
            $json = ConvertTo-Json -InputObject $arr -Compress -Depth 10
            $json | Should -BeExactly '[[[[1]]]]'
        }
    }

    Context 'Array with mixed content types' {
        It 'Should serialize array with mixed scalars correctly' {
            $arr = @(1, 'two', 3.14, $true, $null)
            $json = ConvertTo-Json -InputObject $arr -Compress
            $json | Should -BeExactly '[1,"two",3.14,true,null]'
        }

        It 'Should serialize array with nested array and scalars correctly' {
            $arr = @(1, @(2, 3), 4)
            $json = ConvertTo-Json -InputObject $arr -Compress
            $json | Should -BeExactly '[1,[2,3],4]'
        }

        It 'Should serialize array with hashtable elements correctly' {
            $arr = @(@{a = 1}, @{b = 2})
            $json = ConvertTo-Json -InputObject $arr -Compress
            $json | Should -BeExactly '[{"a":1},{"b":2}]'
        }

        It 'Should serialize array with PSCustomObject elements correctly' {
            $arr = @([PSCustomObject]@{x = 1}, [PSCustomObject]@{y = 2})
            $json = ConvertTo-Json -InputObject $arr -Compress

Review comment (suggestion):
I'd replace `1` with a `value` so that we can check that result is `value.ToString()`.
(We could use IPAddress in this and previous tests.)

Code after:
    Context 'String scalar types' {
        It 'Should serialize string <Description> correctly via Pipeline and InputObject' -TestCases @(
            @{ Description = 'regular'; Value = 'hello'; Expected = '"hello"' }
            @{ Description = 'empty'; Value = ''; Expected = '""' }
            @{ Description = 'with spaces'; Value = 'hello world'; Expected = '"hello world"' }
            @{ Description = 'with newline'; Value = "line1`nline2"; Expected = '"line1\nline2"' }
            @{ Description = 'with tab'; Value = "col1`tcol2"; Expected = '"col1\tcol2"' }
            @{ Description = 'with quotes'; Value = 'say "hello"'; Expected = '"say \"hello\""' }
            @{ Description = 'with backslash'; Value = 'c:\path'; Expected = '"c:\\path"' }
            @{ Description = 'unicode'; Value = '???'; Expected = '"???"' }
            @{ Description = 'emoji'; Value = '??'; Expected = '"??"' }
        ) {
            param($Description, $Value, $Expected)
            $jsonPipeline = $Value | ConvertTo-Json -Compress
            $jsonInputObject = ConvertTo-Json -InputObject $Value -Compress
            $jsonPipeline | Should -BeExactly $Expected
            $jsonInputObject | Should -BeExactly $Expected
        }

        It 'Should ignore ETS properties on string' {
            $str = Add-Member -InputObject 'hello' -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
            $json = $str | ConvertTo-Json -Compress
            $json | Should -BeExactly '"hello"'
        }
    }

    Context 'DateTime and related types' {
        It 'Should serialize DateTime with UTC kind via Pipeline and InputObject' {
            $dt = [DateTime]::new(2024, 6, 15, 10, 30, 0, [DateTimeKind]::Utc)
            $jsonPipeline = $dt | ConvertTo-Json -Compress
            $jsonInputObject = ConvertTo-Json -InputObject $dt -Compress
            $jsonPipeline | Should -BeExactly '"2024-06-15T10:30:00Z"'
            $jsonInputObject | Should -BeExactly '"2024-06-15T10:30:00Z"'
        }

        It 'Should serialize DateTime with Local kind' {
            $dt = [DateTime]::new(2024, 6, 15, 10, 30, 0, [DateTimeKind]::Local)
            $json = $dt | ConvertTo-Json -Compress
            $offset = $dt.ToString('zzz')
            $expected = '"2024-06-15T10:30:00' + $offset + '"'
            $json | Should -BeExactly $expected
        }

        It 'Should serialize DateTime with Unspecified kind via Pipeline and InputObject' {
            $dt = [DateTime]::new(2024, 6, 15, 10, 30, 0, [DateTimeKind]::Unspecified)
            $jsonPipeline = $dt | ConvertTo-Json -Compress
            $jsonInputObject = ConvertTo-Json -InputObject $dt -Compress
            $jsonPipeline | Should -BeExactly '"2024-06-15T10:30:00"'
            $jsonInputObject | Should -BeExactly '"2024-06-15T10:30:00"'
        }