core icon indicating copy to clipboard operation
core copied to clipboard

System.Text.Json is a waste of time.

Open ghost opened this issue 7 years ago • 11 comments

I don't get why the .NET Core Json module (System.Text.Json) is modelled after the old Xml DOM pattern with objects like JDocument, JElement and so on. Of course, the complete absence of samples ref makes it impossible to see if there is a better way around.

If I was doing this, I would simply merge the hugely popular (and only used?) Newtonsoft.Json project into .NET Core itself. That way, you don't need to redo your documentation, you don't need to write new sample code, you don't need to a heck of a lot of things. The best thing would be that millions of developers who are currently using Newtonsoft.Json don't need to do anything other than change namespaces maybe!

Why is Microsoft over-complicating things?

ghost avatar May 28 '19 08:05 ghost

and only used?

This is hugely opinionated because problems with Newtonsoft.Json are well known and plenty. Performance is not that good too.

On the other hand, there are a bunch of popular serializers:

  • https://github.com/neuecc/Utf8Json
  • https://github.com/fsharp/FSharp.Data

Szer avatar May 28 '19 10:05 Szer

@Szer : ".NET Core" by itself is a hugely opinionated library. So yes, my post was opinionated.

Both the problems you have referenced are not even "issues". The first one is by design and the second one can be changed by a simple option flag. I use the library (Newtonsoft.Json) quite heavily across multiple projects, even enterprise and cloud-scale applications and have zero problems with it [I am vociferous getting attention to issue I face, you are free to Google for what I've said about that library!].

ghost avatar May 28 '19 11:05 ghost

System.Text.Json is still under construction. It is high-performance (no allocation) API. If you care primarily about dev usability, keep using other more user-friendly APIs like Newtonsoft.Json, etc.

cc @joshfree @ahsonkhan @steveharter

karelz avatar May 28 '19 16:05 karelz

I don't get why the .NET Core Json module (System.Text.Json) is modelled after the old Xml DOM pattern with objects like JDocument, JElement and so on. Of course, the complete absence of samples ref makes it impossible to see if there is a better way around.

If I was doing this, I would simply merge the hugely popular (and only used?) Newtonsoft.Json project into .NET Core itself. That way, you don't need to redo your documentation, you don't need to write new sample code, you don't need to a heck of a lot of things. The best thing would be that millions of developers who are currently using Newtonsoft.Json don't need to do anything other than change namespaces maybe!

Why is Microsoft over-complicating things?

You are making opinion when the thing was under implementation and not even releases 5 months back. When there is something available in the framework then it obviously have performance gain rather then using external api for the same functionality. .net core 3. is released less then couple of week ago. Let's see the performance gains

KamranShahid avatar Oct 07 '19 17:10 KamranShahid

and only used?

This is hugely opinionated because problems with Newtonsoft.Json are well known and plenty. Performance is not that good too.

On the other hand, there are a bunch of popular serializers:

  • https://github.com/neuecc/Utf8Json
  • https://github.com/fsharp/FSharp.Data

System.Text.Json is riddled with limitations. Just look at Microsoft's own documentation.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to?pivots=dotnet-5-0

Much of expected functionality is not offered out of the box. They offer workarounds, but a workaround is not a solution, it is a hack by definition. Some thing are just simply not possible with System.Net.Json. So more performance (which is usually meaningless since we are talking about milliseconds), but now I have to write my own converters? No thanks. Newtonsoft.json is still used more than any other json library available by over 10 fold, even in new .net core applications. There is something to be said about that. Yes, OP is posting an opinion, but it appears is the opinion held by the vast majority of the dev community, myself included.

jwilson-proag avatar May 24 '21 16:05 jwilson-proag

What i believe is in .net core 3 it was the first step to at-least use within framework implementation of simpler thing rather then using newtonsoft or other third party libraries. in 5.0 early feedbacks about shortcoming were address (as 5.0 is not LT release) and possibly expecting more improvement by the time of .net core 6.0 finalized. moreover writer of newtonsoft library was even full time associated with Microsoft around 2019 if i remember correctly. So things will improve as per new versions. it is true that lots of third party libraries using newtonsoft internally but they are also gradually moving to System.Text.Json only where ever possible.

KamranShahid avatar May 25 '21 18:05 KamranShahid

What i believe is in .net core 3 it was the first step to at-least use within framework implementation of simpler thing rather then using newtonsoft or other third party libraries. in 5.0 early feedbacks about shortcoming were address (as 5.0 is not LT release) and possibly expecting more improvement by the time of .net core 6.0 finalized. moreover writer of newtonsoft library was even full time associated with Microsoft around 2019 if i remember correctly. So things will improve as per new versions. it is true that lots of third party libraries using newtonsoft internally but they are also gradually moving to System.Text.Json only where ever possible.

Good call. I have been doing a little research into the topic and what I found is the major reasons for moving towards System.Text.Json is because Newtonsoft releases updates on a pretty regular schedule, much faster than Microsoft. This opens the door to runtime errors if there are version mismatches within the .net core framework and the version implemented within a given software. Microsoft implementing their own Json library removes that risk. Now users can upgrade there Newtonsoft libraries without risk of breaking the underlying .net core framework. For that reason, I think it is a good idea. I "mostly" retract my previous statement. I will still use Newtonsoft in most my applications simply because of the robustness of it, but I def understand now why Microsoft chose to go this direction.

jwilson-proag avatar May 25 '21 18:05 jwilson-proag

We removed Newtonsoft in favor of System.Text.Json in our products when we moved to .net 5. Sure, we had to implement some custom converters, but we get less allocations and more control. Can see why some would like to continue using Newtonsoft, but the move to System.Text.Json was an easy decision for us.

uffebjorklund avatar May 26 '21 05:05 uffebjorklund

We removed Newtonsoft in favor of System.Text.Json in our products when we moved to .net 5. Sure, we had to implement some custom converters, but we get less allocations and more control. Can see why some would like to continue using Newtonsoft, but the move to System.Text.Json was an easy decision for us.

same here

KamranShahid avatar Jun 24 '21 13:06 KamranShahid

We removed Newtonsoft in favor of System.Text.Json in our products when we moved to .net 5. Sure, we had to implement some custom converters, but we get less allocations and more control. Can see why some would like to continue using Newtonsoft, but the move to System.Text.Json was an easy decision for us.

Because it has too many limitations like it was motioned. Here is one simple limitation below, you can only specify one DefaultIgnoreCondition globally. Is this a limitation? of course.

Can you do this?

services.AddControllersWithViews().AddJsonOptions(opts =>
{
        opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
        opts.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault; //?
});

Nope, why not? they do not contradict and have many use cases.

Why have this limitation in the first place if they simply could make the JsonIgnoreCondition Enum marked with [Flag] attribute so it can be combined using bitwise operator OR ? Or provide other structure to accept multiple default conditions?

services.AddControllersWithViews().AddJsonOptions(opts =>
{
        opts.JsonSerializerOptions.DefaultIgnoreCondition = 
                  JsonIgnoreCondition.WhenWritingNull | JsonIgnoreCondition.WhenWritingDefault;
});

How about having an ignore condition for empty arrays? Should we write a converter for that? These are the basic everyday usage of JSON and these very commonly used functionality should be provided out of box. Converters can be written for very specific application use case not for ignoring empty arrays ect.

arman-g avatar Nov 14 '21 04:11 arman-g

Note that JsonIgnoreCondition.WhenWritingDefault implies WhenWritingNull for reference types. The default value for a reference type is null.

steveharter avatar Nov 16 '21 17:11 steveharter