using Egw.Window.Data; using Microsoft.VisualBasic; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; using WebWindowTest.Models; using static WebWindowTest.Json.WindowConst; namespace WebWindowTest.Json { [JsonObject(MemberSerialization.OptIn)] public class JsonWindow { private string m_sProfilePath; [JsonProperty] public string ProfilePath { get { return m_sProfilePath; } set { m_sProfilePath = value; } } private string m_sMaterial; [JsonProperty] public string Material { get { return m_sMaterial; } set { m_sMaterial = value; } } private string m_sColorMaterial; [JsonProperty] public string ColorMaterial { get { return m_sColorMaterial; } set { m_sColorMaterial = value; } } private string m_sGlass; [JsonProperty] public string Glass { get { return m_sGlass; } set { m_sGlass = value; } } private List m_AreaList = new List(); [JsonProperty] public List AreaList { get { return m_AreaList; } set { m_AreaList = value; } } public JsonWindow(string ProfilePath, string Material, string ColorMaterial, string Glass) { m_sProfilePath = ProfilePath; m_sMaterial = Material; m_sColorMaterial = ColorMaterial; m_sGlass = Glass; } internal Window Deserialize() { Window Window = new Window() { sProfilePath = m_sProfilePath, sMaterial = m_sMaterial, sColorMaterial = m_sColorMaterial, sGlass = m_sGlass }; foreach (var Area in AreaList) Window.AreaList.Add((Frame)Area.Deserialize(null, Window)); return Window; } } public class JsonArea { private int m_nGroupId; [JsonProperty] public int GroupId { get { return m_nGroupId; } set { m_nGroupId = value; } } 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 string m_Threshold; [JsonProperty] public string Threshold { get { return m_Threshold; } set { m_Threshold = 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; } } //private JsonFrameArcElement m_FrameArcElem; //[JsonProperty] //public JsonFrameArcElement ArcElement //{ // get // { // return m_FrameArcElem; // } // set // { // m_FrameArcElem = value; // } //} public JsonFrame() : base(AreaTypes.FRAME) { } public JsonFrame(Shapes Shape, string sThreshold, bool BottomRail, int BottomRailQty, int GroupId) : base(AreaTypes.FRAME) { m_Shape = Shape; m_Threshold= sThreshold; m_bBottomRail = BottomRail; m_nBottomRailQty = BottomRailQty; base.GroupId = GroupId; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Frame newFrame = new Frame(null, ParentWindow); newFrame.SetGroupId(GroupId); newFrame.SetAreaType(AreaTypes.FRAME); newFrame.SetSelShape(m_Shape); newFrame.RefreshThresholdList(); newFrame.SetSelThresholdFromName(m_Threshold); newFrame.SetBottomRail(m_bBottomRail); newFrame.SetBottomRailQty(m_nBottomRailQty); //if(ArcElement != null) // newFrame.SetFrameArcElem(ArcElement.Deserialize(newFrame, ParentWindow)); //Frame.AppliedDone(); for (var DimensionIndex = 0; DimensionIndex <= m_DimensionList.Count - 1; DimensionIndex++) newFrame.DimensionList[DimensionIndex].SetDimension(m_DimensionList[DimensionIndex].Value); 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 IsSashVertical { 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 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; } } //private JsonSashArcElement m_SashArcElem; //[JsonProperty] //public JsonSashArcElement ArcElement //{ // get // { // return m_SashArcElem; // } // set // { // m_SashArcElem = value; // } //} private string m_Hardware; [JsonProperty] public string Hardware { get { return m_Hardware; } set { m_Hardware = value; } } private List m_HwOptionList = new List(); [JsonProperty] public List HwOptionList { get { return m_HwOptionList; } set { m_HwOptionList = value; } } public JsonSash() : base(AreaTypes.SASH) { } public JsonSash(bool IsSashVertical, SashTypes SashType, bool SashBottomRail, int SashBottomRailQty, string Hardware, int GroupId) : base(AreaTypes.SASH) { m_bIsSashVertical = IsSashVertical; m_SashType = SashType; m_bBottomRail = SashBottomRail; m_nBottomRailQty = SashBottomRailQty; m_Hardware = Hardware; base.GroupId = GroupId; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Sash newSash = new Sash(ParentArea, ParentWindow); newSash.SetGroupId(GroupId); newSash.SetAreaType(AreaTypes.SASH); newSash.SetSashQty(m_SashList.Count); newSash.SetIsSashVertical(m_bIsSashVertical); newSash.SetSashBottomRail(m_bBottomRail); newSash.SetSashBottomRailQty(m_nBottomRailQty); 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].HasHandle); newSash.SashList[SashIndex].SetDimension(m_SashList[SashIndex].Dimension); newSash.SashList[SashIndex].SetMeasureType(m_SashList[SashIndex].MeasureType); foreach (var Joint in m_SashList[SashIndex].JointList) newSash.SashList[SashIndex].JointList.Add(Joint.Deserialize((Area)ParentArea)); } newSash.SetSelFamilyHardwareFromIndex(Hardware); //newSash.ReqRefreshShape(); newSash.SetSelHardwareFromId(Hardware); foreach (var HwOption in m_HwOptionList) newSash.SelHwOptionList.Add(HwOption.Name, HwOption.Value); //if (ArcElement != null) // newSash.SetSashArcElem(ArcElement.Deserialize(newSash, ParentWindow)); 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 bool m_SplitStartVert; [JsonProperty] public bool SplitStartVert { get { return m_SplitStartVert; } set { m_SplitStartVert = value; } } private List m_SplitVertList = new List(); [JsonProperty] public List SplitVertList { get { return m_SplitVertList; } set { m_SplitVertList = value; } } private List m_SplitHorizList = new List(); [JsonProperty] public List SplitHorizList { get { return m_SplitHorizList; } set { m_SplitHorizList = value; } } public JsonSplit() : base(AreaTypes.SPLIT) { } public JsonSplit(SplitShapes SplitShape, int GroupId) : base(AreaTypes.SPLIT) { m_SplitShape = SplitShape; base.GroupId = GroupId; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Split Split = new Split(ParentArea, ParentWindow); Split.SetGroupId(GroupId); Split.SetAreaType(AreaTypes.SPLIT); Split.SetSplitShape(m_SplitShape); Split.SetSplitStartVert(m_SplitStartVert); if (m_SplitVertList.Count == 0) Split.SetSplitQtyVert(0); else { Split.SetSplitQtyVert(m_SplitVertList.Count - 1); for (var SplitIndex = 0; SplitIndex <= m_SplitVertList.Count - 1; SplitIndex++) { Split.SplitVertList[SplitIndex].SetIsRelative(m_SplitVertList[SplitIndex].IsRelative); Split.SplitVertList[SplitIndex].SetDimension(m_SplitVertList[SplitIndex].Dimension); Split.SplitVertList[SplitIndex].SetMeasureType(m_SplitVertList[SplitIndex].MeasureType); } } if (m_SplitHorizList.Count == 0) Split.SetSplitQtyHoriz(0); else { Split.SetSplitQtyHoriz(m_SplitHorizList.Count - 1); for (var SplitIndex = 0; SplitIndex <= m_SplitHorizList.Count - 1; SplitIndex++) { Split.SplitHorizList[SplitIndex].SetIsRelative(m_SplitHorizList[SplitIndex].IsRelative); Split.SplitHorizList[SplitIndex].SetDimension(m_SplitHorizList[SplitIndex].Dimension); Split.SplitHorizList[SplitIndex].SetMeasureType(m_SplitHorizList[SplitIndex].MeasureType); } } 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) { } public JsonSplitted(int GroupId) : base(AreaTypes.SPLITTED) { base.GroupId = GroupId; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Splitted newSplitted = new Splitted(ParentArea, ParentWindow); newSplitted.SetGroupId(GroupId); newSplitted.SetAreaType(AreaTypes.SPLITTED); //Split.AppliedDone(); foreach (var Area in AreaList) { var AreaDeserealized = Area.Deserialize(newSplitted, ParentWindow); if (AreaDeserealized != null) { newSplitted.AreaList.Add(AreaDeserealized); } } return newSplitted; } } 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, int GroupId) : base(AreaTypes.FILL) { m_FillType = FillType; base.GroupId = GroupId; } internal override Area Deserialize(Area ParentArea, Window ParentWindow) { Fill Fill = new Fill(ParentArea, ParentWindow); Fill.SetGroupId(GroupId); 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 JsonFrameArcElement //{ // private double m_Section; // [JsonProperty] // public double Section // { // get // { // return m_Section; // } // set // { // m_Section = value; // } // } // private bool m_bCutEdge; // [JsonProperty] // public bool CutEdge // { // get // { // return m_bCutEdge; // } // set // { // m_bCutEdge = value; // } // } // private bool m_bIsAlign; // [JsonProperty] // public bool IsAlign // { // get // { // return m_bIsAlign; // } // set // { // m_bIsAlign = value; // } // } // public JsonFrameArcElement(double Section, bool CutEdge, bool IsAlign) // { // m_Section = Section; // m_bCutEdge = CutEdge; // m_bIsAlign = IsAlign; // } // internal FrameArcElement Deserialize(Area ParentArea, Window ParentWindow) // { // FrameArcElement arcElement = new FrameArcElement(ParentArea, ParentWindow, m_Section, m_bCutEdge, m_bIsAlign); // return arcElement; // } //} //public class JsonSashArcElement //{ // private double m_Section; // [JsonProperty] // public double Section // { // get // { // return m_Section; // } // set // { // m_Section = value; // } // } // private bool m_bCutEdge; // [JsonProperty] // public bool CutEdge // { // get // { // return m_bCutEdge; // } // set // { // m_bCutEdge = value; // } // } // public JsonSashArcElement(double Section, bool CutEdge) // { // m_Section = Section; // m_bCutEdge = CutEdge; // } // internal SashArcElement Deserialize(Area ParentArea, Window ParentWindow) // { // SashArcElement arcElement = new SashArcElement(ParentArea, ParentWindow, m_Section, m_bCutEdge); // return arcElement; // } //} public class JsonJoint { private int m_nIndex; [JsonProperty] public int Index { 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 Index, Joints JointType) { m_nIndex = Index; m_JointType = JointType; } internal Joint Deserialize(Area newArea) { Joint newJoint = new Joint(newArea, m_nIndex, m_JointType); return newJoint; } } public class JsonHwOption { private string m_Name; [JsonProperty] public string Name { get { return m_Name; } set { m_Name = value; } } private string m_Value; [JsonProperty] public string Value { get { return m_Value; } set { m_Value = value; } } public JsonHwOption(string Name, string Value) { m_Name = Name; m_Value = Value; } //internal AGBOption Deserialize() //{ // AGBOption newOption = new AGBOption(m_Name, m_Value); // return newOption; //} } public class JsonFrameDimension { private int m_nIndex; [JsonProperty] public int Index { get { return m_nIndex; } } private string m_sName; [JsonProperty] public string Name { get { return m_sName; } } private double m_dValue; [JsonProperty] public double Value { get { return m_dValue; } set { m_dValue = value; } } public JsonFrameDimension(int Index, string Name, double Value) { m_nIndex = Index; m_sName = Name; m_dValue = Value; } } public class JsonSashDimension { private int m_nSashId; [JsonProperty] public int SashId { get { return m_nSashId; } } private Openings m_OpeningType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public Openings OpeningType { get { return m_OpeningType; } } private MeasureTypes m_MeasureType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public MeasureTypes MeasureType { get { return m_MeasureType; } } private double m_dDimension; [JsonProperty] public double Dimension { get { return m_dDimension; } set { m_dDimension = value; } } private bool m_bHasHandle; [JsonProperty] public bool HasHandle { get { return m_bHasHandle; } set { m_bHasHandle = value; } } private List m_JointList = new List(); [JsonProperty] public List JointList { get { return m_JointList; } set { m_JointList = value; } } public JsonSashDimension(Openings OpeningType, MeasureTypes MeasureType, bool HasHandle, double Dimension, int SashId) { m_OpeningType = OpeningType; m_MeasureType = MeasureType; m_bHasHandle = HasHandle; m_dDimension = Dimension; m_nSashId = SashId; } } public class JsonSplitDimension { private bool m_bIsRelative = false; [JsonProperty] public bool IsRelative { get { return m_bIsRelative; } } private double m_dDimension; [JsonProperty] public double Dimension { get { return m_dDimension; } set { m_dDimension = value; } } private MeasureTypes m_MeasureType; [JsonProperty] [JsonConverter(typeof(StringEnumConverter))] public MeasureTypes MeasureType { get { return m_MeasureType; } } public JsonSplitDimension(bool IsRelative, double Dimension, MeasureTypes MeasureType) { m_bIsRelative = IsRelative; m_dDimension = Dimension; m_MeasureType = MeasureType; } } 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); } } }