-
Notifications
You must be signed in to change notification settings - Fork 478
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 Microsoft.AdaptiveExpressions.Core, fork of AdaptiveExpressions on System.Text.Json (and AOT compatible) #6783
Merged
tracyboehrer
merged 15 commits into
microsoft:main
from
jevansaks:adaptiveexpressions-on-systemtextjson
Jun 14, 2024
Merged
Add Microsoft.AdaptiveExpressions.Core, fork of AdaptiveExpressions on System.Text.Json (and AOT compatible) #6783
tracyboehrer
merged 15 commits into
microsoft:main
from
jevansaks:adaptiveexpressions-on-systemtextjson
Jun 14, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fcbcb92
to
ec35e51
Compare
jevansaks
commented
May 13, 2024
jevansaks
commented
May 13, 2024
This reverts commit 72a11ac.
jevansaks
commented
May 16, 2024
tracyboehrer
approved these changes
Jun 13, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #6791
AdaptiveExpressions on System.Text.Json overview
To make AdaptiveExpressions library work with AOT compilation, it was necessary to migrate from Newtonsoft to System.Text.Json. This has user-visible API and implementation differences.
After discussing with @tracyboehrer we agreed to fork AdaptiveExpressions and produce a parallel nuget package for callers to opt in to that uses System.Text.Json.
Expanded IMemory interface and constrained expression return types
The expression system calls out to the IMemory object to get and set values into a user-provided object graph. It is expected that TryGetValue returns only primitive types like number, string. But TryGetValue can also return lists or other objects. In many cases those objects are opaque and are just passed back into IMemory.SetValue or returned back to the caller, but in other cases the expression engine used to try to manipulate the objects or serialize them to json. Because all such methods would require reflection, those responsibilities are now delegated back to IMemory and the expression engine only understands primitive types, a small number of List types (
IList
andList<object>
), and System.Text.Json types (e.g.JsonArray
andJsonObject
).In some cases the expression engine will create a list (e.g. if you foreach over an object), and in this case the expression will create a
List<object>
and the user-implemented IMemory is expected to handle this externally-created object as well.Added JsonNodeMemory
For callers that don't want to implement IMemory, the easiest way to migrate to AOT is to use JsonObjects to store the data you want to evaluate expressions against and wrap them in JsonNodeMemory to pass in to evaluation functions. You can also use JsonSerializer.SerializeToNode to serialize an existing non-json object into JsonNode in an AOT-compatible way.
Many methods now take JsonTypeInfo
System.Text.Json supports AOT by not relying on reflection, and instead the link between a type and its converter is via
JsonTypeInfo
. You get one of these using the STJ source generator.Implementation within and APIs on AdaptiveExpressions that would have needed to do json serialization on unknown types now are marked as
[RequiresDynamicCode]
and[RequiresUnreferencedCode]
and there are overloads of those methods and types that take JsonTypeInfo which can be used instead.Testing AOT mode
It's difficult to publish & test component in AOT compilation mode, so for testing components in AOT mode we turn on AOT warnings and ensure that the tests and product code are AOT-warning free to convince ourselves that the component will behave correctly when compiled as AOT with trimming on.
Microsoft.AdaptiveExpressions.Core.Tests are using some of the AOT patterns but still test SimpleObjectMemory paths and other non-AOT compatible paths. Microsoft.AdaptiveExpressions.Core.AOT.Tests is a copy of most of the tests from Microsoft.AdaptiveExpressions.Core.Tests but rewritten to be going through exclusively AOT-compatible modes. Functionally this means only testing against the JsonNodeMemory backing implementation and thus some of the round-trip tests aren't particularly interesting but they remain to show others how to convert such code in the future.