Serialize converters were added
This commit is contained in:
@@ -1,124 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using StructureHelperCommon.Models;
|
||||
|
||||
namespace DataAccess
|
||||
{
|
||||
using Newtonsoft.Json;
|
||||
using StructureHelperCommon.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
|
||||
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var logger = new TraceLogger();
|
||||
// class Program
|
||||
// {
|
||||
// static void Main(string[] args)
|
||||
// {
|
||||
// var logger = new TraceLogger();
|
||||
|
||||
// Create objects with complex relationships
|
||||
var parent1 = new Parent { Name = "Parent_1" };
|
||||
var parent2 = new Parent { Name = "Parent_2" };
|
||||
// // Create objects with complex relationships
|
||||
// var parent1 = new Parent { Name = "Parent_1" };
|
||||
// var parent2 = new Parent { Name = "Parent_2" };
|
||||
|
||||
var detail1 = new Detail { Description = "Detail_1", InternalNote = "Secret Note 1" };
|
||||
var detail2 = new Detail { Description = "Detail_2", InternalNote = "Secret Note 2" };
|
||||
var detail3 = new Detail { Description = "Detail_3", InternalNote = "Secret Note 3" };
|
||||
// var detail1 = new Detail { Description = "Detail_1", InternalNote = "Secret Note 1" };
|
||||
// var detail2 = new Detail { Description = "Detail_2", InternalNote = "Secret Note 2" };
|
||||
// var detail3 = new Detail { Description = "Detail_3", InternalNote = "Secret Note 3" };
|
||||
|
||||
var subDetail1 = new SubDetail { Info = "SubDetail_1" };
|
||||
// var subDetail1 = new SubDetail { Info = "SubDetail_1" };
|
||||
|
||||
// Set up relationships
|
||||
parent1.Details.Add(detail1);
|
||||
parent1.Details.Add(detail2);
|
||||
// // Set up relationships
|
||||
// parent1.Details.Add(detail1);
|
||||
// parent1.Details.Add(detail2);
|
||||
|
||||
parent2.Details.Add(detail2); // Shared detail
|
||||
parent2.Details.Add(detail3);
|
||||
// parent2.Details.Add(detail2); // Shared detail
|
||||
// parent2.Details.Add(detail3);
|
||||
|
||||
detail3.SubDetails.Add(subDetail1);
|
||||
// detail3.SubDetails.Add(subDetail1);
|
||||
|
||||
// Serialize with custom converters and trace logging
|
||||
string json = Serialize(new List<Parent> { parent1, parent2 }, logger);
|
||||
Console.WriteLine("Serialized JSON:");
|
||||
Console.WriteLine(json);
|
||||
// // Serialize with custom converters and trace logging
|
||||
// string json = Serialize(new List<Parent> { parent1, parent2 }, logger);
|
||||
// Console.WriteLine("Serialized JSON:");
|
||||
// Console.WriteLine(json);
|
||||
|
||||
// Deserialize with custom converters and trace logging
|
||||
var deserializedParents = Deserialize<List<Parent>>(json, logger);
|
||||
// // Deserialize with custom converters and trace logging
|
||||
// var deserializedParents = Deserialize<List<Parent>>(json, logger);
|
||||
|
||||
Console.WriteLine("\nDeserialized Objects:");
|
||||
foreach (var parent in deserializedParents)
|
||||
{
|
||||
Console.WriteLine($"Parent: {parent.Name}, Id: {parent.Id}");
|
||||
foreach (var detail in parent.Details)
|
||||
{
|
||||
Console.WriteLine($" Detail: {detail.Description}, Id: {detail.Id}");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Console.WriteLine("\nDeserialized Objects:");
|
||||
// foreach (var parent in deserializedParents)
|
||||
// {
|
||||
// Console.WriteLine($"Parent: {parent.Name}, Id: {parent.Id}");
|
||||
// foreach (var detail in parent.Details)
|
||||
// {
|
||||
// Console.WriteLine($" Detail: {detail.Description}, Id: {detail.Id}");
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
static string Serialize(object obj, TraceLogger logger)
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
Converters = new List<JsonConverter>
|
||||
{
|
||||
new ParentConverter(logger), // Add the specific converter
|
||||
// Add other converters if needed
|
||||
},
|
||||
Formatting = Formatting.Indented
|
||||
};
|
||||
// static string Serialize(object obj, TraceLogger logger)
|
||||
// {
|
||||
// var settings = new JsonSerializerSettings
|
||||
// {
|
||||
// Converters = new List<JsonConverter>
|
||||
// {
|
||||
// new ParentConverter(logger), // Add the specific converter
|
||||
// // Add other converters if needed
|
||||
// },
|
||||
// Formatting = Formatting.Indented
|
||||
// };
|
||||
|
||||
return JsonConvert.SerializeObject(obj, settings);
|
||||
}
|
||||
// return JsonConvert.SerializeObject(obj, settings);
|
||||
// }
|
||||
|
||||
static T Deserialize<T>(string json, TraceLogger logger)
|
||||
{
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
Converters = new List<JsonConverter>
|
||||
{
|
||||
new ParentConverter(logger), // Add the specific converter
|
||||
// Add other converters if needed
|
||||
}
|
||||
};
|
||||
// static T Deserialize<T>(string json, TraceLogger logger)
|
||||
// {
|
||||
// var settings = new JsonSerializerSettings
|
||||
// {
|
||||
// Converters = new List<JsonConverter>
|
||||
// {
|
||||
// new ParentConverter(logger), // Add the specific converter
|
||||
// // Add other converters if needed
|
||||
// }
|
||||
// };
|
||||
|
||||
return JsonConvert.DeserializeObject<T>(json, settings);
|
||||
}
|
||||
}
|
||||
// return JsonConvert.DeserializeObject<T>(json, settings);
|
||||
// }
|
||||
// }
|
||||
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Parent
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
[JsonProperty("parent_name")]
|
||||
public string Name { get; set; }
|
||||
//public class Parent
|
||||
// {
|
||||
// public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
public List<Detail> Details { get; set; } = new List<Detail>();
|
||||
}
|
||||
// [JsonProperty("parent_name")]
|
||||
// public string Name { get; set; }
|
||||
|
||||
public class Detail
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
// public List<Detail> Details { get; set; } = new List<Detail>();
|
||||
// }
|
||||
|
||||
[JsonPropertyName("detail_description")] // Compatible with System.Text.Json
|
||||
public string Description { get; set; }
|
||||
// public class Detail
|
||||
// {
|
||||
// public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
[JsonIgnore] // This property will be ignored during serialization
|
||||
public string InternalNote { get; set; }
|
||||
// [JsonPropertyName("detail_description")] // Compatible with System.Text.Json
|
||||
// public string Description { get; set; }
|
||||
|
||||
public List<SubDetail> SubDetails { get; set; } = new List<SubDetail>();
|
||||
}
|
||||
// [JsonIgnore] // This property will be ignored during serialization
|
||||
// public string InternalNote { get; set; }
|
||||
|
||||
public class SubDetail
|
||||
{
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
public string Info { get; set; }
|
||||
}
|
||||
// public List<SubDetail> SubDetails { get; set; } = new List<SubDetail>();
|
||||
// }
|
||||
|
||||
// public class SubDetail
|
||||
// {
|
||||
// public Guid Id { get; set; } = Guid.NewGuid();
|
||||
// public string Info { get; set; }
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user