PackUtils/JsonUtility.cs
/// <summary>
/// Rename all property found in jtoken and childrens (core / recursive method)
/// </summary>
/// <param name="jtoken"></param>
/// <param name="oldPropertyName"></param>
/// <param name="newPropertyName"></param>
/// <param name="handleValue"></param>
/// <returns></returns>
Rename all property found in jtoken and childrens (core / recursive method)
Rename all property found in jtoken and childrens ( core / recursive method )
private static void RenamePropertyFromJToken(JToken token, string oldPropertyName, string newPropertyName, Func<JToken, JToken> handleValue)
        {
            JContainer container = token as JContainer;
            if (container == null)
            {
                return; 
            }
            List<JToken> removeList = new List<JToken>();
            foreach (JToken jtoken in container.Children())
            {
                if (jtoken is JProperty prop)
                {
                    var matching = Regex.IsMatch(prop.Path, $"*.{oldPropertyName}".ToWildcardToRegular(), RegexOptions.IgnoreCase | RegexOptions.CultureInvariant) ||
                                    string.Equals(prop.Path, oldPropertyName, StringComparison.InvariantCultureIgnoreCase);
                    if (matching)
                    {
                        removeList.Add(jtoken);
                    }
                }
                RenamePropertyFromJToken(jtoken, oldPropertyName, newPropertyName, handleValue);
            }
            for (int i = 0; i < removeList.Count; i++)
            {
                var prop = (JProperty)removeList[i];
                if (handleValue != null)
                {
                    prop.Value = (string)handleValue.Invoke(prop.Value);
                }
                prop.Rename(newPropertyName);
            }
        }
private static void RenamePropertyFromJToken ( JToken token , string oldPropertyName , string newPropertyName , Func < JToken , JToken > handleValue ) { JContainer container = token as JContainer ; if ( container == null ) { return ; } List < JToken > removeList = new List < JToken > ( ) ; foreach ( JToken jtoken in container . Children ( ) ) { if ( jtoken is JProperty prop ) { var matching = Regex . IsMatch ( prop . Path , $" *. { oldPropertyName } " . ToWildcardToRegular ( ) , RegexOptions . IgnoreCase | RegexOptions . CultureInvariant ) || string . Equals ( prop . Path , oldPropertyName , StringComparison . InvariantCultureIgnoreCase ) ; if ( matching ) { removeList . Add ( jtoken ) ; } } RenamePropertyFromJToken ( jtoken , oldPropertyName , newPropertyName , handleValue ) ; } for ( int i = 0 ; i < removeList . Count ; i ++ ) { var prop = ( JProperty ) removeList [ i ] ; if ( handleValue != null ) { prop . Value = ( string ) handleValue . Invoke ( prop . Value ) ; } prop . Rename ( newPropertyName ) ; } }
Rename all property found in jtoken and childrens (core / recursive method)
Rename all property found in jtoken and childrens ( core / recursive method )
// abort recursive // call recursive  // replace 
None
token
JToken
oldPropertyName
string
None
newPropertyName
string
None
handleValue
Func<JToken, JToken>
None
jtoken
None