I found the following example in StackOverflow. https://stackoverflow.com/a/56741885
When I use Newtonsoft, it works like the preceding article link. However, it doesn't work with System.Text.Json; I get the error stated below.
using DataModelsClassLibrary1;
using Microsoft.Data.SqlClient;
using Microsoft.IdentityModel.Protocols;
//using Newtonsoft.Json;
using System.Data;
using System.Text.Json;
namespace DataLayerClassLibrary1
{
public class Class1
{
public static List<TodoItem> GetAllTodos()
{
var connString = @"Server=(localdb)\MSSQLLocalDB;Database=Database1;Trusted_Connection=True;";
var dt = new DataTable();
List<TodoItem> todos = new List<TodoItem>();
using (var con = new SqlConnection(connString))
{
SqlCommand command = new SqlCommand(
"SELECT * FROM Todo",
con);
con.Open();
SqlDataReader reader = command.ExecuteReader();
dt.Load(reader);
if (dt.Rows.Count > 0)
{
//var serializedMyObjects = JsonConvert.SerializeObject(dt);
//// Here you get the object
//todos = (List<TodoItem>)JsonConvert.DeserializeObject(serializedMyObjects, typeof(List<TodoItem>));
var serializedMyObjects = JsonSerializer.Serialize(dt);
// Here you get the object
todos = JsonSerializer.Deserialize<List<TodoItem>>(serializedMyObjects);
}
con.Close();
}
return todos;
}
}
}
Error:
Unhandled exception. System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported. Path: $.Columns.DataType. ---> System.NotSupportedException: Serialization and deserialization of 'System.Type' instances are not supported.
at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) at System.Text.Json.Serialization.JsonConverter
1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter
1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter
1.TryWriteAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.Converters.IEnumerableConverter
1.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonCollectionConverter2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter
1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer) at System.Text.Json.Serialization.Converters.ObjectDefaultConverter
1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state) at System.Text.Json.Serialization.JsonConverter
1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
--- End of inner exception stack trace --- at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex) at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteUsingSerializer[TValue](Utf8JsonWriter writer, TValue& value, JsonTypeInfo jsonTypeInfo) at System.Text.Json.JsonSerializer.WriteStringUsingSerializer[TValue](TValue& value, JsonTypeInfo jsonTypeInfo) at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options) at DataLayerClassLibrary1.Class1.GetAllTodos() in . .