Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions JUST.net/JsonTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,9 @@ private void RecursiveEvaluate(ref JToken parentToken, IDictionary<string, JArra
bool isLoop = false;
bool isBulk = false;

foreach (JToken childToken in tokens)
for (int i = 0; i < tokens.Count(); i++)
{
var childToken = tokens.ElementAt(i);
ParseToken(parentToken, parentArray, currentArrayToken, ref selectedTokens, ref tokensToReplace, ref tokensToDelete, ref loopProperties, ref condProps, ref arrayToForm, ref dictToForm, ref tokenToForm, ref tokensToAdd, ref isLoop, ref isBulk, childToken);
}

Expand Down Expand Up @@ -247,7 +248,21 @@ private void PostOperationsBuildUp(ref JToken parentToken, List<JToken> tokenToF
}
else if (token is JArray arr && parentToken.Parent != null)
{
(parentToken.Parent as JProperty).Value = arr;
switch (parentToken.Parent.Type)
{
case JTokenType.Array:
parentToken.Replace(arr);
break;
case JTokenType.Property:
(parentToken.Parent as JProperty).Value = arr;
break;
default:
if (Context.IsStrictMode())
{
throw new Exception($"don't know what to do with {token} and {parentToken.Type} parent!");
}
break;
}
}
else
{
Expand Down Expand Up @@ -902,12 +917,12 @@ private object ParseApplyOver(IDictionary<string, JArray> array, IDictionary<str
var contextInput = Context.Input;
var input = JToken.Parse(Transform(parameters[0].ToString(), contextInput.ToString()));
Context.Input = input;
if (parameters[1].ToString().Trim().Trim('\'').StartsWith('{'))
if (parameters[1].ToString().Trim().Trim('\'').StartsWith("{"))
{
var jobj = JObject.Parse(parameters[1].ToString().Trim().Trim('\''));
output = new JsonTransformer(Context).Transform(jobj, input);
}
else if (parameters[1].ToString().Trim().Trim('\'').StartsWith('['))
else if (parameters[1].ToString().Trim().Trim('\'').StartsWith("["))
{
var jarr = JArray.Parse(parameters[1].ToString().Trim().Trim('\''));
output = new JsonTransformer(Context).Transform(jarr, input);
Expand Down
13 changes: 12 additions & 1 deletion UnitTests/ConditionalFunctionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void ConditionalGroupNonExistingWithLoopInside()

Assert.AreEqual("{\"Result\":{},\"Other\":\"property\"}", result);
}

[Test, Category("IfCondition")]
public void ConstantEmptyArray()
{
Expand All @@ -245,5 +245,16 @@ public void ConstantEmptyArray()

Assert.AreEqual("{\"result\":[]}", result);
}

[Test]
public void ConditionalGroupOnArrayItemWithLoopInside()
{
const string input = "{ \"additional_content\": [{ \"Cards_feed\": { \"cards_feed\": [ \"2\"] } }] }";
const string transformer = "{ \"content\": [ { \"aaa\": \"bbb\" }, { \"#ifgroup(#exists($.additional_content[*].Cards_feed))\": { \"#loop($.additional_content[*].Cards_feed.cards_feed)\": { \"xxx\": \"yyy\" } } } ] }";

var result = new JsonTransformer(new JUSTContext { EvaluationMode = EvaluationMode.Strict }).Transform(transformer, input);

Assert.AreEqual("{\"content\":[{\"aaa\":\"bbb\"},[{\"xxx\":\"yyy\"}]]}", result);
}
}
}
4 changes: 2 additions & 2 deletions UnitTests/JUST.net.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

<ItemGroup>
<PackageReference Include="nunit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
</ItemGroup>

<ItemGroup>
Expand Down