-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JsonSerializer.Parse<DataTable> failed #21366
Comments
cc @steveharter |
Thanks, because NewtonSoft.Json support this feature, would be awesome if |
We ran into this issues as well, and being stubborn (& lazy ... since I did not read all the docs first) ... I decided to trudge through and forcefully and painstakingly remove JSON.Net from our 2.2 to 3.1 migration. In the end we created a I have not tested it extensively and I am sure there are dozens of other better examples out there, but this was my first attempt and is currently working in our situation. Maybe this can be a stepping stone for others instead of just (1) go back to Json.Net or (2) wait for Net 5! I'm sure I will refactor this a dozen times before it goes to production but constructive comments are always helpful. If it is not useful (or lacking in any area), then please disregard this post (or admins can delete it!).
Edited as Suggested (below):
I reuse Happy coding! |
From a quick glance, I would recommend one adjustment that caught my eye. Remove all the calls to For example: - jsonWriter.WriteString(JsonEncodedText.Encode(key), JsonEncodedText.Encode(valueString));
+ jsonWriter.WriteString(key, valueString); |
@ahsonkhan Yeah, that is simple. For some reason early on I thought it was required, so just copied and pasted it there! Thanks! |
@AlonCG The main problem is when you are going to read serialized data and turn it to dataset. |
@etghanibahman, yeah ... I basically just do the reverse, hence the "..." in the Read function. This needs some work, since I don't like the double enumeration and could use some optimization (and I am not sure it is even a good idea, but I needed reusable bits)... but again, this could just be used as a stepping stone for others that are stuck and need a bit of a boost?
JsonElementToDataTable Extension and support functions (could use some work for sure!)
While this works for us, we have not done extensive testing on it, at all. It was just implemented to get around the issue of not having serialization for DataTables. So instead of waiting for Core 5 or going back to Json.Net (hindsight being 20/20), this is what we got. |
Thank you, it works really well in my case. |
@AlonCG Basically there is a severe need to have a serializer for dataset, I have made some amendments in your solution to have it. Fro example I have made some changes for column of type datetime or timespans, because based on my experience in our current project, in case we have no data for mentioned types, the code that you have written will cease to work.
|
From @bkbartje in dotnet/runtime#31795
|
From @chenyj796 in dotnet/runtime#41920:
|
We recently published guidance against (de)serializing the Those who need this functionality can write custom converters as shown in examples provided above. We'll work on providing guidance on this in the JSON documentation, including an example of how to implement the functionality safely, if possible. |
Moving this to the dotnet/docs repo as this is now considered an issue we should provide security guidance, documentation, and possibly a workaround for. cc @tdykstra |
This issue has been closed as part of the issue backlog grooming process outlined in #22351. That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the |
@layomia is there no option to simply ignore unsafe types? If not, isn't that a bug? |
How do we hook in to the processor so that we can just ignore these types? |
To confirm, this is still indeed an issue.... .net 5's system.text.json still cannot handle a datatable. |
STJ will treat any unknown type that does not implement Here's an example: class Program
{
static void Main(string[] args)
{
JsonSerializerOptions options = new();
options.Converters.Add(new UnsupportedType<DataTable>());
DataTable dt = JsonSerializer.Deserialize<DataTable>("{}", options); // throws NotSupportedException
}
}
internal class UnsupportedType<T> : JsonConverter<T>
{
public override T? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotSupportedException($"{typeof(T)} does not support deserialization.");
}
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
{
throw new NotSupportedException($"{typeof(T)} does not support serialization.");
}
} |
I quite disappointed... could someone give me the implementation for doing this. its 2023, newtonsoft has this for like how long? cant even find a DataTableJsonConverter : JsonConverter which works.. quite sad |
Same problem here. |
Whew, this issue started in NET CORE 3.0, and has now reached .NET 9 ! |
Typical Microsoft style; if you look at the WinUI3 UI designer posts, you will see the worse. |
Convert an object into json string and then convert into DataTable is an easy way to generate
DataTable
, but this approach fails indotnet core 3 preview5
I have created a working dotnet core 2 code snippet at https://dotnetfiddle.net/cD6NFD
dotnetfiddle doesn't support dotnet core 3 yet, so my dotnet core 3 code example see below:
below are the error log:
The text was updated successfully, but these errors were encountered: