Skip to content

Commit 79bf1f5

Browse files
committed
Allow Complex JSON as Template input
Previously nested and array JSON was converted to flat dictionary.
1 parent ac51004 commit 79bf1f5

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

src/IotTelemetrySimulator/RunnerConfiguration.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Extensions.Configuration;
1010
using Microsoft.Extensions.Logging;
1111
using Newtonsoft.Json;
12+
using Newtonsoft.Json.Linq;
1213

1314
public class RunnerConfiguration
1415
{
@@ -450,26 +451,37 @@ private static string GetTemplateFromSection(IConfigurationSection subSection, s
450451
return valueString;
451452
}
452453

453-
var dictionaryVals = new Dictionary<string, string>();
454-
ConvertToDictionary(templateConfigSection, dictionaryVals, templateConfigSection);
455-
456-
return JsonConvert.SerializeObject(dictionaryVals);
454+
return JsonConvert.SerializeObject(ConvertToJToken(templateConfigSection));
457455
}
458456

459-
static void ConvertToDictionary(IConfigurationSection configuration, Dictionary<string, string> data, IConfigurationSection top = null)
457+
// Implemention from https://stackoverflow.com/a/62533775
458+
private static JToken ConvertToJToken(IConfiguration config)
460459
{
461-
var children = configuration.GetChildren();
462-
foreach (var child in children)
460+
JObject obj = new JObject();
461+
462+
foreach (var child in config.GetChildren())
463463
{
464-
if (child.Value == null)
464+
if (child.Path.EndsWith(":0"))
465465
{
466-
ConvertToDictionary(configuration.GetSection(child.Key), data, configuration);
467-
continue;
466+
var arr = new JArray();
467+
468+
foreach (var arrayChild in config.GetChildren())
469+
{
470+
arr.Add(ConvertToJToken(arrayChild));
471+
}
472+
473+
return arr;
468474
}
469475

470-
var key = top != null ? child.Path.Substring(top.Path.Length + 1) : child.Path;
471-
data[key] = child.Value;
476+
obj.Add(child.Key, ConvertToJToken(child));
472477
}
478+
479+
if (!obj.HasValues && config is IConfigurationSection section)
480+
{
481+
return new JValue(section.Value);
482+
}
483+
484+
return obj;
473485
}
474486
}
475487
}

test/IotTelemetrySimulator.Test/IotTelemetrySimulator.Test.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
@@ -32,6 +32,9 @@
3232
<AdditionalFiles Include="../../stylecop.json" Link="stylecop.json" />
3333
</ItemGroup>
3434
<ItemGroup>
35+
<None Update="test_files\test9-config-array-template.json">
36+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
37+
</None>
3538
<None Update="test_files\test5-config-payloads-as-json.json">
3639
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
3740
</None>

test/IotTelemetrySimulator.Test/RunnerConfigurationTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,5 +296,21 @@ public void When_DeviceSpecific_Interval_Is_Not_Positive_Number_Should_Throw(str
296296

297297
Assert.Throws<ConfigurationErrorsException>(() => RunnerConfiguration.Load(configuration, NullLogger.Instance));
298298
}
299+
300+
[Fact]
301+
public void When_Payload_Array_Template_Loads_Correctly()
302+
{
303+
var configuration = new ConfigurationBuilder()
304+
.AddJsonFile("./test_files/test9-config-array-template.json", false, false)
305+
.Build();
306+
307+
var target = RunnerConfiguration.Load(configuration, NullLogger.Instance);
308+
var templatedPayload = Assert.IsType<TemplatedPayload>(target.PayloadGenerator.Payloads[0]);
309+
Assert.Equal(1, templatedPayload.Variables.Variables.Count);
310+
Assert.True(templatedPayload.Variables.Variables[0].Sequence);
311+
Assert.Equal("device0001", templatedPayload.DeviceId);
312+
Assert.Equal(new[] { "Counter" }, templatedPayload.Variables.Variables[0].GetReferenceVariableNames());
313+
Assert.Equal("[{\"value1\":{\"value2\":\"$.Value\"}}]", templatedPayload.Template.ToString());
314+
}
299315
}
300316
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"Variables": [
3+
{
4+
"name": "Value",
5+
"sequence": true,
6+
"values": [ "$.Counter", "true" ]
7+
}
8+
],
9+
"Payloads": [
10+
{
11+
"type": "template",
12+
"deviceId": "device0001",
13+
"template": [{"value1": {"value2": "$.Value" } }]
14+
}
15+
]
16+
}

0 commit comments

Comments
 (0)