public string Name { get; set; }
public List<KeyValuePair<string, Object>> Properties { get; set; }
public void DeSerialiseTech(string inp)
{
this.Properties = new List<KeyValuePair<string, Object>>();
Regex rx = new Regex("(\\S+)\\s*=\\s*(\\S*)", RegexOptions.Compiled);
Match rxMatch = rx.Match(inp);
if (rxMatch.Success)
{
this.Name = rxMatch.Groups[1].Value;
Object properties = DeSerialisePart(inp.Substring(rxMatch.Index + rxMatch.Length), rx);
try {
this.Properties = (List<KeyValuePair<string, Object>>)properties;
} catch { }
}
}
private Object DeSerialisePart(string inp, Regex rx)
{
MatchCollection rxMatches = rx.Matches(inp);
if (rxMatches.Count == 0) return inp;
List<KeyValuePair<string, Object>> subDict = new List<KeyValuePair<string, Object>>();
int closeIdx = 0;
foreach (Match rxMatch in rxMatches)
{
if (rxMatch.Index >= closeIdx)
{
if (rxMatch.Groups[2].Value == "{")
{
int startIdx = rxMatch.Index;
closeIdx = startIdx;
int openCnt = 1;
foreach (char c in inp.Substring(startIdx + rxMatch.Length))
{
closeIdx += 1;
if (c == '{') openCnt += 1;
else if (c == '}') openCnt -= 1;
if (openCnt == 0) break;
}
subDict.Add(new KeyValuePair<string, Object>(rxMatch.Groups[1].Value, DeSerialisePart(inp.Substring(startIdx + rxMatch.Length, closeIdx - startIdx - 1), rx)));
}
else subDict.Add(new KeyValuePair<string, Object>(rxMatch.Groups[1].Value, rxMatch.Groups[2].Value));
}
}
return subDict;
}
}