Repository navigation
Description
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.dllto have a reference to the largeSystem.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
FilipToth commentedon May 25, 2021
Well, in my opinion, dynamic makes it a lot cleaner to read/write DOM in applications where speed isn’t a major issue.
davidfowl commentedon May 25, 2021
cc @JamesNK
jkotas commentedon May 25, 2021
System.Linq.Expression is "archived component".
dynamicas it exist today is effectively deprecated tech. It is not good for new tech to take dependency on effectively deprecated tech.FilipToth commentedon May 25, 2021
@jkotas are there any plans to update it? Aren’t dynamics a good way to interop with non-statically typed languages?
jkotas commentedon May 25, 2021
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 heavySystem.Linq.Expressiondependency, but that is completely speculative at this point.More discussion on this is at dotnet/designs#163 (comment)
FilipToth commentedon May 25, 2021
Maybe in later dotnet versions then. Thanks.
steveharter commentedon May 25, 2021
@KrzysztofCwalina comments on supporting a better version of dynamic (with IntelliSense perhaps)?
davidfowl commentedon May 25, 2021
@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).
JamesNK commentedon May 25, 2021
My feedback is the same:
13 remaining items
KrzysztofCwalina commentedon Jun 16, 2021
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
KrzysztofCwalina commentedon Jun 16, 2021
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.
John0King commentedon Aug 11, 2021
@steveharter
what are being "archived" ? dynamic or System.Linq.Expression ?
jkotas commentedon Aug 12, 2021
@John0King Both dynamic (as it exists today) and System.Linq.Expression are archived components. See #27790 for the discussion.
John0King commentedon Aug 30, 2021
@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
.Joinmethod in Linq is not easy to use compare a.LeftJoin()or.RightJoinexpression, and With all async mode , there no async version ofToListAsync()ToArrayAsyncmethod.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!
jkotas commentedon Aug 30, 2021
We are not deleting any of these APIs. The existing code that uses these APIs can be moved to new .Net versions just fine.
John0King commentedon Aug 31, 2021
@jkotas I think "archived" means: ready for obsolete and deprecated
stephentoub commentedon Aug 31, 2021
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."
davidikin45 commentedon Sep 9, 2021
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)gregsdennis commentedon Sep 10, 2021
Yes, but it was concluded that
dynamicis the wrong way to support this as it subverts the type system.