Skip to content

How useful is C# "dynamic" with System.Text.Json.Nodes.JsonObject? #53195

Closed

Description

@steveharter

Currently JsonObject supports C# "dynamic" which primarily means an instance of JsonObject can get\set a property value without having to use a string for the property name.

Although dynamic support has been requested, I believe it was primarily desired as a work-around since System.Text.Json didn't have a writeable DOM feature yet.

Since System.Text.Json now has a writable DOM through JsonNode without dynamic, is the dynamic capability redundant and should it be removed?

In theory, support for dynamic could make code sharing between C# and scripting languaged like JavaScript a bit easier, but what are other benefits are there?

Some negatives of dynamic:

  • Dynamic is slower.
  • There is no IntelliSense or type checking (also the case with non-dynamic cases).
  • The current implementation requires casts to the appropriate type or call to GetValue<T>(), so the copy-paste ability for JavaScript doesn't quite work.
  • Requires System.Text.Json.dll to have a reference to the large System.Linq.Expressions.dll, although the IL Linker removes that if dynamic is not used.
  • Doesn't help with arrays.

Here's a brief sample of dynamic and non-dynamic programming models:

const string Json = "{\"MyNumber\":42, \"MyArray\":[10,11]}";

// dynamic
{
    dynamic obj = JsonNode.Parse(Json);
    int number = (int)obj.MyNumber;
    Debug.Assert(number == 42);
    
    obj.MyString = "Hello";
    Debug.Assert((string)obj.MyString == "Hello");
}

// non-dynamic
{
    JsonObject obj = JsonNode.Parse(Json).AsObject();
    int number = (int)obj["MyNumber"];
    Debug.Assert(number == 42);
    
    obj["MyString"] = "Hello";
    Debug.Assert((string)obj["MyString"] == "Hello");
}

Note that properties on a strongly-typed POCO and collection elements can also be declared as dynamic, and the JsonSerializer can also support dynamic.

Activity

  1. added
    design-discussion
    on May 24, 2021
  2. added this to the 6.0.0 milestone on May 24, 2021
  3. self-assigned this
    on May 24, 2021
  4. ghost added
    untriaged
    on May 24, 2021
  5. FilipToth commented on May 25, 2021

    @FilipToth
    Contributor

    Well, in my opinion, dynamic makes it a lot cleaner to read/write DOM in applications where speed isn’t a major issue.

  6. davidfowl commented on May 25, 2021

    @davidfowl
    Member
  7. jkotas commented on May 25, 2021

    @jkotas
    Member

    Requires System.Text.Json.dll to have a reference to the large System.Linq.Expressions.dll, although the IL Linker removes that if dynamic is not used.

    System.Linq.Expression is "archived component". dynamic as it exist today is effectively deprecated tech. It is not good for new tech to take dependency on effectively deprecated tech.

  8. FilipToth commented on May 25, 2021

    @FilipToth
    Contributor

    @jkotas are there any plans to update it? Aren’t dynamics a good way to interop with non-statically typed languages?

  9. jkotas commented on May 25, 2021

    @jkotas
    Member

    I am not aware of any plans around better dynamic. I agree that it is attractive for certain scenarios and there may be a way to reinvent it to be simple, lean, fast, and without the problematic heavy System.Linq.Expression dependency, but that is completely speculative at this point.

    More discussion on this is at dotnet/designs#163 (comment)

  10. FilipToth commented on May 25, 2021

    @FilipToth
    Contributor

    Maybe in later dotnet versions then. Thanks.

  11. steveharter commented on May 25, 2021

    @steveharter
    ContributorAuthor

    @KrzysztofCwalina comments on supporting a better version of dynamic (with IntelliSense perhaps)?

  12. davidfowl commented on May 25, 2021

    @davidfowl
    Member

    @CyrusNajmabadi how crazy would it be to do something with embedded languages and JSON? Using the JSON to drive IntelliSense of the dynamic object being returned from these APIs? Of course it's easy when you have a JSON literal but I wonder if the schema for IntelliSense could come from outside of the C# type system and feed into it (I guess it's effectively F# type providers).

  13. JamesNK commented on May 25, 2021

    @JamesNK
    Member

    My feedback is the same:

    • I don't see many people using dynamic with Json.NET (however I don't go looking for this kind of information).
    • I don't think it is a hugely valuable feature.
    • I wouldn't recommend making dynamic a focus of how a JSON DOM is used and designing around it, but if you want to make it an option, and you're ok with the complexity that adds, then go ahead.
  14. 13 remaining items

  15. KrzysztofCwalina commented on Jun 16, 2021

    @KrzysztofCwalina
    Member

    Who's working on the "This potential newer flavor" of dynamic? I do think we need it at some point. It might be a syntactic sugar, but the current syntax obscures the user intent enough that our current plan for untyped APIs in the Azure SDK might not be viable without it. After all C# is just syntactic sugar for IL, and you would not enjoy coding in IL, would you? :-)

    cc: @MadsTorgersen

  16. KrzysztofCwalina commented on Jun 16, 2021

    @KrzysztofCwalina
    Member

    Having said that, I would totally prefer a new dynamic flavor. There is no reason to involve the DLR into what amounts to syntactic sugar over dictionary access. I would prefer if dynamic was implemented as a set of attributes applied to simple lookup and cast APIs.

  17. removed
    untriaged
    on Jul 9, 2021
  18. John0King commented on Aug 11, 2021

    @John0King

    @steveharter

    what are being "archived" ? dynamic or System.Linq.Expression ?

  19. jkotas commented on Aug 12, 2021

    @jkotas
    Member

    @John0King Both dynamic (as it exists today) and System.Linq.Expression are archived components. See #27790 for the discussion.

  20. John0King commented on Aug 30, 2021

    @John0King

    @jkotas Linq and Expression are largely used in many many place, such as EF Core, And I believe many people do not know about the talking even start, your team should have a announcement to the community that you are discussing about those , I find that any news in https://github.com/dotnet/announcements/issues be noticed by me is also too late, you already done the change and is waiting for feedback about the new change.

    In fact, today's .Join method in Linq is not easy to use compare a .LeftJoin() or .RightJoin expression, and With all async mode , there no async version of ToListAsync() ToArrayAsync method.
    and make those things archived, seems declare their death, but where's the replacement ?

    IMO, you can declare something should not be used , but there must be a replacement, otherwise, how can anyone who used those code and move forward together with the .Net version ? Please don't drop developers ! there no more developers of .net to drop anymore!

  21. jkotas commented on Aug 30, 2021

    @jkotas
    Member

    how can anyone who used those code and move forward together with the .Net version ?

    We are not deleting any of these APIs. The existing code that uses these APIs can be moved to new .Net versions just fine.

  22. John0King commented on Aug 31, 2021

    @John0King

    @jkotas I think "archived" means: ready for obsolete and deprecated

  23. stephentoub commented on Aug 31, 2021

    @stephentoub
    Member

    I think "archived" means: ready for obsolete and deprecated

    It does not. The meaning as we use it is spelled out in the README for the relevant library:
    https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq.Expressions/README.md
    "The library and supporting language features are mature and no longer evolving, and the risk of code change likely exceeds the benefit. We will consider changes that address significant bugs or regressions, or changes that are necessary to continue shipping the binaries. Other changes will be rejected."

  24. davidikin45 commented on Sep 9, 2021

    @davidikin45

    Wasn't the whole point of #29690 to allow WebAPI to accept untyped json payloads and access them easily. e.g model.Property1? Seems strange you would regress this functionality previously provided by Newtonsoft.Json

    [HttpPost] public async Task<IActionResult> SubmitAsync(dynamic model)

  25. gregsdennis commented on Sep 10, 2021

    @gregsdennis
    Contributor

    Yes, but it was concluded that dynamic is the wrong way to support this as it subverts the type system.

  26. ghost locked as resolved and limited conversation to collaborators on Oct 10, 2021
  27. unlocked this conversation on Mar 22, 2022
  28. ghost locked as resolved and limited conversation to collaborators on Apr 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Text.Jsondesign-discussion

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions