Skip to content

ConvertTo-Json Honors JsonPropertyAttribute #6847

Closed

Description

In PowerShell v6, Convert(To\From)-Json uses Newtonsoft.Json. ConvertTo-Json acknowledges JsonIgnoreAttributes but does not honor JsonPropertyAttributes. have some objects that are decorated with these attributes and serialize differently between ASP.NET and PowerShell. I've had to make a hacked version of ConvertTo-Json to support these attributes. For me, it results in a difference in casing which causes my JavaScript code to fail.

I propose that the JSON cmdlets honor the JsonPropertyAttributes placed on properties.

Activity

  1. iSazonov commented on May 10, 2018

    @iSazonov
    Collaborator

    Adam Driscoll (@adamdriscoll) Thanks for you report! Please add simple repo steps.

  2. adamdriscoll commented on May 10, 2018

    @adamdriscoll
    ContributorAuthor
    PS C:\Program Files\PowerShell\6.0.1> Add-Type 'public class MyClass { [Newtonsoft.Json.JsonProperty("myProperty")]publi
    c string MyProperty {get;set;} }' -ReferencedAssemblies 'Newtonsoft.Json'
    WARNING: (1) : Assuming assembly reference 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'Newtonsoft.Json' matches identity 'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
    
    (1) : >>> public class MyClass { [Newtonsoft.Json.JsonProperty("myProperty")]public string MyProperty {get;set;} }
    
    WARNING: (1) : Assuming assembly reference 'System.Runtime, Version=4.0.20.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' used by 'Newtonsoft.Json' matches identity 'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' of 'System.Runtime', you may need to supply runtime policy
    
    (1) : >>> public class MyClass { [Newtonsoft.Json.JsonProperty("myProperty")]public string MyProperty {get;set;} }
    
    PS C:\Program Files\PowerShell\6.0.1> $Class = New-Object MyClass
    PS C:\Program Files\PowerShell\6.0.1> $Class | ConvertTo-Json
    {
      "MyProperty": null
    }
    PS C:\Program Files\PowerShell\6.0.1> [Newtonsoft.Json.JsonConvert]::SerializeObject($Class)
    {"myProperty":null}
    
  3. iSazonov commented on May 10, 2018

    @iSazonov
    Collaborator

    Mark Kraus (@markekraus) Could you please comment the Issue?

  4. markekraus commented on May 10, 2018

    @markekraus
    Contributor

    It looks like instead of letting Newtonsoft handle the serialization, we hit this code path

    private object ProcessCustomObject<T>(object o, int depth)

    Which essentially converts the object to a dictionary and striping it of the any useful JSON attributes.

    At this point, I'm already convinced the right direction for JSON is to just wrap Newtonsoft and stop using our own logic as well as make some of the newtonsoft features available. This probably needs to be new cmdlets. Because pretty much everything we do at this point is a breaking change in the current cmdlets.

    As for fixing this in the current cmdlets... I have some (bad) ideas such as re-inventing the wheel to inspect the attributes on the object properties.. Maybe before we make the object ditcionary, we test the object for Json related attributes and pass the object directly to NewtonSoft... but that has depth issues.

  5. iSazonov commented on May 11, 2018

    @iSazonov
    Collaborator

    Mark Kraus (@markekraus) Thanks!

    serialize differently between ASP.NET and PowerShell

    What does ASP.NET use? Newtonsoft?

  6. adamdriscoll commented on May 11, 2018

    @adamdriscoll
    ContributorAuthor

    Yeah, it uses NewtonSoft.

  7. adamdriscoll commented on May 11, 2018

    @adamdriscoll
    ContributorAuthor

    Mark Kraus (@markekraus) I agree this is a super breaking change so I'm not sure the best way to address it and I did make a hacked version of this that bypasses the ProcessCustomObject to produce the dictionary for particular types of objects. This alleviates the depth issue for me but isn't a good fix for PowerShell proper.

    I think it would be a little weird to have totally separate cmdlets that do the same thing with different underlying technology but idk if adding parameters to the existing cmdlets is the best option either.

    It's a tricky situation, I guess.

  8. markekraus commented on May 11, 2018

    @markekraus
    Contributor

    For clarity, the depth issues I was referring to are in regards to to the possible solution of inspecting the object for json attributes.. the question od how deep to inspect and how to detect looping etc..

    I'm not fond of having new cmdlets, but the way I see it working is that we'd add the new cmdlets, mark the old ones as deprecated (with warnings), then remove them 3 minor version later and adding the original cmdlets names as aliases to the new ones. In theory, the new cmdlets would "just work" in most cases, but obviously, there would be subtle differences.

  9. felixfbecker commented on Jun 15, 2018

    @felixfbecker
    Contributor

    Using NewtonSoft directly would be awesome because almost all API C# clients use it to decorate their attributes - in C#/PowerShell they will be PascalCased, but for JSON they usually need to be lowercased. So to support proper interop with APIs honoring JsonPropertyAttribute is crucial.

  10. iSazonov commented on Jun 16, 2018

    @iSazonov
    Collaborator

    Mark Kraus (@markekraus) What scenarios we break if we move to Newton serialization?
    Will this affect only Convert(To\From)-Json cmdlets?
    It would be great if you wrote RFC.
    This will allow us to move.
    We could enter a new parameter to support backward compatibility and not create new cmdlets.

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  14. added
    Resolution-No Activity
    on Nov 16, 2023
  15. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Resolution-No ActivityWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions