Converts a DataTable to and from JSON.
Inheritance: JsonConverter
        /// <summary>
        /// Reads the JSON representation of the object.
        /// </summary>
        /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="existingValue">The existing value of object being read.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // handle typed datasets
            DataSet ds = (objectType == typeof(DataSet))
                ? new DataSet()
                : (DataSet)Activator.CreateInstance(objectType);

            DataTableConverter converter = new DataTableConverter();

            reader.Read();

            while (reader.TokenType == JsonToken.PropertyName)
            {
                DataTable dt = ds.Tables[(string)reader.Value];
                bool exists = (dt != null);

                dt = (DataTable)converter.ReadJson(reader, typeof(DataTable), dt, serializer);

                if (!exists)
                    ds.Tables.Add(dt);

                reader.Read();
            }

            return ds;
        }
Beispiel #2
0
 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
   DataSet dataSet = new DataSet();
   DataTableConverter dataTableConverter = new DataTableConverter();
   reader.Read();
   while (reader.TokenType == JsonToken.PropertyName)
   {
     DataTable table = (DataTable) dataTableConverter.ReadJson(reader, typeof (DataTable), (object) null, serializer);
     dataSet.Tables.Add(table);
     reader.Read();
   }
   return (object) dataSet;
 }
Beispiel #3
0
 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
 {
   DataSet dataSet = (DataSet) value;
   DefaultContractResolver contractResolver = serializer.ContractResolver as DefaultContractResolver;
   DataTableConverter dataTableConverter = new DataTableConverter();
   writer.WriteStartObject();
   foreach (DataTable dataTable in (InternalDataCollectionBase) dataSet.Tables)
   {
     writer.WritePropertyName(contractResolver != null ? contractResolver.GetResolvedPropertyName(dataTable.TableName) : dataTable.TableName);
     dataTableConverter.WriteJson(writer, (object) dataTable, serializer);
   }
   writer.WriteEndObject();
 }
Beispiel #4
0
    /// <summary>
    /// Reads the JSON representation of the object.
    /// </summary>
    /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
    /// <param name="objectType">Type of the object.</param>
    /// <param name="existingValue">The existing value of object being read.</param>
    /// <param name="serializer">The calling serializer.</param>
    /// <returns>The object value.</returns>
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
      DataSet ds = new DataSet();

      DataTableConverter converter = new DataTableConverter();

      reader.Read();

      while (reader.TokenType == JsonToken.PropertyName)
      {
        DataTable dt = (DataTable)converter.ReadJson(reader, typeof (DataTable), null, serializer);
        ds.Tables.Add(dt);
      }

      return ds;
    }
    /// <summary>
    /// Writes the JSON representation of the object.
    /// </summary>
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
    /// <param name="value">The value.</param>
    /// <param name="serializer">The calling serializer.</param>
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
      DataSet dataSet = (DataSet)value;

      DataTableConverter converter = new DataTableConverter();

      writer.WriteStartObject();

      foreach (DataTable table in dataSet.Tables)
      {
        writer.WritePropertyName(table.TableName);
        
        converter.WriteJson(writer, table, serializer);
      }

      writer.WriteEndObject();
    }
    /// <summary>
    /// Writes the JSON representation of the object.
    /// </summary>
    /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
    /// <param name="value">The value.</param>
    /// <param name="serializer">The calling serializer.</param>
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
      DataSet dataSet = (DataSet)value;
      DefaultContractResolver resolver = serializer.ContractResolver as DefaultContractResolver;

      DataTableConverter converter = new DataTableConverter();

      writer.WriteStartObject();

      foreach (DataTable table in dataSet.Tables)
      {
        writer.WritePropertyName((resolver != null) ? resolver.ResolvePropertyName(table.TableName) : table.TableName);
        
        converter.WriteJson(writer, table, serializer);
      }

      writer.WriteEndObject();
    }
Beispiel #7
0
        /// <summary>
        /// Writes the JSON representation of the object.
        /// </summary>
        /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param>
        /// <param name="value">The value.</param>
        /// <param name="serializer">The calling serializer.</param>
        public override void WriteJson(JsonWriter writer, object?value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }

            DataSet dataSet = (DataSet)value;
            DefaultContractResolver?resolver = serializer.ContractResolver as DefaultContractResolver;

            DataTableConverter converter = new DataTableConverter();

            writer.WriteStartObject();

            foreach (DataTable table in dataSet.Tables)
            {
                writer.WritePropertyName((resolver != null) ? resolver.GetResolvedPropertyName(table.TableName) : table.TableName);

                converter.WriteJson(writer, table, serializer);
            }

            writer.WriteEndObject();
        }
Beispiel #8
0
        public override object ReadJson(JsonReader reader, Type objectType, [Nullable(2)] object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }
            DataSet dataSet = (objectType == typeof(DataSet)) ? new DataSet() : ((DataSet)Activator.CreateInstance(objectType));

            DataTableConverter dataTableConverter = new DataTableConverter();

            reader.ReadAndAssert();
            while (reader.TokenType == JsonToken.PropertyName)
            {
                DataTable dataTable = dataSet.Tables[(string)reader.Value];
                bool      flag      = dataTable != null;
                dataTable = (DataTable)dataTableConverter.ReadJson(reader, typeof(DataTable), dataTable, serializer);
                if (!flag)
                {
                    dataSet.Tables.Add(dataTable);
                }
                reader.ReadAndAssert();
            }
            return(dataSet);
        }
Beispiel #9
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            DataTable dataTable;

            if (reader.TokenType == JsonToken.PropertyName)
            {
                dataTable = new DataTable((string)reader.Value);
                reader.Read();
            }
            else
            {
                dataTable = new DataTable();
            }
            reader.Read();
            while (reader.TokenType == JsonToken.StartObject)
            {
                DataRow row = dataTable.NewRow();
                reader.Read();
                while (reader.TokenType == JsonToken.PropertyName)
                {
                    string index = (string)reader.Value;
                    reader.Read();
                    if (!dataTable.Columns.Contains(index))
                    {
                        Type columnDataType = DataTableConverter.GetColumnDataType(reader.TokenType);
                        dataTable.Columns.Add(new DataColumn(index, columnDataType));
                    }
                    row[index] = reader.Value ?? (object)DBNull.Value;
                    reader.Read();
                }
                row.EndEdit();
                dataTable.Rows.Add(row);
                reader.Read();
            }
            return((object)dataTable);
        }
        private static void CreateRow(JsonReader reader, DataTable dt, JsonSerializer serializer)
        {
            object  value;
            DataRow dataRow = dt.NewRow();

            reader.ReadAndAssert();
            while (reader.TokenType == JsonToken.PropertyName)
            {
                string str = (string)reader.Value;
                reader.ReadAndAssert();
                DataColumn item = dt.Columns[str];
                if (item == null)
                {
                    item = new DataColumn(str, DataTableConverter.GetColumnDataType(reader));
                    dt.Columns.Add(item);
                }
                if (item.DataType == typeof(DataTable))
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        reader.ReadAndAssert();
                    }
                    DataTable dataTable = new DataTable();
                    while (reader.TokenType != JsonToken.EndArray)
                    {
                        DataTableConverter.CreateRow(reader, dataTable, serializer);
                        reader.ReadAndAssert();
                    }
                    dataRow[str] = dataTable;
                }
                else if (!item.DataType.IsArray || !(item.DataType != typeof(byte[])))
                {
                    if (reader.Value != null)
                    {
                        value = serializer.Deserialize(reader, item.DataType);
                        if (value == null)
                        {
                            value = DBNull.Value;
                        }
                    }
                    else
                    {
                        value = DBNull.Value;
                    }
                    dataRow[str] = value;
                }
                else
                {
                    if (reader.TokenType == JsonToken.StartArray)
                    {
                        reader.ReadAndAssert();
                    }
                    List <object> objs = new List <object>();
                    while (reader.TokenType != JsonToken.EndArray)
                    {
                        objs.Add(reader.Value);
                        reader.ReadAndAssert();
                    }
                    Array arrays = Array.CreateInstance(item.DataType.GetElementType(), objs.Count);
                    ((ICollection)objs).CopyTo(arrays, 0);
                    dataRow[str] = arrays;
                }
                reader.ReadAndAssert();
            }
            dataRow.EndEdit();
            dt.Rows.Add(dataRow);
        }