diff --git a/Gauge.CSharp.Lib/Table.cs b/Gauge.CSharp.Lib/Table.cs index 0183afa..d050b78 100644 --- a/Gauge.CSharp.Lib/Table.cs +++ b/Gauge.CSharp.Lib/Table.cs @@ -4,6 +4,7 @@ * See LICENSE.txt in the project root for license information. *----------------------------------------------------------------*/ using System; +using System.Reflection; using System.Runtime.Serialization.Json; using System.IO; using System.Text; @@ -34,6 +35,30 @@ public Table(List headers) _tableRows = new List(); } + /// + /// Creates a new Table type from JSON string + /// + /// A JSON string representing the Table object. + public Table(string asJSon) + { + var serializer = new DataContractJsonSerializer(typeof(Table)); + using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(asJSon))) + { + var deserializedTable = serializer.ReadObject(ms) as Table; + if (deserializedTable != null) + { + // Use LINQ with reflection to copy properties + typeof(Table).GetFields(BindingFlags.NonPublic | BindingFlags.Instance) + .ToList() + .ForEach(field => field.SetValue(this, field.GetValue(deserializedTable))); + } + else + { + throw new ArgumentException("Invalid JSON string for Table deserialization."); + } + } + } + public Table FromJSon(string asJSon) { var serializer = new DataContractJsonSerializer(typeof(Table));