| | | 1 | | using Newtonsoft.Json.Linq; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Text; |
| | | 6 | | |
| | | 7 | | namespace WireMock.Util |
| | | 8 | | { |
| | | 9 | | internal static class JsonUtils |
| | | 10 | | { |
| | | 11 | | public static T ParseJTokenToObject<T>(object value) |
| | 11 | 12 | | { |
| | 11 | 13 | | switch (value) |
| | | 14 | | { |
| | | 15 | | case JToken tokenValue: |
| | 10 | 16 | | return tokenValue.ToObject<T>(); |
| | | 17 | | |
| | | 18 | | default: |
| | 1 | 19 | | return default(T); |
| | | 20 | | } |
| | 11 | 21 | | } |
| | | 22 | | |
| | | 23 | | public static string GenerateDynamicLinqStatement(JToken jsonObject) |
| | 11 | 24 | | { |
| | 11 | 25 | | var lines = new List<string>(); |
| | 11 | 26 | | WalkNode(jsonObject, null, null, lines); |
| | | 27 | | |
| | 10 | 28 | | return lines.First(); |
| | 10 | 29 | | } |
| | | 30 | | |
| | | 31 | | private static void WalkNode(JToken node, string path, string propertyName, List<string> lines) |
| | 41 | 32 | | { |
| | 41 | 33 | | if (node.Type == JTokenType.Object) |
| | 10 | 34 | | { |
| | 10 | 35 | | ProcessObject(node, propertyName, lines); |
| | 9 | 36 | | } |
| | 31 | 37 | | else if (node.Type == JTokenType.Array) |
| | 1 | 38 | | { |
| | 1 | 39 | | ProcessArray(node, propertyName, lines); |
| | 1 | 40 | | } |
| | | 41 | | else |
| | 30 | 42 | | { |
| | 30 | 43 | | ProcessItem(node, path ?? "it", propertyName, lines); |
| | 29 | 44 | | } |
| | 39 | 45 | | } |
| | | 46 | | |
| | | 47 | | private static void ProcessObject(JToken node, string propertyName, List<string> lines) |
| | 10 | 48 | | { |
| | 10 | 49 | | var items = new List<string>(); |
| | 10 | 50 | | var text = new StringBuilder("new ("); |
| | | 51 | | |
| | | 52 | | // In case of Object, loop all children. Do a ToArray() to avoid `Collection was modified` exceptions. |
| | 85 | 53 | | foreach (JProperty child in node.Children<JProperty>().ToArray()) |
| | 28 | 54 | | { |
| | 28 | 55 | | WalkNode(child.Value, child.Path, child.Name, items); |
| | 27 | 56 | | } |
| | | 57 | | |
| | 9 | 58 | | text.Append(string.Join(", ", items)); |
| | 9 | 59 | | text.Append(")"); |
| | | 60 | | |
| | 9 | 61 | | if (!string.IsNullOrEmpty(propertyName)) |
| | 1 | 62 | | { |
| | 1 | 63 | | text.AppendFormat(" as {0}", propertyName); |
| | 1 | 64 | | } |
| | | 65 | | |
| | 9 | 66 | | lines.Add(text.ToString()); |
| | 9 | 67 | | } |
| | | 68 | | |
| | | 69 | | private static void ProcessArray(JToken node, string propertyName, List<string> lines) |
| | 1 | 70 | | { |
| | 1 | 71 | | var items = new List<string>(); |
| | 1 | 72 | | var text = new StringBuilder("(new [] { "); |
| | | 73 | | |
| | | 74 | | // In case of Array, loop all items. Do a ToArray() to avoid `Collection was modified` exceptions. |
| | 1 | 75 | | int idx = 0; |
| | 7 | 76 | | foreach (JToken child in node.Children().ToArray()) |
| | 2 | 77 | | { |
| | 2 | 78 | | WalkNode(child, $"{node.Path}[{idx}]", null, items); |
| | 2 | 79 | | idx++; |
| | 2 | 80 | | } |
| | | 81 | | |
| | 1 | 82 | | text.Append(string.Join(", ", items)); |
| | 1 | 83 | | text.Append("})"); |
| | | 84 | | |
| | 1 | 85 | | if (!string.IsNullOrEmpty(propertyName)) |
| | 1 | 86 | | { |
| | 1 | 87 | | text.AppendFormat(" as {0}", propertyName); |
| | 1 | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | lines.Add(text.ToString()); |
| | 1 | 91 | | } |
| | | 92 | | |
| | | 93 | | private static void ProcessItem(JToken node, string path, string propertyName, List<string> lines) |
| | 30 | 94 | | { |
| | 30 | 95 | | string castText = string.Empty; |
| | 30 | 96 | | switch (node.Type) |
| | | 97 | | { |
| | | 98 | | case JTokenType.Boolean: |
| | 1 | 99 | | castText = $"bool({path})"; |
| | 1 | 100 | | break; |
| | | 101 | | |
| | | 102 | | case JTokenType.Date: |
| | 1 | 103 | | castText = $"DateTime({path})"; |
| | 1 | 104 | | break; |
| | | 105 | | |
| | | 106 | | case JTokenType.Float: |
| | 2 | 107 | | castText = $"double({path})"; |
| | 2 | 108 | | break; |
| | | 109 | | |
| | | 110 | | case JTokenType.Guid: |
| | 1 | 111 | | castText = $"Guid({path})"; |
| | 1 | 112 | | break; |
| | | 113 | | |
| | | 114 | | case JTokenType.Integer: |
| | 11 | 115 | | castText = $"int({path})"; |
| | 11 | 116 | | break; |
| | | 117 | | |
| | | 118 | | case JTokenType.Null: |
| | 1 | 119 | | castText = "null"; |
| | 1 | 120 | | break; |
| | | 121 | | |
| | | 122 | | case JTokenType.String: |
| | 10 | 123 | | castText = $"string({path})"; |
| | 10 | 124 | | break; |
| | | 125 | | |
| | | 126 | | case JTokenType.TimeSpan: |
| | 1 | 127 | | castText = $"TimeSpan({path})"; |
| | 1 | 128 | | break; |
| | | 129 | | |
| | | 130 | | case JTokenType.Uri: |
| | 1 | 131 | | castText = $"Uri({path})"; |
| | 1 | 132 | | break; |
| | | 133 | | |
| | | 134 | | default: |
| | 1 | 135 | | throw new NotSupportedException( |
| | 1 | 136 | | $"JTokenType '{node.Type}' cannot be converted to a Dynamic Linq cast operator."); |
| | | 137 | | } |
| | | 138 | | |
| | 29 | 139 | | if (!string.IsNullOrEmpty(propertyName)) |
| | 25 | 140 | | { |
| | 25 | 141 | | castText += $" as {propertyName}"; |
| | 25 | 142 | | } |
| | | 143 | | |
| | 29 | 144 | | lines.Add(castText); |
| | 29 | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |