Skip to content
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

deserializing to DataSet returns 0 tables #31795

Closed
bkbartje opened this issue Feb 5, 2020 · 3 comments
Closed

deserializing to DataSet returns 0 tables #31795

bkbartje opened this issue Feb 5, 2020 · 3 comments

Comments

@bkbartje
Copy link

bkbartje commented Feb 5, 2020

when I use this method
var ds = System.Text.Json.JsonSerializer.Deserialize(json);
the dataset has gets no tables and stays empty.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Feb 5, 2020
@vcsjones
Copy link
Member

vcsjones commented Feb 5, 2020

Related to https://github.com/dotnet/runtime/issues/29959#issuecomment-503876056. "DataTable and related types" are not supported in .NET Core 3. I would assume that since DataTable is not supported, then DataSet is not either.

@etghanibahman
Copy link

etghanibahman commented Feb 20, 2020

There are some kind of serializations that do not exist in system.text.json by default. you should add them manually , and for this there are two patterns : the basic pattern and the factory pattern
The one that you need is a basic pattern for Dataset. The following works well for me, I hope it helps you too.


public class DataSetConverter : JsonConverter<DataSet>
    `{`
        public override DataSet Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
        {
            using var jsonDoc = JsonDocument.ParseValue(ref reader);
            var rootElement = jsonDoc.RootElement;
            var dataSet = rootElement.JsonElementToDataSet();
            return dataSet;
        }
        public override void Write(Utf8JsonWriter jsonWriter, DataSet value, JsonSerializerOptions options)
        {
            jsonWriter.WriteStartArray();
            foreach (DataTable source in value.Tables)
            {
                foreach (DataRow dr in source.Rows)
                {
                    jsonWriter.WriteStartObject();
                    foreach (DataColumn col in source.Columns)
                    {
                        var key = col.ColumnName.Trim();
                        var valueString = dr[col].ToString();
                        switch (col.DataType.FullName)
                        {
                            case "System.Guid":
                                jsonWriter.WriteString(key, valueString);
                                break;
                            case "System.Char":
                            case "System.String":
                                jsonWriter.WriteString(key, valueString);
                                break;
                            case "System.Boolean":
                                Boolean.TryParse(valueString, out bool boolValue);
                                jsonWriter.WriteBoolean(key, boolValue);
                                break;
                            case "System.DateTime":
                                if (DateTime.TryParse(valueString, out DateTime dateValue))
                                {
                                    jsonWriter.WriteString(key, dateValue);
                                }
                                else
                                {
                                    jsonWriter.WriteString(key, "");
                                }
                                break;
                            case "System.TimeSpan":
                                if (DateTime.TryParse(valueString, out DateTime timeSpanValue))
                                {
                                    jsonWriter.WriteString(key, timeSpanValue.ToString());
                                }
                                else
                                {
                                    jsonWriter.WriteString(key, "");
                                }
                                break;
                            case "System.Byte":
                            case "System.SByte":
                            case "System.Decimal":
                            case "System.Double":
                            case "System.Single":
                            case "System.Int16":
                            case "System.Int32":
                            case "System.Int64":
                            case "System.UInt16":
                            case "System.UInt32":
                            case "System.UInt64":
                                if (long.TryParse(valueString, out long intValue))
                                {
                                    jsonWriter.WriteNumber(key, intValue);
                                }
                                else
                                {
                                    double.TryParse(valueString, out double doubleValue);
                                    jsonWriter.WriteNumber(key, doubleValue);
                                }
                                break;
                            default:
                                jsonWriter.WriteString(key, valueString);
                                break;
                        }
                    }
                    jsonWriter.WriteEndObject();
                }

            }
            jsonWriter.WriteEndArray();
        }
    }

@layomia
Copy link
Contributor

layomia commented Feb 21, 2020

@layomia layomia closed this as completed Feb 21, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Feb 21, 2020
@layomia layomia added this to the 5.0 milestone Feb 21, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants