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

Code before:

        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 using ToString' {
            $ip = [System.Net.IPAddress]::Parse('192.168.1.1')
            $arr = ,(,(,(,($ip))))
            $json = ConvertTo-Json -InputObject $arr -Compress -Depth 2
            $json | Should -BeExactly '[[["192.168.1.1"]]]'
        }

        It 'Should serialize deeply nested array with sufficient Depth as full object' {
            $ip = [System.Net.IPAddress]::Parse('10.0.0.1')
            $arr = ,(,(,(,($ip))))
            $json = ConvertTo-Json -InputObject $arr -Compress -Depth 10
            $json | Should -BeLike '*AddressFamily*'
            $json | Should -Not -BeExactly '[[[["10.0.0.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 PSCustomObject elements correctly' {
            $arr = @([PSCustomObject]@{x = 1}, [PSCustomObject]@{y = 2})
            $json = ConvertTo-Json -InputObject $arr -Compress
            $json | Should -BeExactly '[{"x":1},{"y":2}]'
        }

        It 'Should serialize array with DateTime elements correctly' {
            $date1 = [DateTime]::new(2024, 6, 15, 10, 30, 0, [DateTimeKind]::Utc)
            $date2 = [DateTime]::new(2024, 12, 25, 0, 0, 0, [DateTimeKind]::Utc)

Review comment (suggestion):
I'd prefer "192.168.1.1" as in previous test and BeExactly  with `{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952,"IPAddressToString":"192.168.1.1"}`

Code after:
            @{ 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"'
        }

        It 'Should serialize DateTimeOffset correctly via Pipeline and InputObject' {
            $dto = [DateTimeOffset]::new(2024, 6, 15, 10, 30, 0, [TimeSpan]::FromHours(9))
            $jsonPipeline = $dto | ConvertTo-Json -Compress