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 Microsoft.AdaptiveExpressions.Core, fork of AdaptiveExpressions on System.Text.Json (and AOT compatible) #6783

Merged

Conversation

jevansaks
Copy link
Member

@jevansaks jevansaks commented May 7, 2024

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 and List<object>), and System.Text.Json types (e.g. JsonArray and JsonObject).

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.

@jevansaks jevansaks force-pushed the adaptiveexpressions-on-systemtextjson branch from fcbcb92 to ec35e51 Compare May 13, 2024 21:13
@jevansaks jevansaks changed the title Refactor AdaptiveExpressions to use System.Text.Json Add AdaptiveExpressionsSTJ, fork of AdaptiveExpressions on System.Text.Json (and AOT compatible) May 13, 2024
@tracyboehrer tracyboehrer added the Automation: No parity PR does not need to be applied to other languages. label May 13, 2024
@jevansaks jevansaks marked this pull request as ready for review May 13, 2024 22:02
@jevansaks jevansaks requested a review from a team as a code owner May 13, 2024 22:02
@jevansaks jevansaks changed the title Add AdaptiveExpressionsSTJ, fork of AdaptiveExpressions on System.Text.Json (and AOT compatible) Add Microsoft.AdaptiveExpressions.Core, fork of AdaptiveExpressions on System.Text.Json (and AOT compatible) May 30, 2024
@tracyboehrer tracyboehrer merged commit 78d95ce into microsoft:main Jun 14, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Automation: No parity PR does not need to be applied to other languages.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

AdaptiveExpressions: AOT and System.Text.Json
2 participants