/** * Do a Shift style insert : * 1) if there is no data "there", then just set it * 2) if there is already a list "there", just add the data to the list * 3) if there something other than a list there, grab it and stuff it and the data into a list * and overwrite what is there with a list. */ public override JToken HandleFinalSet(ITraversalStep traversalStep, JToken tree, string key, JToken data) { var optSub = traversalStep.Get(tree, key); if (optSub == null || optSub.Type == JTokenType.Null) { // nothing is here so just set the data traversalStep.OverwriteSet(tree, key, data); } else if (optSub is JArray arr) { // there is a list here, so we just add to it arr.Add(data); } else { // take whatever is there and make it the first element in an Array var temp = new JArray(); temp.Add(optSub); temp.Add(data); traversalStep.OverwriteSet(tree, key, temp); } return(data); }
/** * Only make a new instance of a container object for SET, if there is nothing "there". */ public override JToken HandleIntermediateGet(ITraversalStep traversalStep, JToken tree, string key, TraversalStepOperation op) { var sub = traversalStep.Get(tree, key); if ((sub == null || sub.Type == JTokenType.Null) && op == TraversalStepOperation.SET) { // get our child to make the container object, so it will be happy with it sub = traversalStep.GetChild().NewContainer(); traversalStep.OverwriteSet(tree, key, sub); } return(sub); }