using Microsoft.VisualBasic; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using static WebWindowConfigurator.Json.WindowConst; namespace WebWindowConfigurator.Json { [JsonObject(MemberSerialization.OptIn)] public class JsonWindow { private string m_sProfilePath; [JsonProperty] public string ProfilePath { get { return m_sProfilePath; } set { m_sProfilePath = value; } } private List m_AreaList = new List(); [JsonProperty] public List AreaList { get { return m_AreaList; } set { m_AreaList = value; } } public JsonWindow(string ProfilePath) { m_sProfilePath = ProfilePath; } internal Window Deserialize() { Window Window = new Window() { sProfilePath = m_sProfilePath }; foreach (var Area in AreaList) Window.AreaList.Add((Frame)Area.Deserialize(null,Window)!); return Window; } } public class JsonArea { private List m_AreaList = new List(); [JsonProperty] public List AreaList { get { return m_AreaList; } set { m_AreaList = value; } } private AreaTypes m_AreaType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public AreaTypes AreaType { get { return m_AreaType; } set { m_AreaType = value; } } public JsonArea(AreaTypes AreaType) { m_AreaType = AreaType; } internal virtual Area? Deserialize(Area ParentArea, Window ParentWindow) { return null; } } public class JsonFrame : JsonArea { private Shapes m_Shape; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public Shapes Shape { get { return m_Shape; } set { m_Shape = value; } } private List m_DimensionList = new List(); [JsonProperty] public List DimensionList { get { return m_DimensionList; } } private List m_JointList = new List(); [JsonProperty] public List JointList { get { return m_JointList; } set { m_JointList = value; } } private bool m_bBottomRail; [JsonProperty] public bool BottomRail { get { return m_bBottomRail; } set { m_bBottomRail = value; } } private int m_nBottomRailQty; [JsonProperty] public int BottomRailQty { get { return m_nBottomRailQty; } set { m_nBottomRailQty = value; } } public JsonFrame() : base(AreaTypes.FRAME) { } public JsonFrame(Shapes Shape, bool BottomRail, int BottomRailQty) : base(AreaTypes.FRAME) { m_Shape = Shape; m_bBottomRail = BottomRail; m_nBottomRailQty = BottomRailQty; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Frame newFrame = new Frame(null, ParentWindow); newFrame.SetAreaType(AreaTypes.FRAME); newFrame.SetSelShape(m_Shape); newFrame.SetBottomRail(m_bBottomRail); newFrame.SetBottomRailQty(m_nBottomRailQty); //Frame.AppliedDone(); for (var DimensionIndex = 0; DimensionIndex <= m_DimensionList.Count - 1; DimensionIndex++) newFrame.DimensionList[DimensionIndex].dValue = m_DimensionList[DimensionIndex].dValue; for (var JointIndex = 0; JointIndex <= m_JointList.Count - 1; JointIndex++) newFrame.JointList[JointIndex].SetSelJointType(m_JointList[JointIndex].JointType); foreach (var Area in AreaList) { var aList = Area.Deserialize(newFrame, ParentWindow); if (aList != null) { newFrame.AreaList.Add(aList); } } return newFrame; } } public class JsonSash : JsonArea { private bool m_bIsSashVertical; [JsonProperty] public bool bIsSashVertical { get { return m_bIsSashVertical; } set { m_bIsSashVertical = value; } } private List m_SashList = new List(); [JsonProperty] public List SashList { get { return m_SashList; } set { m_SashList = value; } } private SashTypes m_SashType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public SashTypes SashType { get { return m_SashType; } set { m_SashType = value; } } private List m_JointList = new List(); [JsonProperty] public List JointList { get { return m_JointList; } set { m_JointList = value; } } private bool m_bSashBottomRail; [JsonProperty] public bool SashBottomRail { get { return m_bSashBottomRail; } set { m_bSashBottomRail = value; } } private int m_nSashBottomRailQty; [JsonProperty] public int SashBottomRailQty { get { return m_nSashBottomRailQty; } set { m_nSashBottomRailQty = value; } } private string m_Hardware; [JsonProperty] public string Hardware { get { return m_Hardware; } set { m_Hardware = value; } } public JsonSash() : base(AreaTypes.SASH) { } public JsonSash(bool bIsSashVertical, SashTypes SashType, bool SashBottomRail, int SashBottomRailQty/*, string Hardware*/) : base(AreaTypes.SASH) { m_bIsSashVertical = bIsSashVertical; m_SashType = SashType; m_bSashBottomRail = SashBottomRail; m_nSashBottomRailQty = SashBottomRailQty; m_Hardware = Hardware; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Sash newSash = new Sash(ParentArea, ParentWindow); newSash.SetAreaType(AreaTypes.SASH); newSash.SetSashQty(m_SashList.Count); newSash.SetIsSashVertical(m_bIsSashVertical); newSash.SetSashBottomRail(m_bSashBottomRail); newSash.SetSashBottomRailQty(m_nSashBottomRailQty); //Sash.AppliedDone(); for (var SashIndex = 0; SashIndex <= m_SashList.Count - 1; SashIndex++) { newSash.SashList[SashIndex].SetOpeningType(m_SashList[SashIndex].OpeningType); newSash.SashList[SashIndex].SetHasHandle(m_SashList[SashIndex].bHasHandle); newSash.SashList[SashIndex].SetDimension(m_SashList[SashIndex].dDimension); } foreach (var Joint in m_JointList) newSash.JointList.Add(Joint.Deserialize((Area)ParentArea)); //Sash.RefreshHardwareList(); //Sash.SetSelHardwareFromId(Hardware); foreach (var Area in AreaList) { var AreaDeserealized = Area.Deserialize(newSash, ParentWindow); if (AreaDeserealized != null) { newSash.AreaList.Add(AreaDeserealized); } } return newSash; } } public class JsonSplit : JsonArea { private SplitShapes m_SplitShape; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] internal SplitShapes SplitShape { get { return m_SplitShape; } set { m_SplitShape = value; } } private List m_SplitPositionList = new List(); [JsonProperty] public List SplitPositionList { get { return m_SplitPositionList; } set { m_SplitPositionList = value; } } public JsonSplit() : base(AreaTypes.SPLIT) { } public JsonSplit(SplitShapes SplitShape) : base(AreaTypes.SPLIT) { m_SplitShape = SplitShape; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Split Split = new Split(ParentArea, ParentWindow); Split.SetAreaType(AreaTypes.SPLIT); Split.SetSplitShape(m_SplitShape, true); Split.SetSplitQty(m_SplitPositionList.Count - 1, true); //Split.AppliedDone(); for (var SplitIndex = 0; SplitIndex <= m_SplitPositionList.Count - 1; SplitIndex++) { Split.SplitPositionList[SplitIndex].SetIsRelative(m_SplitPositionList[SplitIndex].bIsRelative); Split.SplitPositionList[SplitIndex].SetDimension(m_SplitPositionList[SplitIndex].dDimension); } foreach (var Area in AreaList) { var AreaDeserealized = Area.Deserialize(Split, ParentWindow); if (AreaDeserealized != null) { Split.AreaList.Add(AreaDeserealized); } } return Split; } } public class JsonSplitted : JsonArea { public JsonSplitted() : base(AreaTypes.SPLITTED) { } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Splitted Split = new Splitted(ParentArea, ParentWindow); Split.SetAreaType(AreaTypes.SPLITTED); //Split.AppliedDone(); foreach (var Area in AreaList) { var AreaDeserealized = Area.Deserialize(Split, ParentWindow); if (AreaDeserealized != null) { Split.AreaList.Add(AreaDeserealized); } } return Split; } } public class JsonFill : JsonArea { private FillTypes m_FillType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public FillTypes FillType { get { return m_FillType; } set { m_FillType = value; } } public JsonFill() : base(AreaTypes.FILL) { } public JsonFill(FillTypes FillType) : base(AreaTypes.FILL) { m_FillType = FillType; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Fill Fill = new Fill(ParentArea, ParentWindow); Fill.SetAreaType(AreaTypes.FILL); Fill.SetFillType(m_FillType); //Fill.AppliedDone(); foreach (var Area in AreaList) { var AreaDeserealized = Area.Deserialize(Fill, ParentWindow); if (AreaDeserealized != null) { Fill.AreaList.Add(AreaDeserealized); } } return Fill; } } public class JsonJoint { private int m_nIndex; [JsonProperty] public int nIndex { get { return m_nIndex; } } private Joints m_JointType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public Joints JointType { get { return m_JointType; } set { m_JointType = value; } } public JsonJoint(int nIndex, Joints JointType) { m_nIndex = nIndex; m_JointType = JointType; } internal Joint Deserialize(Area newArea) { Joint newJoint = new Joint(newArea, m_nIndex, m_JointType); return newJoint; } } public class JsonCurve { private GDB_TY m_CurveType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public GDB_TY CurveType { get { return m_CurveType; } set { m_CurveType = value; } } private Point3d m_ptStart; [JsonProperty] public Point3d ptStart { get { return m_ptStart; } set { m_ptStart = value; } } private Point3d m_ptEnd; [JsonProperty] public Point3d ptEnd { get { return m_ptEnd; } set { m_ptEnd = value; } } } public class JsonFrameDimension { private int m_nIndex; [JsonProperty] public int nIndex { get { return m_nIndex; } } private string m_sName; [JsonProperty] public string sName { get { return m_sName; } } private double m_dValue; [JsonProperty] public double dValue { get { return m_dValue; } set { m_dValue = value; } } public JsonFrameDimension(int nIndex, string sName, double dValue) { m_nIndex = nIndex; m_sName = sName; m_dValue = dValue; } } public class JsonSashDimension { private Openings m_OpeningType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public Openings OpeningType { get { return m_OpeningType; } } private bool m_bHasHandle; [JsonProperty] public bool bHasHandle { get { return m_bHasHandle; } set { m_bHasHandle = value; } } private double m_dDimension; [JsonProperty] public double dDimension { get { return m_dDimension; } set { m_dDimension = value; } } public JsonSashDimension(Openings OpeningType, bool bHasHandle, double dDimension) { m_OpeningType = OpeningType; m_bHasHandle = bHasHandle; m_dDimension = dDimension; } } public class JsonSplitDimension { private bool m_bIsRelative = false; [JsonProperty] public bool bIsRelative { get { return m_bIsRelative; } } private double m_dDimension; [JsonProperty] public double dDimension { get { return m_dDimension; } set { m_dDimension = value; } } public JsonSplitDimension(bool bIsRelative, double dDimension) { m_bIsRelative = bIsRelative; m_dDimension = dDimension; } } public class PolymorphicJsonConverter : JsonConverter { public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // disambiguo il tipo JsonArea grazie al capo AreaType nelle classi giuste, e le popolo di dati JObject item = JObject.Load(reader); var RawType = (string?)item["AreaType"]; AreaTypes Type = AreaTypes.NULL; Enum.TryParse(RawType, out Type); JsonArea JsonArea = null; switch (Type) { case AreaTypes.FRAME: { JsonArea = new JsonFrame(); break; } case AreaTypes.SASH: { JsonArea = new JsonSash(); break; } case AreaTypes.FILL: { JsonArea = new JsonFill(); break; } case AreaTypes.SPLIT: { JsonArea = new JsonSplit(); break; } case AreaTypes.SPLITTED: { JsonArea = new JsonSplitted(); break; } } if (!Information.IsNothing(JsonArea)) { serializer.Populate(item.CreateReader(), JsonArea); return JsonArea; } else return null; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new NotImplementedException(); } public override bool CanConvert(Type objectType) { // gestisco solo i tipi dericati da JsonArea return typeof(JsonArea).IsAssignableFrom(objectType); } } }