| | | 1 | | using System.Collections; |
| | | 2 | | using System.Text.Json.Nodes; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.OpenApi; |
| | | 5 | | /// <summary> |
| | | 6 | | /// Helpers to create System.Text.Json.Nodes from .NET objects for OpenAPI representation. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class OpenApiJsonNodeFactory |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Create a JsonNode from a .NET object. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="value">The .NET object to convert.</param> |
| | | 14 | | /// <returns>A JsonNode representation of the object.</returns> |
| | | 15 | | public static JsonNode? FromObject(object? value) |
| | | 16 | | { |
| | 0 | 17 | | return value is null |
| | 0 | 18 | | ? null |
| | 0 | 19 | | : value switch |
| | 0 | 20 | | { |
| | 0 | 21 | | bool b => JsonValue.Create(b), |
| | 0 | 22 | | string s => JsonValue.Create(s), |
| | 0 | 23 | | sbyte or byte or short or ushort or int or uint or long or ulong => JsonValue.Create(Convert.ToInt64(val |
| | 0 | 24 | | float or double or decimal => JsonValue.Create(Convert.ToDouble(value)), |
| | 0 | 25 | | DateTime dt => JsonValue.Create(dt.ToString("o")), |
| | 0 | 26 | | Guid g => JsonValue.Create(g.ToString()), |
| | 0 | 27 | | IDictionary dict => ToJsonObject(dict), |
| | 0 | 28 | | IEnumerable en when value is not string => ToJsonArray(en), |
| | 0 | 29 | | _ => FromPocoOrString(value), |
| | 0 | 30 | | }; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | private static JsonObject ToJsonObject(IDictionary dict) |
| | | 34 | | { |
| | 0 | 35 | | var obj = new JsonObject(); |
| | 0 | 36 | | foreach (DictionaryEntry de in dict) |
| | | 37 | | { |
| | 0 | 38 | | if (de.Key is null) |
| | | 39 | | { |
| | | 40 | | continue; |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | var k = de.Key.ToString() ?? string.Empty; |
| | 0 | 44 | | obj[k] = FromObject(de.Value); |
| | | 45 | | } |
| | 0 | 46 | | return obj; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | private static JsonArray ToJsonArray(IEnumerable en) |
| | | 50 | | { |
| | 0 | 51 | | var arr = new JsonArray(); |
| | 0 | 52 | | foreach (var item in en) |
| | | 53 | | { |
| | 0 | 54 | | arr.Add(FromObject(item)); |
| | | 55 | | } |
| | 0 | 56 | | return arr; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | private static JsonNode FromPocoOrString(object value) |
| | | 60 | | { |
| | 0 | 61 | | var t = value.GetType(); |
| | 0 | 62 | | if (!t.IsPrimitive && t != typeof(string) && !typeof(IEnumerable).IsAssignableFrom(t)) |
| | | 63 | | { |
| | 0 | 64 | | var props = t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance) |
| | 0 | 65 | | if (props.Length > 0) |
| | | 66 | | { |
| | 0 | 67 | | var obj = new JsonObject(); |
| | 0 | 68 | | foreach (var p in props) |
| | | 69 | | { |
| | 0 | 70 | | if (!p.CanRead) |
| | | 71 | | { |
| | | 72 | | continue; |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | var v = p.GetValue(value); |
| | 0 | 76 | | if (v is null) |
| | | 77 | | { |
| | | 78 | | continue; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | obj[p.Name] = FromObject(v); |
| | | 82 | | } |
| | 0 | 83 | | return obj; |
| | | 84 | | } |
| | | 85 | | } |
| | 0 | 86 | | return JsonValue.Create(value?.ToString() ?? string.Empty)!; |
| | | 87 | | } |
| | | 88 | | } |