| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Nodes; |
| | | 3 | | using System.Text.Json.Serialization; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Api.Client.Extensions; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides extension methods for <see cref="JsonObject"/>. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class JsonObjectExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Returns true if the specified <see cref="JsonObject"/> represents an activity. |
| | | 14 | | /// </summary> |
| | | 15 | | public static bool IsActivity(this JsonObject obj) |
| | | 16 | | { |
| | 0 | 17 | | return obj.ContainsKey("type") && obj.ContainsKey("id") && obj.ContainsKey("version"); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Serializes the specified value to a <see cref="JsonObject"/>. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="value">The value to serialize.</param> |
| | | 24 | | /// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param> |
| | | 25 | | /// <returns>A <see cref="JsonObject"/> representing the specified value.</returns> |
| | | 26 | | public static JsonNode SerializeToNode(this object value, JsonSerializerOptions? options = null) |
| | | 27 | | { |
| | 0 | 28 | | options ??= (new JsonSerializerOptions |
| | 0 | 29 | | { |
| | 0 | 30 | | PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| | 0 | 31 | | |
| | 0 | 32 | | }).WithConverters(new JsonStringEnumConverter()); |
| | | 33 | | |
| | 0 | 34 | | return JsonSerializer.SerializeToNode(value, options)!; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Serializes the specified value to a <see cref="JsonArray"/>. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="value">The value to serialize.</param> |
| | | 41 | | /// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param> |
| | | 42 | | /// <returns>A <see cref="JsonObject"/> representing the specified value.</returns> |
| | | 43 | | public static JsonArray SerializeToArray(this object value, JsonSerializerOptions? options = null) |
| | | 44 | | { |
| | 0 | 45 | | options ??= new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; |
| | | 46 | | |
| | 0 | 47 | | return JsonSerializer.SerializeToNode(value, options)!.AsArray(); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Serializes the specified value to a <see cref="JsonArray"/>. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="value">The value to serialize.</param> |
| | | 54 | | /// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param> |
| | | 55 | | /// <returns>A <see cref="JsonObject"/> representing the specified value.</returns> |
| | | 56 | | public static JsonArray SerializeToArray<T>(this IEnumerable<T> value, JsonSerializerOptions? options = null) |
| | | 57 | | { |
| | 0 | 58 | | options ??= new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; |
| | | 59 | | |
| | 0 | 60 | | return JsonSerializer.SerializeToNode(value, options)!.AsArray(); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Deserializes the specified <see cref="JsonNode"/> to the specified type. |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="value">The <see cref="JsonNode"/> to deserialize.</param> |
| | | 67 | | /// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param> |
| | | 68 | | /// <typeparam name="T">The type to deserialize to.</typeparam> |
| | | 69 | | /// <returns>The deserialized value.</returns> |
| | | 70 | | public static T Deserialize<T>(this JsonNode value, JsonSerializerOptions? options = null) |
| | | 71 | | { |
| | 0 | 72 | | options ??= new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; |
| | | 73 | | |
| | 0 | 74 | | if (value is JsonObject jsonObject) |
| | 0 | 75 | | return JsonSerializer.Deserialize<T>(jsonObject, options)!; |
| | | 76 | | |
| | 0 | 77 | | if (value is JsonArray jsonArray) |
| | 0 | 78 | | return JsonSerializer.Deserialize<T>(jsonArray, options)!; |
| | | 79 | | |
| | 0 | 80 | | if (typeof(T).IsEnum || (Nullable.GetUnderlyingType(typeof(T))?.IsEnum ?? false)) |
| | | 81 | | { |
| | 0 | 82 | | if (value.GetValueKind() == JsonValueKind.Null) |
| | 0 | 83 | | return default!; |
| | 0 | 84 | | return (T)Enum.Parse(Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T), value.ToString()); |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | if (value is JsonValue jsonValue) |
| | 0 | 88 | | return jsonValue.GetValue<T>(); |
| | | 89 | | |
| | 0 | 90 | | throw new NotSupportedException($"Cannot deserialize {value.GetType()} to {typeof(T)}."); |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Sets the property value of the specified model. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="model">The model to set the property value on.</param> |
| | | 97 | | /// <param name="value">The value to set.</param> |
| | | 98 | | /// <param name="path">The path to the property.</param> |
| | | 99 | | public static void SetProperty(this JsonObject model, JsonNode? value, params string[] path) |
| | | 100 | | { |
| | 0 | 101 | | model = GetPropertyContainer(model, path); |
| | 0 | 102 | | model[path.Last()] = value?.SerializeToNode(); |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | /// <summary> |
| | | 106 | | /// Sets the property value of the specified model. |
| | | 107 | | /// </summary> |
| | | 108 | | /// <param name="model">The model to set the property value on.</param> |
| | | 109 | | /// <param name="value">The value to set.</param> |
| | | 110 | | /// <param name="path">The path to the property.</param> |
| | | 111 | | public static void SetProperty(this JsonObject model, JsonArray? value, params string[] path) |
| | | 112 | | { |
| | 0 | 113 | | model = GetPropertyContainer(model, path); |
| | 0 | 114 | | model[path.Last()] = value?.SerializeToNode(); |
| | 0 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Sets the property value of the specified model. |
| | | 119 | | /// </summary> |
| | | 120 | | /// <param name="model">The model to set the property value on.</param> |
| | | 121 | | /// <param name="value">The value to set.</param> |
| | | 122 | | /// <param name="path">The path to the property.</param> |
| | | 123 | | public static void SetProperty(this JsonObject model, IEnumerable<JsonNode> value, params string[] path) |
| | | 124 | | { |
| | 0 | 125 | | model = GetPropertyContainer(model, path); |
| | 0 | 126 | | model[path.Last()] = new JsonArray(value.Select(x => x.SerializeToNode()).ToArray()); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the property value of the specified model. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <param name="model">The model to get the property value from.</param> |
| | | 133 | | /// <param name="path">The path to the property.</param> |
| | | 134 | | /// <returns>The property value.</returns> |
| | | 135 | | public static JsonNode? GetProperty(this JsonObject model, params string[] path) |
| | | 136 | | { |
| | 0 | 137 | | var currentModel = model; |
| | | 138 | | |
| | 0 | 139 | | foreach (var prop in path.SkipLast(1)) |
| | | 140 | | { |
| | 0 | 141 | | if (currentModel[prop] is not JsonObject value) |
| | 0 | 142 | | return null; |
| | | 143 | | |
| | 0 | 144 | | currentModel = value; |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | return currentModel[path.Last()]; |
| | 0 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Gets the property value of the specified model. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <param name="model">The model to get the property value from.</param> |
| | | 154 | | /// <param name="path">The path to the property.</param> |
| | | 155 | | /// <typeparam name="T">The type to deserialize to.</typeparam> |
| | | 156 | | /// <returns>The property value.</returns> |
| | | 157 | | public static T? TryGetProperty<T>(this JsonObject model, params string[] path) |
| | | 158 | | { |
| | | 159 | | try |
| | | 160 | | { |
| | 0 | 161 | | return model.GetProperty<T>(path); |
| | | 162 | | } |
| | 0 | 163 | | catch (Exception) |
| | | 164 | | { |
| | 0 | 165 | | return default; |
| | | 166 | | } |
| | 0 | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Gets the property value of the specified model. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <param name="model">The model to get the property value from.</param> |
| | | 173 | | /// <param name="path">The path to the property.</param> |
| | | 174 | | /// <typeparam name="T">The type to deserialize to.</typeparam> |
| | | 175 | | /// <returns>The property value.</returns> |
| | | 176 | | public static T? GetProperty<T>(this JsonObject model, params string[] path) |
| | | 177 | | { |
| | 0 | 178 | | var property = GetProperty(model, path); |
| | 0 | 179 | | return property != null ? property.Deserialize<T>() : default; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | /// <summary> |
| | | 183 | | /// Gets the property value of the specified model. |
| | | 184 | | /// </summary> |
| | | 185 | | /// <param name="model">The model to get the property value from.</param> |
| | | 186 | | /// <param name="options">The <see cref="JsonSerializerOptions"/> to use when deserializing.</param> |
| | | 187 | | /// <param name="path">The path to the property.</param> |
| | | 188 | | /// <typeparam name="T">The type to deserialize to.</typeparam> |
| | | 189 | | /// <returns>The property value.</returns> |
| | | 190 | | public static T? GetProperty<T>(this JsonObject model, JsonSerializerOptions options, params string[] path) |
| | | 191 | | { |
| | 0 | 192 | | var property = GetProperty(model, path); |
| | 0 | 193 | | return property != null ? property.Deserialize<T>(options) : default; |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | /// <summary> |
| | | 197 | | /// Returns the property container of the specified model. |
| | | 198 | | /// </summary> |
| | | 199 | | /// <param name="model">The model to set the property value on.</param> |
| | | 200 | | /// <param name="path">The path to the property.</param> |
| | | 201 | | private static JsonObject GetPropertyContainer(this JsonObject model, params string[] path) |
| | | 202 | | { |
| | 0 | 203 | | foreach (var prop in path.SkipLast(1)) |
| | | 204 | | { |
| | 0 | 205 | | var property = model[prop] as JsonObject ?? new JsonObject(); |
| | 0 | 206 | | model[prop] = property; |
| | 0 | 207 | | model = property; |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | return model; |
| | | 211 | | } |
| | | 212 | | } |