powershell - How do I correctly serialize a PSObject with a DateTime into JSON? -
lets have psobject
integer , date want serialize json , have recipients able deserialize it. in javascript following json:
json.stringify({date: new date(), number: 23}) "{"date":"2014-09-10t14:11:27.092z","number":23}" json.parse(json.stringify({date: new date(), number: 23})) >> object {date: "2014-09-10t14:13:28.950z", number: 23}
that looks pretty standard json can interpreted correctly javascript.
however, appears powershell's convertto-json
inconsistent datetime
serialization can't convert , forth correctly. illustrate this:
ps c:\dev> $testobj = new-object -typename psobject -property @{ date = get-date; number = 23; } ps c:\dev> $testobj number date ------ ---- 23 9/10/2014 9:52:14 ps c:\dev\git\strawman> $testobj | convertto-json { "number": 23, "date": { "value": "\/date(1410357134361)\/", "displayhint": 2, "datetime": "wednesday, september 10, 2014 9:52:14 am" } } ps c:\dev> $testobj | convertto-json | convertfrom-json number date ------ ---- 23 @{value=9/10/2014 1:52:14 pm; displayhint=2; datetime=wednesday, september 10, 2014 9:52:14 am}
not json produces date property not valid @ , can't interpreted correctly javascript, powershell's convertfrom-json
can't interpret correctly give same looking object back.
is there way correctly serialize psobject
datetime
valid json can correctly deserialized recipients?
i ended writing function convert psobject psobject can converted cleaner json:
<# .synopsis creates new psobject properties of original object not able serialized json converted value can converted json. includes following types: * datetime conducts deep property search .example convert custom psobject have parsable dates in json $customobject = new-object -typename psobject -property @{ date = get-date; number = 23; innerdate = new-object -typename psobject -property @{date=get-date;} } ## bad json ps c:\dev> $customobject | convertto-json { "date": { "value": "\/date(1410372629047)\/", "displayhint": 2, "datetime": "wednesday, september 10, 2014 2:10:29 pm" }, "number": 23, "innerdate": { "date": { "value": "\/date(1410372629047)\/", "displayhint": 2, "datetime": "wednesday, september 10, 2014 2:10:29 pm" } } } ## json ps c:\dev> $customobject | convertto-jsonifiablepsobject | convertto-json { "date": "2014-09-10t14:10:29.0477785-04:00", "number": 23, "innerdate": { "date": "2014-09-10t14:10:29.0477785-04:00" } } #> function convertto-jsonifiablepsobject { param ( [parameter(mandatory=$true, valuefrompipeline=$true)] [psobject]$object ) $newobjectproperties = @{} foreach ($property in $object.psobject.properties) { $value = $property.value if ($property.typenameofvalue -eq "system.management.automation.pscustomobject") { $value = convertto-jsonifiablepsobject -object $property.value } elseif ($property.typenameofvalue -eq "system.datetime") { $value = $property.value.tostring("yyyy'-'mm'-'dd't'hh':'mm':'ss.fffffffk") } $newobjectproperties[$property.name] = $value } return new-object -typename psobject -property $newobjectproperties }
Comments
Post a Comment