Skip to content

Cosmos: Modernize our JSON serialization/deserialization #34567

Closed

Description

@roji

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.

Activity

  1. added this to the Backlog milestone on Aug 29, 2024
  2. self-assigned this
    on Aug 29, 2024
  3. removed their assignment
    on Aug 31, 2024
  4. AndriySvyryd commented on Sep 14, 2024

    @AndriySvyryd
    Member

    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.

    Yes and #14570

  5. added theissue type on Jun 15, 2025
  6. JoasE commented on Dec 20, 2025

    @JoasE
    Contributor

    @roji

    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

  7. roji commented on Dec 20, 2025

    @roji
    MemberAuthor

    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.

  8. mstfzhoseini-sketch commented on Feb 21, 2026

    @mstfzhoseini-sketch
  9. roji commented on Feb 21, 2026

    @roji
    Author
  10. 12 remaining items

  11. JoasE commented on Apr 28, 2026

    @JoasE
    Contributor

    @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.

  12. roji commented on Apr 28, 2026

    @roji
    MemberAuthor

    @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?

  13. JoasE commented on Apr 28, 2026

    @JoasE
    Contributor

    @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.

  14. AndriySvyryd commented on Jul 15, 2026

    @AndriySvyryd
    Member

    @JoasE What is still missing for this that's not tracked by other issues?

  15. JoasE commented on Jul 16, 2026

    @JoasE
    Contributor

    @AndriySvyryd I'm working on tying this into the query pipeline, which I'm hoping to complete by early next week

  16. modified the milestones: Backlog, 11.0.0 on Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions