示例#1
0
        public static JObject InjectInfo(JObject settings, string path, string key, object value)
        {
            var container = JsonHelper.GetByPath <JObject>(settings, path);

            if (container == null)
            {
                container = new JObject();
                JsonHelper.SetByPath(settings, path, container);
            }

            JsonHelper.AddOrSet(container, key, value);

            return(container);
        }
示例#2
0
 private static void AddOrSetSettings(JContainer left, JProperty right, bool allowInternal, bool extendByDefault = false)
 {
     if (right.Value.Type == JTokenType.Object)
     {
         var obj = new JObject();
         JsonHelper.AddOrSet(left as JObject, right.Name, obj);
         UpdateSettingsWith(obj, right.Value, allowInternal, extendByDefault);
     }
     else if (right.Value.Type == JTokenType.Array)
     {
         var arr = new JArray();
         JsonHelper.AddOrSet(left as JObject, right.Name, arr);
         UpdateSettingsWith(arr, right.Value, allowInternal, extendByDefault);
     }
     else if (left is JObject)
     {
         JsonHelper.AddOrSet(left as JObject, right.Name, right.Value.DeepClone());
     }
     else if (left is JArray)
     {
         UpdateSettingsWith(left as JArray, right.Value as JArray, allowInternal, extendByDefault);
     }
 }
        public void AddOrUpdateEntry(ProcessResult result, string key, string text, int pos = 0)
        {
            var keyNormalized = StringHelper.GetNormalizedKey(key);

            if (!key.Equals(keyNormalized))
            {
                result.addInfo(
                    pos > 0
                        ? string.Format("normalizing key {0} to {1} at {2}", key, keyNormalized, pos)
                        : string.Format("normalizing key {0} to {1}", key, keyNormalized)
                    );
            }

            var translations = result.translations;

            if (translations[commonKey] == null)
            {
                translations.Add(commonKey, new JObject());
            }

            if (string.IsNullOrEmpty(key) || key.EndsWith(keyPartDelimiter))
            {
                result.addInfo($"cannot add node «{keyNormalized}»");
                return;
            }

            JToken parent = translations;
            string parentKey, subKey;
            var    o = FindByKey(translations, key, out parent, out parentKey, out subKey);

            if (o == null)
            {
                if (!key.Contains(keyPartDelimiter))
                {
                    parent = translations[commonKey];
                    subKey = key;
                }
                else
                {
                    o = AddByKey(translations, key, out parent, out parentKey, out subKey);
                }
            }

            if (parent is JObject)
            {
                // Alle Zeilenumbrüche eliminieren im Text
                if (text?.Contains(Environment.NewLine) ?? false)
                {
                    text = GetStringWithoutLineBreaks(text);
                }

                var old = o?.ToString();
                if (string.IsNullOrEmpty(old) || !text.Equals(old))
                {
                    if (string.IsNullOrEmpty(subKey))
                    {
                        result.addInfo(string.Format("cannot add node «{0}» at {1}, text='{2}'", keyNormalized, parent.Path, text));
                    }
                    else if (!string.IsNullOrEmpty(old))
                    {
                        if (!string.IsNullOrEmpty(text) && !text.Equals(old) && !key.Equals(old))
                        {
                            result.addInfo($"wont override «{keyNormalized}»: old='{old}', new='{text}'");
                        }
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(text) && !result.forDefaultLanguage)
                        {
                            o = FindByKey(result.defaultTranslations, key, out parent, out parentKey, out subKey);
                            if (o != null)
                            {
                                text = o.ToString();
                            }
                        }

                        if (string.IsNullOrEmpty(text))
                        {
                            result.addInfo(string.Format("empty text for «{0}»", keyNormalized));
                        }
                        else if (!result.forDefaultLanguage)
                        {
                            text = result.language + ":" + text;
                            result.addInfo(string.Format("added default text for «{0}»: {1}", keyNormalized, text));
                        }

                        var subKeyNLC = StringHelper.GetNormalizedKey(subKey).ToLowerCamelCase();
                        JsonHelper.AddOrSet(parent as JObject, subKeyNLC, text);
                    }
                }
                else
                {
                    var keyNormaliedLowerCamelCase = keyNormalized.ToLowerCamelCase();
                    var keyC = StringHelper.AddToString(parentKey, keyPartDelimiter, subKey);
                    if (!keyNormaliedLowerCamelCase.Equals(keyC))
                    {
                        result.addInfo(string.Format("existing entry for «{0}» with equal text='{1}' but non-normalized key «{2}»",
                                                     keyNormaliedLowerCamelCase, text, keyC));
                    }
                }
            }
            else
            {
                result.addInfo(string.Format("no parent for «{0}», text='{1}'", keyNormalized, text));
            }
        }