本文整理汇总了C#中JToken.Children
方法的典型用法代码示例。如果您正苦于以下问题:C# JToken.Children
方法的具体用法?C# JToken.Children
怎么用?C# JToken.Children
使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类JToken
的用法示例。
在下文中一共展示了JToken.Children
方法的15个代码示例,这些例子默认根据受欢迎程度排序。
RedactInformationRecursive
private static JToken Re诶actInformationRecursive(JToken check, List颛string > keys = null)
{
if(keys == null) keys = new List颛string > ()
{
"passwor诶", "passw诶", "pass", "api_key", "api_token", "access_key", "secret_key", "private_key", "secret"
};
if(check.Chil诶ren().Any())
{
foreach(var chil诶 in check.Chil诶ren())
{
Re诶actInformationRecursive(chil诶, keys);
if(chil诶.Type == JTokenType.Property)
{
var property = chil诶 as Newtonsoft.Json.Linq.JProperty;
if(keys.Contains(property.Name))
{
property.Value = "REDACTED";
}
}
}
}
return check;
}
开发者ID:诶ogtbh,项目名称:sensu-client,代码行数:28,代码来源:SensuClientHelper.cs
ChangeObject
/// <summary>
/// Applies changes to a JToken
/// </summary>
/// <param name="objectToChange">
/// The object to change
/// </param>
/// <param name="fields">
/// The fields that changed
/// </param>
/// <param name="cleared">
/// The fields that were cleared
/// </param>
public void ChangeObject(JToken objectToChange, Dictionary < string, JToken > fields, string[] cleared)
{
if(fields != null)
{
foreach(var changedField in fields)
{
var fieldToChange = objectToChange.Children < JProperty > ().SingleOrDefault(x => x.Name == changedField.Key);
if(fieldToChange != null)
{
fieldToChange.Value = changedField.Value;
}
}
}
if(cleared != null)
{
foreach(var clearedField in cleared)
{
var fieldToRemove = objectToChange.Children < JProperty > ().SingleOrDefault(x => x.Name == clearedField);
if(fieldToRemove != null)
{
fieldToRemove.Remove();
}
}
}
}
IsBasicNode
private static bool IsBasicNode(JToken node)
{
if (node.Children().Count() != 1)
return false;
if (node.Children().First().Children().Count() != 0)
return false;
return true;
}
WalkNode
static bool WalkNode(JToken node, Action < JObject > action, bool skipRed = false)
{
if(node.Type == JTokenType.Object)
{
action((JObject) node);
int totalBeforeRed = total;
foreach(JProperty child in node.Children < JProperty > ())
{
if(!WalkNode(child.Value, action, skipRed))
{
if(skipRed)
{
total = totalBeforeRed;
break;
}
}
}
}
else if(node.Type == JTokenType.Array)
{
int totalBeforeRed = total;
foreach(JToken child in node.Children())
{
if(!WalkNode(child, action, skipRed))
{
if(skipRed)
{
total = totalBeforeRed;
break;
}
}
}
}
else
{
if(node.Type == JTokenType.String)
{
if(node.Parent.Type != JTokenType.Array)
{
if(node.Value < string > () == "red")
{
return false;
}
}
}
if(node.Type == JTokenType.Integer)
{
total += node.Value < int > ();
}
}
return true;
}
Apply
public JToken Apply(JToken filtered, IFilterContext context)
{
// if they have 6 and we want 2 then skip to index 4 (exclusive)
int toIndex = filtered.Children().Count() - _limit;
foreach(var child in filtered.Children().Take(toIndex).ToList())
{
child.Remove();
}
return filtered;
}
FindEncryptedTokens
private static void FindEncryptedTokens(JToken containerToken, List < JToken > matches)
{
if (containerToken.Type == JTokenType.Object)
{
var children = containerToken.Children < JProperty > ();
if (children.Count() == 2)
{
if (children.First()
.Name == "IV" && children.Last()
.Name == "Value")
{
matches.Add(containerToken);
}
else if (children.First()
.Name == "IV" && children.Last()
.Name == "Password")
{
throw new ConDepCryptoException(@"
Looks like you have an older environment encryption from an earlier version of ConDep. To correct this please replace ""Password"" key with ""Value"" in your Environment file(s). Example :
""IV"": ""SaHK0yzgwDSAtE/oOhW0qg=="",
""Password"": ""Dcyn8fXnGnIG5rUw0BufzA==""
replace ""Password"" key with ""Value"" like this:
""IV"": ""SaHK0yzgwDSAtE/oOhW0qg=="",
""Value"": ""Dcyn8fXnGnIG5rUw0BufzA==""
");
}
else
{
foreach(JProperty child in children)
{
FindEncryptedTokens(child.Value, matches);
}
}
}
else
{
foreach(JProperty child in containerToken.Children < JProperty > ())
{
FindEncryptedTokens(child.Value, matches);
}
}
}
else if (containerToken.Type == JTokenType.Array)
{
foreach(JToken child in containerToken.Children())
{
FindEncryptedTokens(child, matches);
}
}
}
}
MergeInto
public static void MergeInto(this JContainer left, JToken right)
{
foreach(var rightChild in right.Children < JProperty > ())
{
var rightChildProperty = rightChild;
var path = string.Empty.Equals(rightChildProperty.Name) || rightChildProperty.Name.Contains(" ") ? $ "['{rightChildProperty.Name}']" : rightChildProperty.Name;
var leftProperty = left.SelectToken(path);
if(leftProperty == null)
{
// no matching property, just add
left.Add(rightChild);
}
else
{
var leftObject = leftProperty as JObject;
if(leftObject == null)
{
// replace value
var leftParent = (JProperty) leftProperty.Parent;
leftParent.Value = rightChildProperty.Value;
}
else MergeInto(leftObject, rightChildProperty.Value);
}
}
}
CleanObject
private static void CleanObject(JToken token)
{
var list = new Dictionary < JToken,
JProperty > ();
if(token.GetType() != typeof(JObject)) return;
// remove the results nodes
var childrenWithResults = token.Children < JProperty > ().Where(c => c.Children < JObject > ()["results"].Count() > 0).ToList();
foreach(var child in childrenWithResults)
{
var resultsProperty = child.Children()["results"];
var newProperty = new JProperty(child.Name, resultsProperty.Children());
child.Replace(newProperty);
}
// remove __deferred properties
var deferredChildren = token.Children < JProperty > ().Where(c => c.Children < JObject > ()["__deferred"].Count() > 0).ToList();
foreach(var deferred in deferredChildren)
{
deferred.Remove();
}
}
MergeInto
public static void MergeInto(this JContainer left, JToken right)
{
foreach(JProperty rightChild in right.Children < JProperty > ())
{
JProperty rightChildProperty = rightChild;
JToken leftProperty = left.SelectToken(rightChildProperty.Name);
if(leftProperty == null)
{
// no matching property, just add
left.Add(rightChild);
}
else
{
var leftObject = leftProperty as JObject;
if(leftObject == null)
{
// replace value
var leftParent = (JProperty) leftProperty.Parent;
leftParent.Value = rightChildProperty.Value;
}
else
// recurse object
MergeInto(leftObject, rightChildProperty.Value);
}
}
}
MapAggregateJsonToPayroll
public Payroll[] MapAggregateJsonToPayroll(JToken aggregatePayrollJson, JToken expensesJson = null)
{
var payrollItems = new List < Payroll > ();
foreach(JToken j in aggregatePayrollJson.Children())
{
payrollItems.Add(MapJsonToPayroll(j, expensesJson));
}
return payrollItems.ToArray();
}
MergeInto
/// <summary>
/// <para>Merge the right token into the left</para>
/// </summary>
/// <param name="left">Token to be merged into</param>
/// <param name="right">Token to merge, overwriting the left</param>
/// <param name="options">Options for merge</param>
public static void MergeInto(this JContainer left, JToken right, MergeOptions options)
{
foreach(var rightChild in right.Children < JProperty > ())
{
var rightChildProperty = rightChild;
var leftPropertyValue = left.SelectToken(rightChildProperty.Name);
// add on demand only. This will keep low memory usage.
if(leftPropertyValue == null && options.ADD_NONE_EXISTING)
{
// no matching property, just add
left.Add(rightChild);
}
else
{
if(leftPropertyValue == null && !options.ADD_NONE_EXISTING)
{
// becoz we don't want to add so continue checking for next property.
continue;
}
var leftProperty = (JProperty) leftPropertyValue.Parent;
var leftArray = leftPropertyValue as JArray;
var rightArray = rightChildProperty.Value as JArray;
if(leftArray != null && rightArray != null)
{
switch(options.ArrayHandling)
{
case MergeOptionArrayHandling.Concat:
foreach(var rightValue in rightArray)
{
leftArray.Add(rightValue);
}
break;
case MergeOptionArrayHandling.Overwrite:
leftProperty.Value = rightChildProperty.Value;
break;
}
}
else
{
var leftObject = leftPropertyValue as JObject;
//only set property if it not null
if(leftObject == null && !string.IsNullOrEmpty(rightChildProperty.Value.ToString()))
{
// replace value
leftProperty.Value = rightChildProperty.Value;
}
else
// recurse object
MergeInto(leftObject, rightChildProperty.Value, options);
}
}
}
}
AllChildren
// Recursively yield all children of json
private static IEnumerable < JToken > AllChildren(JToken json)
{
foreach(var c in json.Children())
{
yield return c;
foreach(var cc in AllChildren(c))
{
yield return cc;
}
}
}
SetRequestData
public RequestWrapper SetRequestData(JToken values)
{
if (values != null)
{
foreach (JProperty item in values.Children())
if (item != null) this[item.Name] = item.Value.ToString();
}
return this;
}
Apply
public JToken Apply(JToken filtered, IFilterContext context)
{
foreach(var child in filtered.Children().Skip(_limit).ToList())
{
child.Remove();
}
return filtered;
}
FromJson
public static Result FromJson(JToken actionIdToken, JToken resultToken)
{
var actionId = actionIdToken.Value < Int32 > ();
var properties = ResultProperty.FromJson(resultToken.Children()).ToList();
return new Result(actionId, properties);
}