in src/Azure.Deployments.Extensibility.Core/V2/Json/JsonNodeExtensions.cs [63:101]
public static JsonNode SetPropertyValue(this JsonNode? node, JsonPointerProxy propertyPath, JsonNode propertyValue) =>
SetPropertyValue(node, propertyPath.ToJsonPointer(), propertyValue);
public static JsonNode SetPropertyValue(this JsonNode? node, JsonPointer propertyPath, JsonNode propertyValue)
{
ArgumentNullException.ThrowIfNull(node, nameof(node));
if (propertyPath.Count == 0)
{
throw new ArgumentException("Argument cannot be empty.", nameof(propertyPath));
}
for (int i = 0; i < propertyPath.Count; i++)
{
var segment = propertyPath[i];
if (i != propertyPath.Count - 1)
{
if (node is not JsonObject jsonObject)
{
throw new InvalidOperationException($"Property {segment} not found.");
}
if (node[segment] is not { } propertyNode)
{
propertyNode = new JsonObject();
jsonObject[segment] = propertyNode;
}
node = propertyNode.AsObject();
}
else
{
node[segment] = propertyValue;
}
}
return node;
}