Skip to content
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

Add JSON Serialization Support for OLE #11527

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from

Conversation

lonitra
Copy link
Member

@lonitra lonitra commented Jun 13, 2024

Related: #11368

This is a start to support JSON serialization in OLE scenarios.
JsonData<T>: 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().

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<T> 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 and others added 6 commits June 3, 2024 14:16
Include feature/* branches for PR builds (dotnet#11478)
merging main into the feature branch
Merge the latest main into the feature branch
Merge the latest main into the feature branch
Copy link

codecov bot commented Jun 14, 2024

Codecov Report

Attention: Patch coverage is 97.30640% with 8 lines in your changes missing coverage. Please review.

Project coverage is 74.78245%. Comparing base (f024a86) to head (eba27ab).
Report is 1 commits behind head on feature/clipboard.

Additional details and impacted files
@@                     Coverage Diff                     @@
##           feature/clipboard      #11527         +/-   ##
===========================================================
+ Coverage           74.74065%   74.78245%   +0.04179%     
===========================================================
  Files                   3045        3045                 
  Lines                 630626      630886        +260     
  Branches               46860       46874         +14     
===========================================================
+ Hits                  471334      471792        +458     
+ Misses                155932      155724        -208     
- Partials                3360        3370         +10     
Flag Coverage Δ
Debug 74.78245% <97.30640%> (+0.04179%) ⬆️
integration 17.95274% <6.55738%> (-0.00847%) ⬇️
production 47.84703% <90.16393%> (+0.08041%) ⬆️
test 96.98654% <99.15254%> (+0.00120%) ⬆️
unit 44.89268% <90.16393%> (+0.08380%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Copy link
Member

@JeremyKuhne JeremyKuhne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, some minor comments.

public static void WriteJsonData(Stream stream, IJsonData jsonData)
{
using BinaryFormatWriterScope writer = new(stream);
new BinaryLibrary(libraryId: 2, "System.Private.Windows.VirtualJson").Write(writer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put this string in the JsonData type as a constant with a comment.


public readonly string TypeFullName => $"{typeof(JsonData<T>).FullName}";

public readonly object GetRealObject(StreamingContext context) =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you don't technically need IObjectReference for our usages, the workaround for Clipboard versions that don't know of this type would be to copy this with IObjectReference. You can comment to that effect. Writing a test to capture this behavior would be good too.

The test should use the writer code to manually write a JsonData object to a stream, then you can read it with the deserializer and a binder that redirects to this actual type.

merge the latest into the clipboard branch
merging the latest main into clipboard feature branch
@elachlan
Copy link
Contributor

@willibrandon you might be interested in the clipboard work here.

@lonitra lonitra changed the base branch from feature/clipboard to feature/10.0 July 30, 2024 20:13
@lonitra lonitra changed the base branch from feature/10.0 to main August 15, 2024 00:59
Tanya-Solyanik added a commit to Tanya-Solyanik/winforms that referenced this pull request Sep 4, 2024
…ot forcing preview language version which is not supported by VB

Use fluent assertions in ClipboardProxyTests  - copied from Loni's PR dotnet#11527 for future merging.
@Tanya-Solyanik Tanya-Solyanik mentioned this pull request Sep 4, 2024
Tanya-Solyanik added a commit that referenced this pull request Sep 5, 2024
Enable simple creation of VB test projects in this repo solution by not forcing preview language version which is not  supported by VB
Use fluent assertions in ClipboardProxyTests  - copied from Loni's PR #11527 for future merging.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
draft draft PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants