Add comprehensive scalar type tests for ConvertTo-Json (#26736)

This commit is contained in:
Yoshifumi authored and GitHub committed 2026-02-05 15:51:22 -08:00
1 parent d54977441f
commit dd4f2fecfa
1 file changed
+429
@@ -156,4 +156,433 @@ Describe 'ConvertTo-Json' -tags "CI" {
$actual = ConvertTo-Json -Compress -InputObject $obj
$actual | Should -Be '{"Positive":18446744073709551615,"Negative":-18446744073709551615}'
}
#region Comprehensive Scalar Type Tests (Phase 1)
# Test coverage for ConvertTo-Json scalar serialization
# Covers: Pipeline vs InputObject, ETS vs no ETS, all primitive and special types
Context 'Primitive scalar types' {
It 'Should serialize <TypeName> value <Value> correctly via Pipeline and InputObject' -TestCases @(
# Byte types
@{ TypeName = 'byte'; Value = [byte]0; Expected = '0' }
@{ TypeName = 'byte'; Value = [byte]255; Expected = '255' }
@{ TypeName = 'sbyte'; Value = [sbyte]-128; Expected = '-128' }
@{ TypeName = 'sbyte'; Value = [sbyte]127; Expected = '127' }
# Short types
@{ TypeName = 'short'; Value = [short]-32768; Expected = '-32768' }
@{ TypeName = 'short'; Value = [short]32767; Expected = '32767' }
@{ TypeName = 'ushort'; Value = [ushort]0; Expected = '0' }
@{ TypeName = 'ushort'; Value = [ushort]65535; Expected = '65535' }
# Integer types
@{ TypeName = 'int'; Value = 42; Expected = '42' }
@{ TypeName = 'int'; Value = -42; Expected = '-42' }
@{ TypeName = 'int'; Value = 0; Expected = '0' }
@{ TypeName = 'int'; Value = [int]::MaxValue; Expected = '2147483647' }
@{ TypeName = 'int'; Value = [int]::MinValue; Expected = '-2147483648' }
@{ TypeName = 'uint'; Value = [uint]0; Expected = '0' }
@{ TypeName = 'uint'; Value = [uint]::MaxValue; Expected = '4294967295' }
# Long types
@{ TypeName = 'long'; Value = [long]::MaxValue; Expected = '9223372036854775807' }
@{ TypeName = 'long'; Value = [long]::MinValue; Expected = '-9223372036854775808' }
@{ TypeName = 'ulong'; Value = [ulong]0; Expected = '0' }
@{ TypeName = 'ulong'; Value = [ulong]::MaxValue; Expected = '18446744073709551615' }
# Floating-point types
@{ TypeName = 'float'; Value = [float]3.14; Expected = '3.14' }
@{ TypeName = 'float'; Value = [float]::NaN; Expected = '"NaN"' }
@{ TypeName = 'float'; Value = [float]::PositiveInfinity; Expected = '"Infinity"' }
@{ TypeName = 'float'; Value = [float]::NegativeInfinity; Expected = '"-Infinity"' }
@{ TypeName = 'double'; Value = 3.14159; Expected = '3.14159' }
@{ TypeName = 'double'; Value = -3.14159; Expected = '-3.14159' }
@{ TypeName = 'double'; Value = 0.0; Expected = '0.0' }
@{ TypeName = 'double'; Value = [double]::NaN; Expected = '"NaN"' }
@{ TypeName = 'double'; Value = [double]::PositiveInfinity; Expected = '"Infinity"' }
@{ TypeName = 'double'; Value = [double]::NegativeInfinity; Expected = '"-Infinity"' }
@{ TypeName = 'decimal'; Value = 123.456d; Expected = '123.456' }
# BigInteger
@{ TypeName = 'BigInteger'; Value = 18446744073709551615n; Expected = '18446744073709551615' }
# Boolean
@{ TypeName = 'bool'; Value = $true; Expected = 'true' }
@{ TypeName = 'bool'; Value = $false; Expected = 'false' }
# Null
@{ TypeName = 'null'; Value = $null; Expected = 'null' }
) {
param($TypeName, $Value, $Expected)
$jsonPipeline = $Value | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $Value -Compress
$jsonPipeline | Should -BeExactly $Expected
$jsonInputObject | Should -BeExactly $Expected
}
It 'Should include ETS properties on <TypeName>' -TestCases @(
@{ TypeName = 'int'; Value = 42; Expected = '{"value":42,"MyProp":"test"}' }
@{ TypeName = 'double'; Value = 3.14; Expected = '{"value":3.14,"MyProp":"test"}' }
) {
param($TypeName, $Value, $Expected)
$valueWithEts = Add-Member -InputObject $Value -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $valueWithEts | ConvertTo-Json -Compress
$json | Should -BeExactly $Expected
}
}
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"'
}
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
$jsonInputObject = ConvertTo-Json -InputObject $dto -Compress
$jsonPipeline | Should -BeExactly '"2024-06-15T10:30:00+09:00"'
$jsonInputObject | Should -BeExactly '"2024-06-15T10:30:00+09:00"'
}
It 'Should serialize DateOnly as object with properties' {
$d = [DateOnly]::new(2024, 6, 15)
$json = $d | ConvertTo-Json -Compress
$json | Should -BeExactly '{"Year":2024,"Month":6,"Day":15,"DayOfWeek":6,"DayOfYear":167,"DayNumber":739051}'
}
It 'Should serialize TimeOnly as object with properties' {
$t = [TimeOnly]::new(10, 30, 45)
$json = $t | ConvertTo-Json -Compress
$json | Should -BeExactly '{"Hour":10,"Minute":30,"Second":45,"Millisecond":0,"Microsecond":0,"Nanosecond":0,"Ticks":378450000000}'
}
It 'Should serialize TimeSpan as object with properties' {
$ts = [TimeSpan]::new(1, 2, 3, 4, 5)
$json = $ts | ConvertTo-Json -Compress
$json | Should -BeExactly '{"Ticks":937840050000,"Days":1,"Hours":2,"Milliseconds":5,"Microseconds":0,"Nanoseconds":0,"Minutes":3,"Seconds":4,"TotalDays":1.0854630208333333,"TotalHours":26.0511125,"TotalMilliseconds":93784005.0,"TotalMicroseconds":93784005000.0,"TotalNanoseconds":93784005000000.0,"TotalMinutes":1563.06675,"TotalSeconds":93784.005}'
}
It 'Should ignore ETS properties on DateTime' {
$dt = [DateTime]::new(2024, 6, 15, 0, 0, 0, [DateTimeKind]::Utc)
$dt = Add-Member -InputObject $dt -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $dt | ConvertTo-Json -Compress
$json | Should -BeExactly '"2024-06-15T00:00:00Z"'
}
It 'Should include ETS properties on DateTimeOffset' {
$dto = [DateTimeOffset]::new(2024, 6, 15, 10, 30, 0, [TimeSpan]::Zero)
$dto = Add-Member -InputObject $dto -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $dto | ConvertTo-Json -Compress
$json | Should -BeExactly '{"value":"2024-06-15T10:30:00+00:00","MyProp":"test"}'
}
It 'Should include ETS properties on DateOnly' {
$d = [DateOnly]::new(2024, 6, 15)
$d = Add-Member -InputObject $d -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $d | ConvertTo-Json -Compress
$json | Should -BeExactly '{"Year":2024,"Month":6,"Day":15,"DayOfWeek":6,"DayOfYear":167,"DayNumber":739051,"MyProp":"test"}'
}
It 'Should include ETS properties on TimeOnly' {
$t = [TimeOnly]::new(10, 30, 45)
$t = Add-Member -InputObject $t -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $t | ConvertTo-Json -Compress
$json | Should -BeExactly '{"Hour":10,"Minute":30,"Second":45,"Millisecond":0,"Microsecond":0,"Nanosecond":0,"Ticks":378450000000,"MyProp":"test"}'
}
}
Context 'Guid type' {
It 'Should serialize Guid as string via InputObject' {
$guid = [Guid]::new('12345678-1234-1234-1234-123456789abc')
$json = ConvertTo-Json -InputObject $guid -Compress
$json | Should -BeExactly '"12345678-1234-1234-1234-123456789abc"'
}
It 'Should serialize Guid with Extended properties via Pipeline' {
$guid = [Guid]::new('12345678-1234-1234-1234-123456789abc')
$json = $guid | ConvertTo-Json -Compress
$json | Should -BeExactly '{"value":"12345678-1234-1234-1234-123456789abc","Guid":"12345678-1234-1234-1234-123456789abc"}'
}
It 'Should serialize empty Guid correctly via InputObject' {
$json = ConvertTo-Json -InputObject ([Guid]::Empty) -Compress
$json | Should -BeExactly '"00000000-0000-0000-0000-000000000000"'
}
It 'Should include ETS properties on Guid via Pipeline' {
$guid = [Guid]::new('12345678-1234-1234-1234-123456789abc')
$guid = Add-Member -InputObject $guid -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $guid | ConvertTo-Json -Compress
$json | Should -BeExactly '{"value":"12345678-1234-1234-1234-123456789abc","MyProp":"test","Guid":"12345678-1234-1234-1234-123456789abc"}'
}
}
Context 'Uri type' {
It 'Should serialize Uri <Description> correctly via Pipeline and InputObject' -TestCases @(
@{ Description = 'http'; UriString = 'http://example.com'; Expected = '"http://example.com"' }
@{ Description = 'https with path'; UriString = 'https://example.com/path'; Expected = '"https://example.com/path"' }
@{ Description = 'with query'; UriString = 'https://example.com/search?q=test'; Expected = '"https://example.com/search?q=test"' }
@{ Description = 'file'; UriString = 'file:///c:/temp/file.txt'; Expected = '"file:///c:/temp/file.txt"' }
) {
param($Description, $UriString, $Expected)
$uri = [Uri]$UriString
$jsonPipeline = $uri | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $uri -Compress
$jsonPipeline | Should -BeExactly $Expected
$jsonInputObject | Should -BeExactly $Expected
}
It 'Should include ETS properties on Uri' {
$uri = [Uri]'https://example.com'
$uri = Add-Member -InputObject $uri -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $uri | ConvertTo-Json -Compress
$json | Should -BeExactly '{"value":"https://example.com","MyProp":"test"}'
}
}
Context 'Enum types' {
It 'Should serialize enum <EnumType>::<Value> as <Expected> via Pipeline and InputObject' -TestCases @(
@{ EnumType = 'System.DayOfWeek'; Value = 'Sunday'; Expected = '0' }
@{ EnumType = 'System.DayOfWeek'; Value = 'Monday'; Expected = '1' }
@{ EnumType = 'System.DayOfWeek'; Value = 'Saturday'; Expected = '6' }
@{ EnumType = 'System.ConsoleColor'; Value = 'Red'; Expected = '12' }
@{ EnumType = 'System.IO.FileAttributes'; Value = 'ReadOnly'; Expected = '1' }
@{ EnumType = 'System.IO.FileAttributes'; Value = 'Hidden'; Expected = '2' }
) {
param($EnumType, $Value, $Expected)
$enumValue = [Enum]::Parse($EnumType, $Value)
$jsonPipeline = $enumValue | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $enumValue -Compress
$jsonPipeline | Should -BeExactly $Expected
$jsonInputObject | Should -BeExactly $Expected
}
It 'Should serialize enum as "<Expected>" with -EnumsAsStrings' -TestCases @(
@{ EnumType = 'System.DayOfWeek'; Value = 'Sunday'; Expected = 'Sunday' }
@{ EnumType = 'System.DayOfWeek'; Value = 'Monday'; Expected = 'Monday' }
@{ EnumType = 'System.ConsoleColor'; Value = 'Red'; Expected = 'Red' }
) {
param($EnumType, $Value, $Expected)
$enumValue = [Enum]::Parse($EnumType, $Value)
$json = $enumValue | ConvertTo-Json -Compress -EnumsAsStrings
$json | Should -BeExactly "`"$Expected`""
}
It 'Should serialize flags enum correctly via Pipeline and InputObject' {
$flags = [System.IO.FileAttributes]::ReadOnly -bor [System.IO.FileAttributes]::Hidden
$jsonPipeline = $flags | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $flags -Compress
$jsonPipeline | Should -BeExactly '3'
$jsonInputObject | Should -BeExactly '3'
}
It 'Should serialize flags enum as string with -EnumsAsStrings' {
$flags = [System.IO.FileAttributes]::ReadOnly -bor [System.IO.FileAttributes]::Hidden
$json = $flags | ConvertTo-Json -Compress -EnumsAsStrings
$json | Should -BeExactly '"ReadOnly, Hidden"'
}
It 'Should include ETS properties on Enum' {
$enum = Add-Member -InputObject ([DayOfWeek]::Monday) -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $enum | ConvertTo-Json -Compress
$json | Should -BeExactly '{"value":1,"MyProp":"test"}'
}
}
Context 'IPAddress type' {
It 'Should serialize IPAddress v4 correctly via InputObject' {
$ip = [System.Net.IPAddress]::Parse('192.168.1.1')
$json = ConvertTo-Json -InputObject $ip -Compress
$json | Should -BeExactly '{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952}'
}
It 'Should serialize IPAddress v4 correctly via Pipeline' {
$ip = [System.Net.IPAddress]::Parse('192.168.1.1')
$json = $ip | ConvertTo-Json -Compress
$json | Should -BeExactly '{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952,"IPAddressToString":"192.168.1.1"}'
}
It 'Should serialize IPAddress v6 correctly via InputObject' {
$ip = [System.Net.IPAddress]::Parse('::1')
$json = ConvertTo-Json -InputObject $ip -Compress
$json | Should -BeExactly '{"AddressFamily":23,"ScopeId":0,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":null}'
}
It 'Should serialize IPAddress v6 correctly via Pipeline' {
$ip = [System.Net.IPAddress]::Parse('::1')
$json = $ip | ConvertTo-Json -Compress
$json | Should -BeExactly '{"AddressFamily":23,"ScopeId":0,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":null,"IPAddressToString":"::1"}'
}
It 'Should include ETS properties on IPAddress' {
$ip = [System.Net.IPAddress]::Parse('192.168.1.1')
$ip = Add-Member -InputObject $ip -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = $ip | ConvertTo-Json -Compress
$json | Should -BeExactly '{"AddressFamily":2,"ScopeId":null,"IsIPv6Multicast":false,"IsIPv6LinkLocal":false,"IsIPv6SiteLocal":false,"IsIPv6Teredo":false,"IsIPv6UniqueLocal":false,"IsIPv4MappedToIPv6":false,"Address":16885952,"MyProp":"test","IPAddressToString":"192.168.1.1"}'
}
}
Context 'Scalars as elements of arrays' {
It 'Should serialize array of <TypeName> correctly via Pipeline and InputObject' -TestCases @(
@{ TypeName = 'int'; Values = @(1, 2, 3); Expected = '[1,2,3]' }
@{ TypeName = 'string'; Values = @('a', 'b', 'c'); Expected = '["a","b","c"]' }
@{ TypeName = 'double'; Values = @(1.1, 2.2, 3.3); Expected = '[1.1,2.2,3.3]' }
) {
param($TypeName, $Values, $Expected)
$jsonPipeline = $Values | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $Values -Compress
$jsonPipeline | Should -BeExactly $Expected
$jsonInputObject | Should -BeExactly $Expected
}
# Note: bool array test uses InputObject only because $true/$false are singletons
# and ETS properties added in other tests would affect Pipeline serialization
It 'Should serialize array of bool correctly via InputObject' {
$bools = @($true, $false, $true)
$json = ConvertTo-Json -InputObject $bools -Compress
$json | Should -BeExactly '[true,false,true]'
}
It 'Should serialize array of Guid with Extended properties via Pipeline' {
$guids = @(
[Guid]'11111111-1111-1111-1111-111111111111',
[Guid]'22222222-2222-2222-2222-222222222222'
)
$json = $guids | ConvertTo-Json -Compress
$json | Should -BeExactly '[{"value":"11111111-1111-1111-1111-111111111111","Guid":"11111111-1111-1111-1111-111111111111"},{"value":"22222222-2222-2222-2222-222222222222","Guid":"22222222-2222-2222-2222-222222222222"}]'
}
It 'Should serialize array of enum correctly via Pipeline and InputObject' {
$enums = @([DayOfWeek]::Monday, [DayOfWeek]::Wednesday, [DayOfWeek]::Friday)
$jsonPipeline = $enums | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $enums -Compress
$jsonPipeline | Should -BeExactly '[1,3,5]'
$jsonInputObject | Should -BeExactly '[1,3,5]'
}
It 'Should serialize array of enum as strings with -EnumsAsStrings' {
$enums = @([DayOfWeek]::Monday, [DayOfWeek]::Wednesday, [DayOfWeek]::Friday)
$json = $enums | ConvertTo-Json -Compress -EnumsAsStrings
$json | Should -BeExactly '["Monday","Wednesday","Friday"]'
}
# Note: mixed array test uses InputObject only due to $true singleton issue
It 'Should serialize mixed type array correctly via InputObject' {
$mixed = @(1, 'two', $true, 3.14)
$json = ConvertTo-Json -InputObject $mixed -Compress
$json | Should -BeExactly '[1,"two",true,3.14]'
}
It 'Should serialize array with null elements correctly' {
$arr = @(1, $null, 'three')
$json = $arr | ConvertTo-Json -Compress
$json | Should -BeExactly '[1,null,"three"]'
}
It 'Should include ETS properties on array via InputObject' {
$arr = @(1, 2, 3)
$arr = Add-Member -InputObject $arr -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = ConvertTo-Json -InputObject $arr -Compress
$json | Should -BeExactly '{"value":[1,2,3],"MyProp":"test"}'
}
}
Context 'Scalars as values in hashtables and PSCustomObject' {
It 'Should serialize hashtable with scalar values correctly via Pipeline and InputObject' {
$hash = [ordered]@{
intVal = 42
strVal = 'hello'
boolVal = $true
doubleVal = 3.14
nullVal = $null
}
$expected = '{"intVal":42,"strVal":"hello","boolVal":true,"doubleVal":3.14,"nullVal":null}'
$jsonPipeline = $hash | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $hash -Compress
$jsonPipeline | Should -BeExactly $expected
$jsonInputObject | Should -BeExactly $expected
}
It 'Should serialize PSCustomObject with scalar values correctly via Pipeline and InputObject' {
$obj = [PSCustomObject]@{
intVal = 42
strVal = 'hello'
boolVal = $true
doubleVal = 3.14
}
$expected = '{"intVal":42,"strVal":"hello","boolVal":true,"doubleVal":3.14}'
$jsonPipeline = $obj | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $obj -Compress
$jsonPipeline | Should -BeExactly $expected
$jsonInputObject | Should -BeExactly $expected
}
It 'Should serialize hashtable with <TypeName> value correctly' -TestCases @(
@{ TypeName = 'DateTime'; Value = [DateTime]::new(2024, 6, 15, 10, 30, 0, [DateTimeKind]::Utc); Expected = '{"val":"2024-06-15T10:30:00Z"}' }
@{ TypeName = 'Guid'; Value = [Guid]'12345678-1234-1234-1234-123456789abc'; Expected = '{"val":"12345678-1234-1234-1234-123456789abc"}' }
@{ TypeName = 'Enum'; Value = [DayOfWeek]::Monday; Expected = '{"val":1}' }
@{ TypeName = 'Uri'; Value = [Uri]'https://example.com'; Expected = '{"val":"https://example.com"}' }
@{ TypeName = 'BigInteger'; Value = 18446744073709551615n; Expected = '{"val":18446744073709551615}' }
) {
param($TypeName, $Value, $Expected)
$hash = @{ val = $Value }
$jsonPipeline = $hash | ConvertTo-Json -Compress
$jsonInputObject = ConvertTo-Json -InputObject $hash -Compress
$jsonPipeline | Should -BeExactly $Expected
$jsonInputObject | Should -BeExactly $Expected
}
It 'Should serialize hashtable with enum as string correctly' {
$hash = @{ day = [DayOfWeek]::Monday }
$json = $hash | ConvertTo-Json -Compress -EnumsAsStrings
$json | Should -BeExactly '{"day":"Monday"}'
}
It 'Should include ETS properties on hashtable via InputObject' {
$hash = @{ a = 1 }
$hash = Add-Member -InputObject $hash -MemberType NoteProperty -Name MyProp -Value 'test' -PassThru
$json = ConvertTo-Json -InputObject $hash -Compress
$json | Should -BeExactly '{"a":1,"MyProp":"test"}'
}
}
#endregion Comprehensive Scalar Type Tests (Phase 1)
}