Title: How do we customize serialization? · Issue #3173 · PowerShell/PowerShell · GitHub
Open Graph Title: How do we customize serialization? · Issue #3173 · PowerShell/PowerShell
X Title: How do we customize serialization? · Issue #3173 · PowerShell/PowerShell
Description: There was a PowerShell Team blog post years ago about how serialization works and how it should be possible to customize -- and of course, now I have access to all the source code, but so far I haven't been able to make extending the ser...
Open Graph Description: There was a PowerShell Team blog post years ago about how serialization works and how it should be possible to customize -- and of course, now I have access to all the source code, but so far I hav...
X Description: There was a PowerShell Team blog post years ago about how serialization works and how it should be possible to customize -- and of course, now I have access to all the source code, but so far I hav...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/3173
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"How do we customize serialization?","articleBody":"There was a [PowerShell Team blog post years ago](https://blogs.msdn.microsoft.com/powershell/2010/01/07/how-objects-are-sent-to-and-from-remote-sessions/) about how serialization works and how it should be possible to customize -- and of course, now I have access to all the source code, but so far I haven't been able to make extending the serialization work. If someone can help me figure this out, I'll make sure we get a HowTo document for future module authors out of it ...\r\n\r\nI'm going to use a simple example, but remember that I'm looking for the way to make Deserialization work **properly** -- I don't want to hear about all the workarounds for this _particular_ case (there are several). The real problem I'm trying to solve is more complicated.\r\n\r\nI need to call a remote function which takes a DateTimeOffset object, Unlike DateTime, DateTimeOffest isn't treated as a primitive type (although it should be #3172), so I want to fix that in my module.\r\n\r\n## Here's what I tried:\r\n\r\n```posh\r\nusing namespace System.Management.Automation\r\n\r\nclass DateTimeOffsetDeserializer : System.Management.Automation.PSTypeConverter {\r\n\r\n [bool] CanConvertFrom([object]$sourceValue, [Type]$destinationType)\r\n {\r\n Write-Warning \"CanConvertFrom $SourceValue to $($destinationType.FullName)\"\r\n return ([PSObject]$sourceValue).PSTypeNames.Contains(\"Deserialized.System.DateTimeOffset\")\r\n }\r\n\r\n [object] ConvertFrom([object]$sourceValue, [Type]$destinationType, [IFormatProvider]$formatProvider, [bool]$ignoreCase)\r\n {\r\n Write-Warning \"ConvertFrom $SourceValue to $($destinationType.FullName)\"\r\n [PSObject]$psSourceValue = $sourceValue\r\n return [DateTimeOffset]::new($psSourceValue.Ticks, $psSourceValue.Offset)\r\n }\r\n\r\n [bool] CanConvertTo([object]$sourceValue, [Type]$destinationType)\r\n {\r\n Write-Warning \"CanConvertTo $SourceValue to $($destinationType.FullName)\"\r\n return ([PSObject]$sourceValue).PSTypeNames.Contains(\"Deserialized.System.DateTimeOffset\") -and $destinationType -eq \"System.DateTimeOffset\"\r\n }\r\n\r\n [object] ConvertTo([object]$sourceValue, [Type]$destinationType, [IFormatProvider]$formatProvider, [bool]$ignoreCase)\r\n {\r\n Write-Warning \"ConvertTo $SourceValue to $($destinationType.FullName)\"\r\n [PSObject]$psSourceValue = $sourceValue\r\n return [DateTimeOffset]::new($psSourceValue.Ticks, $psSourceValue.Offset)\r\n }\r\n}\r\n\r\n# This should be enough, right?\r\nUpdate-TypeData -TypeName 'DateTimeOffset' -TargetTypeForDeserialization 'DateTimeOffsetDeserializer' -Force\r\n\r\n# I tried adding all the rest of these\r\nUpdate-TypeData -TypeName 'Deserialized.System.DateTimeOffset' -TargetTypeForDeserialization 'DateTimeOffsetDeserializer' -Force\r\nUpdate-TypeData -TypeName 'DateTimeOffset' -MemberName 'TargetTypeForDeserialization' -MemberType 'NoteProperty' -Value 'DateTimeOffsetDeserializer'\r\nUpdate-TypeData -TypeName 'Deserialized.System.DateTimeOffset' -MemberName 'TargetTypeForDeserialization' -MemberType 'NoteProperty' -Value 'DateTimeOffsetDeserializer'\r\n```\r\n\r\n\r\n### But nothing makes the this come out as a DateTimeOffset\r\n```posh\r\nusing namespace System.Management.Automation\r\n[PSSerializer]::Deserialize( [PSSerializer]::Serialize([DateTimeOffset]::Now) ).PSTypeNames\r\n```\r\n\r\n### Or make this not be \"Deserialized\"\r\n```posh\r\nInvoke-Command -ComputerName localhost { [DateTimeOffset]::Now } | % PSTypeNames\r\n```\r\n\r\n### Or make this run without crashing:\r\n```posh\r\nInvoke-Command -ComputerName localhost { param([DateTimeOffset]$Time) $Time.LocalDateTime } -Args ([DateTimeOffset]::Now)\r\n```\r\n\r\nI also tried creating the `DateTimeOffsetDeserializer` class in C#, but I think I'm misunderstanding that original blog post or something about how the registration works...","author":{"url":"https://github.com/Jaykul","@type":"Person","name":"Jaykul"},"datePublished":"2017-02-20T00:45:08Z","url":"https://github.com/PowerShell/PowerShell/issues/3173"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f73ebe7e-b0af-e19a-0d90-ab816f843d23 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D110:1B5150:3E66:53CC:6AFDD4E1 |
| html-safe-nonce | 955d3e1c3d1d410cce322139ceea3795e976ac4b09dc362f57508fc97464c7cc |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMTEwOjFCNTE1MDozRTY2OjUzQ0M6NkFGREQ0RTEiLCJ2aXNpdG9yX2lkIjoiMzQ3OTg2NjcwMzU1MzA5MDc4NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1396a6ad3857cd20df223139a065fd319911923694800c8ed5891578884e0c36 |
| hovercard-subject-tag | issue:208760958 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/3173/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7847ad5854f5f61e261ec0513b93b5d6a987dd2411e3f18583d88e7093fbe805/PowerShell/PowerShell/issues/3173 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7847ad5854f5f61e261ec0513b93b5d6a987dd2411e3f18583d88e7093fbe805/PowerShell/PowerShell/issues/3173 |
| og:image:alt | There was a PowerShell Team blog post years ago about how serialization works and how it should be possible to customize -- and of course, now I have access to all the source code, but so far I hav... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Jaykul |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9918bd4004419458bcd838065ac2a76b76ebd11705d73909fff5ab99e996fa23 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a231c174493a6a442c0b0544475c918b30128208 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width