-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow obtaining original string value from ConvertFrom-Json returned DateTime #16976
Comments
Maybe this concept should be applied to other types the cmdlet converts from strings as well, rather than making |
Please repro steps and input file example. |
Agreed, DateTime was the first to get me in trouble so that's why I singled it out. |
$json = "[{`"id`": `"2022-03-09T09:11:04.254630+00:00`", `"otherData`": true}]"
$converted = ConvertFrom-Json $json
$converted[0].id | Get-Date -Format o As can be seen, the input string is I want to highlight that I don't want to obtain a |
I see ConvertFrom-Json converts the string to local datetime. It is how underlying NewtonSoft.NET works. So we can not change this. You have to do some manual steps to convert local datetime to string you need. |
Is it not possible to somehow disable the automatic conversions and keep all strings as strings? Because this seems as a flaw to me (that should be perhaps fixed in NewtonSoft.NET), destructively converting data without an option to get the original back. Edit: I found this Stack Overflow question: Json.NET Disable the deserialization on DateTime. Would it not possible to pass such options? |
No, it will be huge breaking change. Is your source string always in UTC? If yes, you could use an workaround otherwise it is a dead end. |
Why would it be a breaking change? And no, the source string is not always following a certain format. |
The cmdlet will return different result.
In the case you could pre-process the json so that "hide/mask" the datetime string. |
There could be a |
This is definitely possible to do without a breaking change. You can have an opt in parameter like PowerShell already PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/JsonObject.cs Lines 181 to 187 in 7dc4587
ConvertFrom-Json more closely to Newtonsoft making it harder to potentially migrate to another JSON library in the future (if that's ever going to happen).
The current $json = '"2022-03-09T09:11:04.25463+05:00"'
$converted = ConvertFrom-Json $json
$converted
# Wednesday, 9 March 2022 2:11:04 pm
$converted.Kind
# Local I have no way to know with this deserialized value that the raw string had a |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
Reopening this for WG to review as I do think this is a bug as there shouldn't be a conversion unless the user requests one, which may deviate from current experience but would be reasonable to change going forward. |
Summary of the new feature / enhancement
As a user I want to be able to access the original string value of a converted DateTime so that it can be used in cases where an equivalent ISO 8601 representation would be considered a different value.
I am using an API which returns ISO 8601 strings as IDs. However, PowerShell automatically converts those IDs to DateTime, and using something like
Get-Date -Format o $date
returns a different string that results in a different, invalid ID.Proposed technical implementation details (optional)
There are two ways I can see this implemented:
-NoDateConversion
flag toConvert-FromJson
and cmdlets that use it such asInvoke-RestMethod
OriginalString
member, so that the original string can be accessed like$date.OriginalString
Get-Date -Format o $date
, would output the original string instead of a ISO 8601 equivalent reinterpretation.The text was updated successfully, but these errors were encountered: