[00:00:06] Hi and welcome to this talk on system [00:00:08] text Jason in . [00:00:10] My name is Eric and I'm a principal [00:00:12] engineer at the document libraries team [00:00:14] Microsoft. [00:00:17] Let's jump straight in. [00:00:19] So this year's themes for STJ has been [00:00:22] support for the new language features, [00:00:25] unions and closed hierarchies, [00:00:27] addressing some of the source generator [00:00:29] and native pain points that many of you [00:00:32] might have had, [00:00:34] providing more expressive annotations [00:00:35] for types [00:00:37] and making a whole lot of reliability [00:00:39] fixes. [00:00:41] Just to give you some high-level [00:00:43] numbers, [00:00:45] this year we merged 60 PRs. 17 of them [00:00:48] were adding new features or new APIs. 16 [00:00:52] of them were making source generator and [00:00:54] IoT improvements. [00:00:55] Eight were performance improvements, 19 [00:00:58] were bug fixes. [00:01:02] So let us get started with union types. [00:01:05] I'm assuming that you've already watched [00:01:08] Matt's presentation on union types. So [00:01:10] I'm not going to spend a lot of time [00:01:11] explaining the concept within the [00:01:14] language itself. I'm just going to deep [00:01:17] dive into [00:01:19] C [00:01:20] text Jason interoperability. [00:01:23] So for the [00:01:25] for the purposes of introduction, I've [00:01:27] defined a very simple union type [00:01:30] called it either. It contains either an [00:01:32] integer, a string, or a sample goal that [00:01:35] I haven't bothered defining. [00:01:39] When it comes to serialization, [00:01:41] it works very straightforward. [00:01:45] You know, C sharp allows implicit [00:01:47] conversions from the individual [00:01:49] constituent values into instances of the [00:01:52] union. [00:01:53] And so correspondingly, when you try to [00:01:56] serialize them with STJ, [00:01:58] uh it basically recognizes union [00:02:00] and produces the corresponding [00:02:02] underlying JSON serialization value for [00:02:04] each of those cases. [00:02:06] If you serialize as an integer, [00:02:08] you'll get an integer integer. If you [00:02:10] serialize a string, you'll get a JSON [00:02:12] string. [00:02:13] If you serialize a POJO, uh you'll get a [00:02:15] JSON object. No wrappers, [00:02:17] no discriminators, nothing, just the [00:02:20] actual underlying value. [00:02:24] For most cases, the same applies for [00:02:26] union deserialization. [00:02:28] Uh [00:02:29] in this case, if I attempt to [00:02:31] deserialize this union payload, which [00:02:33] contains polymorphic values, so again, [00:02:36] an integer, a string, and a POJO, [00:02:38] if I try to deserialize it as an array [00:02:40] of that union type, deserialization is [00:02:43] going to succeed. [00:02:45] Uh and if I introspect each of the [00:02:47] deserialized values, I will get [00:02:49] union cases corresponding to the [00:02:51] underlying value. So STJ [00:02:53] uh uses the JSON value type to determine [00:02:56] what the union case it binds to. [00:02:59] And this is something we want to keep in [00:03:01] mind for [00:03:02] the next section, [00:03:04] uh which is some certain restrictions [00:03:06] that [00:03:07] uh STJ has when it uh interacts with [00:03:09] unions. [00:03:11] Now, System.Text.Json is conservative by [00:03:13] default [00:03:14] when inferring target types. It only [00:03:16] looks at what the type of the underlying [00:03:18] JSON value is in the payload. [00:03:21] And primarily, this is to prevent [00:03:22] security issues associated with [00:03:24] unmitigated polymorphic deserialization. [00:03:27] This is a known issue. Uh we've [00:03:29] restricted similarly polymorphic [00:03:30] deserialization. The same happens with [00:03:32] unions. [00:03:34] Um [00:03:35] if more than one union case cases [00:03:38] correspond to the JSON type, [00:03:40] then uh there might be errors. [00:03:43] And just to give you a very basic [00:03:44] example, I've defined here a union cut [00:03:47] type called date or guid. [00:03:50] Um [00:03:51] both guid and date only serializes JSON [00:03:54] strings. [00:03:55] So if I try to serialize instances of [00:03:57] that union, [00:03:59] uh I get a string and the operation [00:04:01] works. [00:04:02] Uh the same not the case if I try to [00:04:05] deserialize a bunny. [00:04:06] And if, for example, I try to give a [00:04:09] valid JSON representation, which in this [00:04:11] case is [00:04:12] a uh date value, deserialization is [00:04:15] actually going to fail. [00:04:16] And the error message mentions that, you [00:04:19] know, the JSON binding type string is [00:04:20] ambiguous for type date or grid. [00:04:23] And the basic reason is that both date [00:04:26] and grid, like I said, they both use the [00:04:29] type string. [00:04:32] So this might be a problem depending on [00:04:34] how you model your use. [00:04:36] And the way that STJ tries to solve this [00:04:39] is by introducing a new type of [00:04:41] abstraction. We call that abstraction [00:04:43] type classic buckets. [00:04:46] I won't get too much into the [00:04:48] nitty-gritty of how type classifiers [00:04:50] work, but I will just say that they kind [00:04:53] of work like JSON converters. They're [00:04:54] this type of general purpose abstraction [00:04:58] that basically let users introspect the [00:05:01] JSON payload and make a decision as to [00:05:03] what [00:05:04] type they want deserialization to bind [00:05:06] to. [00:05:07] It's an abstraction that's generic. It [00:05:09] works both for unions and polymorphic [00:05:11] serialization, but for the purposes of [00:05:13] this presentation, I'm going to show you [00:05:15] how it works in the context of unions. [00:05:18] Without spending too much time on what [00:05:21] this code is doing, I'm just going to [00:05:22] say that [00:05:23] we're registering the classifier with [00:05:26] the previously defined union. [00:05:28] And then uh if you look at the actual [00:05:31] crux of the code that's been defined [00:05:32] here, you will see that it is actually [00:05:35] performing content sniffing on the [00:05:37] underlying payload. [00:05:38] If it determines that the value [00:05:40] successfully parses as date only, then [00:05:42] it returns a type indicating that, hey, [00:05:45] you should be deserializing this as a [00:05:47] date only. Correspondingly, if it parses [00:05:49] a grid, [00:05:50] otherwise it throws an exception. [00:05:53] So that is kind of the basic structure [00:05:54] which should give you an idea of how [00:05:56] this works. [00:05:58] Now there exist certain cases [00:06:01] where you would like to get structural [00:06:03] classification for free. From many ways [00:06:05] in which you could do structural [00:06:06] classification, but for certain very [00:06:09] basic scenarios, for example, where you [00:06:11] want to define union between [00:06:14] as is the case with this example where [00:06:16] I'm defining [00:06:17] a union between a record called dog and [00:06:20] another record called cat. [00:06:22] Assuming that there is sufficient [00:06:24] variation in the actual properties being [00:06:26] defined, then you might be able to use [00:06:28] the built-in structural classifier that [00:06:31] distinguishes between [00:06:33] to values. [00:06:35] Uh it that classification works solely [00:06:37] on the basis of what properties are [00:06:39] defined and does not look recursively to [00:06:40] actual values of properties. [00:06:44] And just to show you how this works in [00:06:46] action, assuming the previously defined [00:06:49] animal union, [00:06:50] uh if I give it a JSON string, uh which [00:06:53] is an array containing a couple of [00:06:54] values, [00:06:56] if we try to deserialize this, we will [00:06:58] uh observe that it actually picks [00:07:01] the correct uh union type based on [00:07:04] whatever properties are being uh [00:07:06] I uh specified in the analog. [00:07:10] Uh [00:07:11] one thing I should note here, however, [00:07:13] is that [00:07:14] uh while this might be essential uh for [00:07:16] certain schemas, uh [00:07:19] it could end up being kind of fragile, [00:07:21] especially for example if, you know, the [00:07:24] type dog [00:07:25] uh admits a property called lives or if [00:07:27] cat admits a property called age, [00:07:29] then uh again this type of structural [00:07:32] classification becomes ambiguous and [00:07:34] break. So use this with caution. [00:07:41] Let's now talk about closed classes, [00:07:42] which is another C Sharp 15 feature. [00:07:44] This is a sibling feature to union [00:07:46] types. It very much attempts to address [00:07:49] the exact same issue of exhausted [00:07:52] matching and correspondingly [00:07:55] has built-in some functionality catering [00:07:56] to closed boxes. [00:08:00] Uh [00:08:01] just to describe the core scenario, [00:08:02] imagine that you're modeling some kind [00:08:05] of event type [00:08:06] and you want to be able to serialize it [00:08:08] with polymorphism. And in the olden [00:08:11] days, the way that you which you would [00:08:13] achieve this [00:08:14] would be to explicitly add a JSON [00:08:17] derived type annotation for each of the [00:08:19] derived types and also specify the name [00:08:23] for the type discriminator for the [00:08:24] individual derived types. And this [00:08:26] boilerplate would only grow as your [00:08:28] model grows. [00:08:30] That is effectively essential for the [00:08:34] case of regular classes because they [00:08:36] they do employ open hierarchies, which [00:08:38] means that the enumeration of all the [00:08:40] derived types is basically undecided [00:08:43] undecidable at compile time. [00:08:48] This changes with closed classes. In the [00:08:51] case of closed classes, [00:08:54] the list of derived types [00:08:56] by necessity [00:08:58] is design board compile time. It's a [00:09:00] list of things that can only be [00:09:02] specified at the current compilation. If [00:09:05] closed classes cannot be derived outside [00:09:08] of the compilation. [00:09:09] And libraries like STJ can use this [00:09:11] feature to basically infer [00:09:14] the derived types being used [00:09:16] for a specific polymorphic type. So if I [00:09:19] just changed the abstract keyword into [00:09:21] closed in my card event, [00:09:23] then [00:09:24] I only need to specify this new inferred [00:09:27] closed type polymorphism property [00:09:29] and set it to true. [00:09:30] And this will be effectively identical [00:09:33] or equivalent to me specifying each of [00:09:35] these [00:09:36] explicit derived type attribute [00:09:38] annotations to just do this and I rely [00:09:40] on the compiler giving me a list of all [00:09:43] the things that I need to support. [00:09:46] So with that, I would like to move on to [00:09:48] a discussion about whether users should [00:09:50] be preferring union types or closed [00:09:52] classes when modeling their [00:09:56] when when modeling their JSON contracts. [00:09:59] And I would say that [00:10:01] you should always prefer closed [00:10:02] hierarchies when modeling trucks [00:10:05] if you have that option. [00:10:07] Closed classes allow type [00:10:08] discriminators, [00:10:10] uh types do not and that is by [00:10:12] construction and that is not [00:10:14] overridable. [00:10:16] Um type discriminators [00:10:18] essentially make schemas more stable [00:10:20] over time. [00:10:21] Uh [00:10:23] it's it's very safe to evolve schemas. [00:10:26] Uh that is not the case with structural [00:10:28] pacification. Structural pacification is [00:10:30] brittle by definition. [00:10:32] So I would suggest that you only use [00:10:35] unions if required by the schema. If you [00:10:37] don't own the schema, [00:10:38] if you have some sort of interrupt [00:10:40] scenario with you know TypeScript style [00:10:42] unions, then absolutely use unions. But [00:10:45] if you have the choice, [00:10:47] then always go for closed classes. You [00:10:49] get the exact same exhaustive [00:10:51] guarantees. [00:10:52] Uh just go with closed classes. [00:10:58] Without I'm going to talk about a few [00:11:00] source generator improvements that we've [00:11:02] been making. [00:11:03] Um the reflection based serializer in [00:11:05] STJ has had support for private member [00:11:08] access for a while via the JSON [00:11:10] constructor attribute or the JSON food [00:11:12] attribute. [00:11:14] These were not being supported by the [00:11:16] source generator. You would get a [00:11:17] diagnostic about this, but generally [00:11:19] speaking [00:11:21] uh this has been creating a deficit when [00:11:23] it comes to functionality. [00:11:25] Uh starting with .NET 11, STJ does bring [00:11:28] support for private member access. It [00:11:30] does so efficiently in modern runtimes [00:11:32] using unsafe accessors. In older [00:11:34] runtimes, it still has able [00:11:36] compatibility mode where it uses [00:11:37] reflection to make the exact same [00:11:39] accesses. [00:11:41] Uh mind you that reflection is always [00:11:43] being done in an safe way. [00:11:45] So so this basically bridges the [00:11:47] functional gap when it comes to private [00:11:49] member access. [00:11:51] Uh it's also bringing conditional in in [00:11:53] it property assignment. [00:11:55] Uh this has always been an issue because [00:11:57] of how it is properties worked. [00:11:58] Uh they were always being set regardless [00:12:01] of the fact of whether [00:12:02] uh a corresponding property existed in a [00:12:04] JSON payload, which often [00:12:07] resulted in data being raised because we [00:12:10] were uh assigning an unit property with [00:12:13] a default value. [00:12:14] This has now been addressed also using [00:12:16] unsafe accessors. [00:12:19] We also permit for partial JSON [00:12:20] serializer context declarations so you [00:12:22] can split [00:12:23] your big context class into multiple [00:12:25] locations. We We've added support for [00:12:28] members, i.e., types of properties that [00:12:30] have been annotated [00:12:31] with the [00:12:33] and we've also added support for generic [00:12:36] converter annotations, [00:12:37] which is a feature intended to improve [00:12:41] AOT support or rather supersede [00:12:44] the non-AOT supporting JSON converter [00:12:46] attribute. [00:12:48] And I'm going to dive a bit deeper with [00:12:50] the final bullet, [00:12:52] generic converter annotations. [00:12:54] Let's consider the following scenario. [00:12:57] Suppose you define [00:12:59] a record [00:13:00] or any POJO that is generic. [00:13:03] And for that particular generic type, [00:13:05] you also need to specify a custom [00:13:07] converter. [00:13:08] Okay, cool. So you define a generic [00:13:10] converter. Let's call it POJO converter [00:13:12] of T. I haven't included it here for [00:13:14] space reasons, but you know, suppose you [00:13:16] have done so. [00:13:18] Is this enough? [00:13:20] No. With older versions of SNJ, you had [00:13:22] to also define a JSON converter factory [00:13:25] where you'd have to define this obscene [00:13:28] code, [00:13:29] which uses reflection that unfortunately [00:13:32] does not work in [00:13:34] So in most scenarios, if you wanted to [00:13:36] support custom converters for generic [00:13:38] types and you also wanted to support [00:13:40] AOT, you'd typically be out of luck. [00:13:43] So this has always been a problem. [00:13:46] Starting with .NET 11, [00:13:49] we've made made it possible to work [00:13:50] around this issue by allowing generic [00:13:52] converter annotations. It basically [00:13:54] means that [00:13:55] if you have a generic type, [00:13:57] you can define a generic converter for [00:13:59] that type and then use [00:14:01] this open declaration on the JSON [00:14:04] converter annotations and the source [00:14:06] generator will make sure to fill in the [00:14:08] right generic parameter. [00:14:10] It goes without saying that an important [00:14:13] requirement for this feature to work [00:14:15] is that the generic type parameters [00:14:18] might, must [00:14:19] exactly match the generic type parameter [00:14:21] of [00:14:23] the Poco itself. If you don't do that, [00:14:25] you'll get a compiler [00:14:26] or you will get a runtime error if you [00:14:28] try to do the same thing in reflection. [00:14:30] So here you get a much simpler [00:14:33] workaround for JSON converter factory [00:14:35] that works in .NET DTO and results in is [00:14:39] much safer to use and uses much less [00:14:42] boilerplate. [00:14:46] Moving on, we've added a bunch of new [00:14:48] attribute annotations when modeling our [00:14:50] Poco contracts. [00:14:53] One feature that STJ has had for a while [00:14:56] is support for custom name policies. [00:14:59] These were historically being specified [00:15:01] at the JSON serializer options layer, [00:15:03] i.e. at runtime. [00:15:05] This effectively meant that what policy [00:15:08] was used for a specific Poco was very [00:15:11] sensitive and subject to user [00:15:13] configuration and this often resulted in [00:15:16] breaks. [00:15:17] So [00:15:19] .NET 11 introduces the [00:15:21] JSON naming policy attribute which [00:15:23] effectively applies a durable naming [00:15:25] policy that isn't sensitive to runtime [00:15:28] JSON serializer options configuration. [00:15:32] In this example that I'm showing here, [00:15:33] you can see that this can be applied [00:15:35] either at the type level. So this [00:15:37] effectively always forces camel case [00:15:40] conversion to all the properties scoped [00:15:42] within that particular type definition, [00:15:44] but it also supports property level [00:15:46] overrides. So for this case, I've [00:15:49] actually mandated that hey, [00:15:51] you should be using kebab case for this [00:15:53] particular property [00:15:55] and you know, so it works. [00:16:00] Similarly, [00:16:02] the JSON ignore attribute has existed [00:16:04] since the beginning of STJ, but it only [00:16:07] worked for individual property [00:16:09] annotations. [00:16:10] We now support type level JSON ignore [00:16:12] conditions. So if for example you want [00:16:15] as a policy to specify that [00:16:17] uh values should be ignored, [00:16:20] uh you can just add this type level [00:16:21] annotation and skip all null values [00:16:24] without needing to copy and paste this [00:16:26] long piece of code in every property. [00:16:32] Finally, as a feature roundup, I'm just [00:16:34] going to quickly mention some of the [00:16:36] other things that we're bringing in with [00:16:38] .NET 11. [00:16:39] Our support for JSON lines [00:16:40] serialization. Uh we've had JSON lines [00:16:43] deserialization support for a while now. [00:16:45] Uh this is now bringing serialization [00:16:48] support so your ASP.NET server can [00:16:50] support JSON lines if necessary. [00:16:53] Uh we've added for F Sharp users uh [00:16:55] built-in support for discriminated [00:16:57] unions. [00:16:58] Uh one thing to note about unions is [00:17:00] that they do not support the source [00:17:01] generator. Uh they only work when using [00:17:04] reflection. [00:17:07] We've added support for generic [00:17:09] polymorphic hierarchies. Effectively, [00:17:11] this works exact same way as the generic [00:17:14] JSON converters that I mentioned before. [00:17:16] Uh it basically means you can apply open [00:17:19] generics in your JSON derived attribute [00:17:22] annotations and it should just work with [00:17:24] some asterisks. [00:17:26] We've added Pascal case naming policy. [00:17:29] We've added strongly typed get type info [00:17:30] of team methods which is particularly [00:17:32] useful uh if you want to, for example, [00:17:35] in libraries serialize in a safe manner. [00:17:39] And we've added out of the box support [00:17:41] for all the numeric types and collection [00:17:43] types that we've added to .NET 11. [00:17:48] With that, I'm just going to give you [00:17:50] some resources uh that you can uh feed [00:17:53] into your agents uh if you want to uh [00:17:56] make use of the new features. [00:17:58] Uh [00:17:59] for for starters, just look at the [00:18:02] what's new in .NET 11 libraries document [00:18:04] at learn.microsoft. It's a very valuable [00:18:06] resource. [00:18:08] And for unions in particular, uh all of [00:18:10] this guidance that I mentioned before [00:18:12] has been encapsulated in this document. [00:18:14] Please do give it a read. [00:18:17] And with that, I would like to thank you [00:18:18] for attending.