winforms
winforms copied to clipboard
Add JSON Serialization Support for OLE
Related: https://github.com/dotnet/winforms/issues/11368
This is a start to support JSON serialization in OLE scenarios.
JsonData
: This is our wrapper class that holds the JSON serialized data along with the original type information. Note that the data that is being JSON serialized uses default JsonSerializer behavior. Users can follow the remarks of the public APIs if custom JSON serialization behavior is needed.
Public APIs:
-
DataObject.SetDataAsJson()
- Allows users to add data toDataObject
that will be JSON serialized. This is how we support JSON serialization for drag/drop scenario. -
Clipboard.SetDataAsJson()
- Allows users to conveniently JSON serialize a type onto the clipboard, but note that this does not acceptDataObject
being passed in. If users want to place aDataObject
on the clipboard, users should ensure data they want to be JSON serialized is already in theDataObject
by utilizingDataObject.SetDataAsJson
and then useClipboard.SetDataObject()
. -
Control.DoDragDropAsJson()
- Allows users to conveniently JSON serialize a type for drag operation, but note that this does not acceptDataObject
being passed in. If users wants to start a drag operation with aDataObject
, user should ensure data they want to be JSON serialized is already in theDataObject
by utilizingDataObject.SetDataAsJson
and then useControl.DoDragDrop()
Tests:
- Unit tests added for new APIs, some of the same tests also added in ComDisabledTests to ensure same behavior if built in com support is turned off.
- Manually tested cross process scenario -- drag/drop and copy/paste from 2 WinForms apps and confirmed able to retrieve the original data without exposing
JsonData<T>
- Manually tested copying data onto clipboard from app running on main branch and pasting into .NET 8 app for informational purposes. This scenario currently does not work as one may expect likely due to
JsonData
not existing in .NET 8. More investigation needs to be done here. - Test behavior in trimmed app:
JsonSerializer
uses reflection to serialize by default, which will not be available when trimming is turned on. We will need to either use source generator APIs to support trimming or configure JsonSerializerOptions.TypeInfoResolver