Powershell: Expose ConvertToJson as an API

Created on 25 Oct 2018  路  7Comments  路  Source: PowerShell/PowerShell

We have ConvertFromJson exposed as an API, but not ConvertToJson.

If we had both, folks that are writing apps that host PowerShell could access Convert*Json via the API rather than needing to use a runspace!

I think we could simply convert what's in ConvertTo-Json to C# and put it in this JsonObjectclass and then have ConvertTo-Json just call that API.

Area-Cmdlets-Utility Committee-Reviewed Issue-Enhancement Resolution-Fixed

All 7 comments

For new public API we need PowerShell Committee approval. /cc @SteveL-MSFT

I would rather see work done to create new API's that more closely wrap NewtonSoft JSON and mark the existing ones as deprecated. If you are in C# and want to do Object-to-JSON serialization, NewtonSoft is already available and much better in so many ways than our ConvertTo-Json plumbing.

The existing JsonObject.ConvertFromJson and the proposing JsonObject.ConvertToJson are for converting to/from JSON in the PowerShell context, which means it honors the ETS and PowerShell specified type conversions.

@daxian-dbw I understand that. What I'm saying is rather than expose the existing implementation, I would rather see work that replaces the existing implementation and finds some way to more closely wrap NewtonSoft, then expose that as an API. We have a bunch of open issues about lack of support for various things that come natural for NewtonSoft. I'm not discounting ETS, we obviously need to support it. But exposing an API for an implementation we know to be flawed is not my preferred path.

So I think what you describe is reasonable... but I think that's really a separate issue - "make Convert*Json better by aligning closer to what NewtonSoft does". And of course this issue is just - have another way to call ConvertTo-Json.

I think a good first step of the eventual goal of what you describe is to move all the logic from the cmdlets into the API. Then the cmdlets would just call those APIs.

From there, we can refactor the logic behind those cmdlets to make them better.

@PowerShell/powershell-committee reviewed this and agrees to exposing ConvertToJson public api for the purpose of conversion in PowerShell context

For information - I see that CoreFX team started and actively develop System.Text.Json. I expect we'll have this in .Net Core 3.0.

Was this page helpful?
0 / 5 - 0 ratings