Skip to content

Commit

Permalink
Fixed json building while reading non-global vars
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarresi committed May 8, 2021
1 parent c4dbaaa commit 474bf2e
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion TwinCatAdsTool.Logic/Services/PersistentVariableService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,47 @@ public async Task<JObject> ReadGlobalPersistentVariables(AdsClient client, IInst
uo.Add(up);
}
}
jobj.Add(element.Key, uo);
var path = element.Key.Split(".");
if (path.Count() == 1)
{
jobj.Add(element.Key, uo);
}
else
{
for(var i = 0; i < path.Length-1; i++)
{
if ((jobj.SelectToken(string.Join(".", path.Take(i+1))) as JObject) == null)
{
if (i > 1)
{
(jobj.SelectToken(string.Join(".", path.Take(i))) as JObject).Add(path[i], new JObject());
}
else
{
jobj.Add(path[i], new JObject());
}
}
}

try
{
(jobj.SelectToken(string.Join(".", path.Take(path.Length - 1))) as JObject).Add(path.Last(), uo);
}
catch(ArgumentException)
{
var token = (jobj.SelectToken(string.Join(".", path)) as JObject);
if(token != null)
{
foreach (var prop in uo.Properties())
{
token.Add(prop.Name, prop.Value);
}
}
}
}

}

}
}
catch (Exception e)
Expand Down

0 comments on commit 474bf2e

Please sign in to comment.