winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Add JSON Serialization Support for OLE

Open lonitra opened this issue 8 months ago • 2 comments

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 to DataObject 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 accept DataObject being passed in. If users want to place a DataObject on the clipboard, users should ensure data they want to be JSON serialized is already in the DataObject by utilizing DataObject.SetDataAsJson and then use Clipboard.SetDataObject().
  • Control.DoDragDropAsJson() - Allows users to conveniently JSON serialize a type for drag operation, but note that this does not accept DataObject being passed in. If users wants to start a drag operation with a DataObject, user should ensure data they want to be JSON serialized is already in the DataObject by utilizing DataObject.SetDataAsJson and then use Control.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
Microsoft Reviewers: Open in CodeFlow

lonitra avatar Jun 13 '24 03:06 lonitra