/** * Static utility method for facilitating writes on input object * * @param parent the source object * @param matchedElement the current spec (leaf) element that was matched with input * @param value to write * @param opMode to determine if write is applicable */ protected static void SetData(JToken parent, MatchedElement matchedElement, JToken value, OpMode opMode) { if (parent is JObject source) { string key = matchedElement.RawKey; if (opMode.IsApplicable(source, key)) { source[key] = value; } } else if (parent is JArray list && matchedElement is ArrayMatchedElement ame) { int origSize = ame.GetOrigSize(); int reqIndex = ame.GetRawIndex(); if (opMode.IsApplicable(list, reqIndex, origSize)) { list[reqIndex] = value; } }
/** * Creates an empty map/list, as required by spec, in the parent map/list at given key/index * * @param keyOrIndex of the parent object to create * @param walkedPath containing the parent object * @param opMode to determine if this write operation is allowed * @return newly created object */ public JToken Create(string keyOrIndex, WalkedPath walkedPath, OpMode opMode) { object parent = walkedPath.LastElement().TreeRef; int? origSizeOptional = walkedPath.LastElement().OrigSize; if (!Int32.TryParse(keyOrIndex, out int index)) { index = -1; } JToken value = null; if (parent is JObject map && opMode.IsApplicable(map, keyOrIndex)) { map[keyOrIndex] = value = CreateValue(); }