Skip to content
Open
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
19 changes: 17 additions & 2 deletions JUST.net/JsonTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ private void RecursiveEvaluate(ref JToken parentToken, State state)
bool isBulk = false;
bool isScope = false;

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

Expand Down Expand Up @@ -267,7 +268,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
11 changes: 11 additions & 0 deletions UnitTests/ConditionalFunctionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,5 +256,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);
}
}
}