You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our Cosmos deserialization is currently based on using Newtonsoft.Json to deserialize to DOM JTokens, which are then e.g. processed by our shapers. We should take a look at the entire approach here and decide what to do; at the very least, we should consider moving from Newtonsoft.Json to System.Text.Json - using Newtonsoft.Json notably means that deserialization happens synchronously (its JsonSerializer doesn't support async).
To improve performance, we could go all the way and stop serializing/deserializing to DOM types altogether, and use low-level parsing directly from UTF8, like we already do in relational.
To improve performance, we could go all the way and stop serializing/deserializing to DOM types altogether, and use low-level parsing directly from UTF8, like we already do in relational.
To improve performance, we could go all the way and stop serializing/deserializing to DOM types altogether, and use low-level parsing directly from UTF8, like we already do in relational.
What about generating a type at query compile and/or model finalizing -time that represents the returned json structure from cosmos? Then EF could leverage JsonSerializer.DeserializeAsync to use streaming deserialization, instead of buffering the returned json string.
using Newtonsoft.Json notably means that deserialization happens synchronously
On that note, and on JsonSerializer.DeserializeAsync: The cosmos db SDK currently gives back a MemoryStream, even for GetItemQueryStreamIterator and ReadItemStreamAsync (which EF currently uses)
See: Azure/azure-cosmos-dotnet-v3#5438
What about generating a type at query compile and/or model finalizing -time that represents the returned json structure from cosmos?
That would mean we'd need to then copy all the data again from that generated type into the actual user type - that would be somewhat similar to what's happening today (with the custom type replacing the DOM types), and wouldn't necessarily be a big improvement. In addition, we're trying to (slowly) move away from techniques which are fundamentally incompatible with NativeAOT, like type generation (note that we don't do type generation anyway else in EF - that's another step in the "wrong" direction beyond what we're already doing).
The point in this issue is to drop down to a lower level, and to use Utf8JsonReader directly - which is what JsonSerializer.DeserializeAsync does under the hood. This is how the relational JSON deserialization already works. I do have some other more extreme ideas which may allow us to leverage JsonSerializer.DeserializeAsync (rather than dropping down to the low-level Utf8JsonReader), but these would mean propagating EF's model into the S.T.Json model of things (and possibly also vice versa) - the feasibility of this is currently unclear.
@roji@AndriySvyryd The more I get into the materialization work for this, the more I think it makes sense to do #34067 first. Implementing this without proper projection binding seems to require more (and more complex) code than adapting the current materializer to support proper projection binding, and either one is going to overwrite the other.
Would you be okay with me switching to #34067 at this point? I believe I can put together a quick solution, even if it’s not the cleanest, given that it would be short-lived as this work will then overwrite the changes right after.
@JoasE I'm fine with whatever way you prefer to do this... Though if you say you'd do #34067, only to overwrite the changes right after, maybe better to just to both at the same time?
@roji Ok yeah, that makes more sense! I was assuming you'd want to split up the effort to prevent growing the pr too much, but if you are ok with that combining it will be more efficient.
Activity
AndriySvyryd commentedon Sep 14, 2024
Yes and #14570
JoasE commentedon Dec 20, 2025
@roji
What about generating a type at query compile and/or model finalizing -time that represents the returned json structure from cosmos? Then EF could leverage
JsonSerializer.DeserializeAsyncto use streaming deserialization, instead of buffering the returned json string.On that note, and on
JsonSerializer.DeserializeAsync: The cosmos db SDK currently gives back a MemoryStream, even for GetItemQueryStreamIterator and ReadItemStreamAsync (which EF currently uses)See: Azure/azure-cosmos-dotnet-v3#5438
roji commentedon Dec 20, 2025
That would mean we'd need to then copy all the data again from that generated type into the actual user type - that would be somewhat similar to what's happening today (with the custom type replacing the DOM types), and wouldn't necessarily be a big improvement. In addition, we're trying to (slowly) move away from techniques which are fundamentally incompatible with NativeAOT, like type generation (note that we don't do type generation anyway else in EF - that's another step in the "wrong" direction beyond what we're already doing).
The point in this issue is to drop down to a lower level, and to use Utf8JsonReader directly - which is what
JsonSerializer.DeserializeAsyncdoes under the hood. This is how the relational JSON deserialization already works. I do have some other more extreme ideas which may allow us to leverageJsonSerializer.DeserializeAsync(rather than dropping down to the low-level Utf8JsonReader), but these would mean propagating EF's model into the S.T.Json model of things (and possibly also vice versa) - the feasibility of this is currently unclear.mstfzhoseini-sketch commentedon Feb 21, 2026
roji commentedon Feb 21, 2026
12 remaining items
JoasE commentedon Apr 28, 2026
@roji @AndriySvyryd The more I get into the materialization work for this, the more I think it makes sense to do #34067 first. Implementing this without proper projection binding seems to require more (and more complex) code than adapting the current materializer to support proper projection binding, and either one is going to overwrite the other.
Would you be okay with me switching to #34067 at this point? I believe I can put together a quick solution, even if it’s not the cleanest, given that it would be short-lived as this work will then overwrite the changes right after.
roji commentedon Apr 28, 2026
@JoasE I'm fine with whatever way you prefer to do this... Though if you say you'd do #34067, only to overwrite the changes right after, maybe better to just to both at the same time?
JoasE commentedon Apr 28, 2026
@roji Ok yeah, that makes more sense! I was assuming you'd want to split up the effort to prevent growing the pr too much, but if you are ok with that combining it will be more efficient.
Merge branch 'feature/dotnet#34067-ProjectSubDocuments' into feature/d…
Merge branch 'main' of https://github.com/dotnet/efcore into feature/d…
Merge branch 'main' of https://github.com/dotnet/efcore into feature/d…
Merge branch 'main' of https://github.com/dotnet/efcore into feature/d…
Merge branch 'main' of https://github.com/dotnet/efcore into feature/d…
Cosmos: Modernize materializer & subdocument projectionbinding (#38550)
AndriySvyryd commentedon Jul 15, 2026
@JoasE What is still missing for this that's not tracked by other issues?
JoasE commentedon Jul 16, 2026
@AndriySvyryd I'm working on tying this into the query pipeline, which I'm hoping to complete by early next week