|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | + |
| 5 | +namespace KY.Core; |
| 6 | + |
| 7 | +public class JsonObject |
| 8 | +{ |
| 9 | + public void Set<T>(JToken target, string propertyPath, T value) |
| 10 | + { |
| 11 | + if (value is JToken token) |
| 12 | + { |
| 13 | + this.Set(target, propertyPath, token); |
| 14 | + } |
| 15 | + else |
| 16 | + { |
| 17 | + this.Set(target, propertyPath, (JToken)new JValue(value)); |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + public void Set(JToken target, string propertyPath, JToken value) |
| 22 | + { |
| 23 | + (string objectPath, string propertyName) = this.GetProperty(propertyPath); |
| 24 | + JToken token = string.IsNullOrEmpty(objectPath) ? target : target.SelectToken(objectPath); |
| 25 | + if (token is not JObject obj) |
| 26 | + { |
| 27 | + throw new InvalidOperationException($"The property {objectPath} is not an object"); |
| 28 | + } |
| 29 | + obj[propertyName] = value; |
| 30 | + } |
| 31 | + |
| 32 | + public void SetFirst<T>(JToken target, string propertyPath, T value) |
| 33 | + { |
| 34 | + if (value is JToken token) |
| 35 | + { |
| 36 | + this.SetFirst(target, propertyPath, token); |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + this.SetFirst(target, propertyPath, (JToken)new JValue(value)); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public void SetFirst(JToken target, string propertyPath, JToken value) |
| 45 | + { |
| 46 | + (string objectPath, string propertyName) = this.GetProperty(propertyPath); |
| 47 | + JToken token = string.IsNullOrEmpty(objectPath) ? target : target.SelectToken(objectPath); |
| 48 | + if (token is not JObject obj) |
| 49 | + { |
| 50 | + throw new InvalidOperationException($"The property {objectPath} is not an object"); |
| 51 | + } |
| 52 | + if (obj.ContainsKey(propertyName)) |
| 53 | + { |
| 54 | + obj[propertyName] = value; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + obj.AddFirst(new JProperty(propertyName, value)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public void SetAfter<T>(JToken target, string propertyPath, string afterProperty, T value) |
| 63 | + { |
| 64 | + if (value is JToken token) |
| 65 | + { |
| 66 | + this.SetAfter(target, propertyPath, afterProperty, token); |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + this.SetAfter(target, propertyPath, afterProperty, (JToken)new JValue(value)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public void SetAfter(JToken target, string propertyPath, string afterProperty, JToken value) |
| 75 | + { |
| 76 | + (string objectPath, string propertyName) = this.GetProperty(propertyPath); |
| 77 | + JToken token = string.IsNullOrEmpty(objectPath) ? target : target.SelectToken(objectPath); |
| 78 | + if (token is not JObject obj) |
| 79 | + { |
| 80 | + throw new InvalidOperationException($"The property {objectPath} is not an object"); |
| 81 | + } |
| 82 | + if (obj.ContainsKey(propertyName) || !obj.ContainsKey(afterProperty)) |
| 83 | + { |
| 84 | + obj[propertyName] = value; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + obj[afterProperty]?.Parent?.AddAfterSelf(new JProperty(propertyName, value)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public void Remove(JToken target, string propertyPath) |
| 93 | + { |
| 94 | + (string objectPath, string propertyName) = this.GetProperty(propertyPath); |
| 95 | + JToken token = string.IsNullOrEmpty(objectPath) ? target : target.SelectToken(objectPath); |
| 96 | + if (token is not JObject obj) |
| 97 | + { |
| 98 | + throw new InvalidOperationException($"The property {objectPath} is not an object"); |
| 99 | + } |
| 100 | + obj.Remove(propertyName); |
| 101 | + } |
| 102 | + |
| 103 | + private Tuple<string, string> GetProperty(string propertyPath) |
| 104 | + { |
| 105 | + string[] chunks = propertyPath.Split('.'); |
| 106 | + return new Tuple<string, string>(string.Join(".", chunks.Take(chunks.Length - 1)), chunks.Last()); |
| 107 | + } |
| 108 | +} |
0 commit comments