understanding-json-schema icon indicating copy to clipboard operation
understanding-json-schema copied to clipboard

Type-specific keywords for PowerShell

Open Liturgist opened this issue 4 years ago • 9 comments

This might be a list of types for PowerShell. This was from version 5.1.17763.1490.

JavaScript  PowerShell
string      System.String
number      System.Int32/System.Decimal
object      System.Hashtable
array       System.Object[]
boolean     System.Boolean
null        $null

Liturgist avatar Nov 25 '20 17:11 Liturgist

I don't understand what you are requesting here or how it relates to this set of documentation?

karenetheridge avatar Nov 25 '20 19:11 karenetheridge

@karenetheridge @Liturgist is probably referring to this table which lists JSON type analogues in different languages. Unfortunately it currently only lists Python and Ruby. I see no reason we couldn't add PowerShell to the table. I think its current sparseness is merely due to waiting for people to contribute additional entires.

embray avatar Nov 25 '20 19:11 embray

@embray, yes, I was thinking that PowerShell could be added to that table.

Specifying an exponent produces a System.Double in PowerShell.

Liturgist avatar Nov 25 '20 19:11 Liturgist

Powershell types derive from .Net. While these are fine (and I might have a few edits if we choose move forward), the canonical way to deal with JSON now is with the System.Text.Json namespace, which supplies its own data models and serializer.

gregsdennis avatar Nov 25 '20 20:11 gregsdennis

Ah I see! Well, let me add some types for Perl:

JavaScript Perl
string scalar (SV)
number scalar (NV or IV), or Math::BigNum, Math::BigInt
object reftype=HASH
array reftype=ARRAY
boolean scalar or JSON::PP::Boolean
null scalar (undef)

karenetheridge avatar Nov 25 '20 21:11 karenetheridge

I see some differences between Windows PowerShell and PowerShell Core. Here is the code and output from each.

$json = @"
{
    "a_string": "now",
    "an_integer": 1,
    "a_float": 1.2,
    "an_exponent": 1.2E3,
    "an_object": {
        "1": "a",
        "2": "b"
    },
    "an_array": [1, 2, 3],
    "a_boolean": true,
    "a_null": null
}
"@

$p = $json | ConvertFrom-Json

$members = @(
    'a_string',
    'an_integer',
    'a_float',
    'an_exponent',
    'an_object',
    'an_array',
    'a_boolean'
)

foreach ($member in $members) {
    $($p.$member.GetType()) |
        Select-Object -Property @{name='Member';e={$member}},Name,BaseType
}

Windows PowerShell

PS C:\src\t> $PSVersionTable.PSVersion.ToString()
5.1.19041.610
PS C:\src\t> .\json-types.ps1

Member      Name           BaseType
------      ----           --------
a_string    String         System.Object
an_integer  Int32          System.ValueType
a_float     Decimal        System.ValueType
an_exponent Double         System.ValueType
an_object   PSCustomObject System.Object
an_array    Object[]       System.Array
a_boolean   Boolean        System.ValueType

PowerShell Core

PS C:\src\t> $PSVersionTable.PSVersion.ToString()
7.1.0-preview.3
PS C:\src\t> .\json-types.ps1

Member      Name           BaseType
------      ----           --------
a_string    String         System.Object
an_integer  Int64          System.ValueType
a_float     Double         System.ValueType
an_exponent Double         System.ValueType
an_object   PSCustomObject System.Object
an_array    Object[]       System.Array
a_boolean   Boolean        System.ValueType

Liturgist avatar Nov 26 '20 15:11 Liturgist

The differences between Windows PowerShell and PowerShell Core are discussed in the question and answer in:

https://stackoverflow.com/a/65040970/447901

Liturgist avatar Nov 27 '20 18:11 Liturgist

This code may be more helpful.

PS C:\src\t> Get-Content -Path .\json-types.ps1 $json = @" { "a_string": "now", "an_integer": 9, "int_min": $(string), "int_min_minus1": $(string - 1), "int_max": $(string), "int_max_plus1": $(string + 1), "int32_min": $(string), "int32_min_minus1": $(string - 1), "int32_max": $(string), "int32_max_plus1": $(string + 1), "int64_min": $(string), "int64_min_minus1": $(string - 1), "int64_max": $(string), "int64_max_plus1": $(string + 1), "a_float": 1.2, "an_exponent": 1.2E93, "an_object": { "1": "a", "2": "b" }, "an_array": [1, 2, 3], "a_boolean": true } "@

"a_null": null

"PowerShell " + $PSVersionTable.PSVersion.ToString() $json $p = $json | ConvertFrom-Json

foreach ($member in ($p.psobject.Members | Where-Object { $_.MemberType -eq 'NoteProperty' }).Name) { $p.$member.GetType() | Select-Object -Property @{name='Member';e={$member}},Name,BaseType } PS C:\src\t> .\json-types.ps1 PowerShell 5.1.19041.610 { "a_string": "now", "an_integer": 9, "int_min": -2147483648, "int_min_minus1": -2147483649, "int_max": 2147483647, "int_max_plus1": 21474836471, "int32_min": -2147483648, "int32_min_minus1": -2147483649, "int32_max": 2147483647, "int32_max_plus1": 21474836471, "int64_min": -9223372036854775808, "int64_min_minus1": -9.22337203685478E+18, "int64_max": 9223372036854775807, "int64_max_plus1": 92233720368547758071, "a_float": 1.2, "an_exponent": 1.2E93, "an_object": { "1": "a", "2": "b" }, "an_array": [1, 2, 3], "a_boolean": true }

Member Name BaseType


a_string String System.Object an_integer Int32 System.ValueType int_min Int32 System.ValueType int_min_minus1 Int64 System.ValueType int_max Int32 System.ValueType int_max_plus1 Int64 System.ValueType int32_min Int32 System.ValueType int32_min_minus1 Int64 System.ValueType int32_max Int32 System.ValueType int32_max_plus1 Int64 System.ValueType int64_min Int64 System.ValueType int64_min_minus1 Double System.ValueType int64_max Int64 System.ValueType int64_max_plus1 Decimal System.ValueType a_float Decimal System.ValueType an_exponent Double System.ValueType an_object PSCustomObject System.Object an_array Object[] System.Array a_boolean Boolean System.ValueType

PowerShell Core

Liturgist avatar Nov 28 '20 17:11 Liturgist

This code may be more helpful.

PS C:\src\t> Get-Content -Path .\json-types.ps1
$json = @"
{
    "a_string": "now",
    "an_integer": 9,
    "int_min": $([string]([int]::MinValue)),
    "int_min_minus1": $([string]([int]::MinValue) - 1),
    "int_max": $([string]([int]::MaxValue)),
    "int_max_plus1": $([string]([int]::MaxValue) + 1),
    "int32_min": $([string]([int32]::MinValue)),
    "int32_min_minus1": $([string]([int32]::MinValue) - 1),
    "int32_max": $([string]([int32]::MaxValue)),
    "int32_max_plus1": $([string]([int32]::MaxValue) + 1),
    "int64_min": $([string]([int64]::MinValue)),
    "int64_min_minus1": $([string]([int64]::MinValue) - 1),
    "int64_max": $([string]([int64]::MaxValue)),
    "int64_max_plus1": $([string]([int64]::MaxValue) + 1),
    "a_float": 1.2,
    "an_exponent": 1.2E93,
    "an_object": {
        "1": "a",
        "2": "b"
    },
    "an_array": [1, 2, 3],
    "a_boolean": true
}
"@
#    "a_null": null
"PowerShell " + $PSVersionTable.PSVersion.ToString()
$json
$p = $json | ConvertFrom-Json

foreach ($member in ($p.psobject.Members | Where-Object { $_.MemberType -eq 'NoteProperty' }).Name) {
    $p.$member.GetType() |
        Select-Object -Property @{name='Member';e={$member}},Name,BaseType
}
PS C:\src\t> .\json-types.ps1
PowerShell 5.1.19041.610
{
    "a_string": "now",
    "an_integer": 9,
    "int_min": -2147483648,
    "int_min_minus1": -2147483649,
    "int_max": 2147483647,
    "int_max_plus1": 21474836471,
    "int32_min": -2147483648,
    "int32_min_minus1": -2147483649,
    "int32_max": 2147483647,
    "int32_max_plus1": 21474836471,
    "int64_min": -9223372036854775808,
    "int64_min_minus1": -9.22337203685478E+18,
    "int64_max": 9223372036854775807,
    "int64_max_plus1": 92233720368547758071,
    "a_float": 1.2,
    "an_exponent": 1.2E93,
    "an_object": {
        "1": "a",
        "2": "b"
    },
    "an_array": [1, 2, 3],
    "a_boolean": true
}

Member           Name           BaseType
------           ----           --------
a_string         String         System.Object
an_integer       Int32          System.ValueType
int_min          Int32          System.ValueType
int_min_minus1   Int64          System.ValueType
int_max          Int32          System.ValueType
int_max_plus1    Int64          System.ValueType
int32_min        Int32          System.ValueType
int32_min_minus1 Int64          System.ValueType
int32_max        Int32          System.ValueType
int32_max_plus1  Int64          System.ValueType
int64_min        Int64          System.ValueType
int64_min_minus1 Double         System.ValueType
int64_max        Int64          System.ValueType
int64_max_plus1  Decimal        System.ValueType
a_float          Decimal        System.ValueType
an_exponent      Double         System.ValueType
an_object        PSCustomObject System.Object
an_array         Object[]       System.Array
a_boolean        Boolean        System.ValueType


PS C:\src\t> .\json-types.ps1
PowerShell - 7.1.0-preview.3
{
    "a_string": "now",
    "an_integer": 9,
    "int_min": -2147483648,
    "int_min_minus1": -2147483649,
    "int_max": 2147483647,
    "int_max_plus1": 21474836471,
    "int32_min": -2147483648,
    "int32_min_minus1": -2147483649,
    "int32_max": 2147483647,
    "int32_max_plus1": 21474836471,
    "int64_min": -9223372036854775808,
    "int64_min_minus1": -9.22337203685478E+18,
    "int64_max": 9223372036854775807,
    "int64_max_plus1": 92233720368547758071,
    "a_float": 1.2,
    "an_exponent": 1.2E93,
    "an_object": {
        "1": "a",
        "2": "b"
    },
    "an_array": [1, 2, 3],
    "a_boolean": true
}

Member           Name           BaseType
------           ----           --------
a_string         String         System.Object
an_integer       Int64          System.ValueType
int_min          Int64          System.ValueType
int_min_minus1   Int64          System.ValueType
int_max          Int64          System.ValueType
int_max_plus1    Int64          System.ValueType
int32_min        Int64          System.ValueType
int32_min_minus1 Int64          System.ValueType
int32_max        Int64          System.ValueType
int32_max_plus1  Int64          System.ValueType
int64_min        Int64          System.ValueType
int64_min_minus1 Double         System.ValueType
int64_max        Int64          System.ValueType
int64_max_plus1  BigInteger     System.ValueType
a_float          Double         System.ValueType
an_exponent      Double         System.ValueType
an_object        PSCustomObject System.Object
an_array         Object[]       System.Array
a_boolean        Boolean        System.ValueType

Liturgist avatar Nov 28 '20 17:11 Liturgist