From 33b96c6af908cbcfb2fb7fe76fed3e676dcbf8fb Mon Sep 17 00:00:00 2001 From: Stef Heyenrath Date: Thu, 6 Sep 2018 21:00:33 +0200 Subject: [PATCH] Fix Sonar issues --- src/WireMock.Net/Matchers/LinqMatcher.cs | 2 +- src/WireMock.Net/Util/JsonUtils.cs | 190 ++++++++++++----------- 2 files changed, 104 insertions(+), 88 deletions(-) diff --git a/src/WireMock.Net/Matchers/LinqMatcher.cs b/src/WireMock.Net/Matchers/LinqMatcher.cs index 371ad9a6..d8f3e2d2 100644 --- a/src/WireMock.Net/Matchers/LinqMatcher.cs +++ b/src/WireMock.Net/Matchers/LinqMatcher.cs @@ -72,7 +72,7 @@ namespace WireMock.Matchers switch (input) { case JObject valueAsJObject: - value = valueAsJObject; // valueAsJObject.ToObject(); + value = valueAsJObject; break; default: diff --git a/src/WireMock.Net/Util/JsonUtils.cs b/src/WireMock.Net/Util/JsonUtils.cs index c35a4984..f2aae675 100644 --- a/src/WireMock.Net/Util/JsonUtils.cs +++ b/src/WireMock.Net/Util/JsonUtils.cs @@ -32,100 +32,116 @@ namespace WireMock.Util { if (node.Type == JTokenType.Object) { - var childLines = new List(); - var text = new StringBuilder("new ("); - - // In case of Object, loop all children. Do a ToArray() to avoid `Collection was modified` exceptions. - foreach (JProperty child in node.Children().ToArray()) - { - WalkNode(child.Value, child.Path, child.Name, childLines); - } - - text.Append(string.Join(", ", childLines)); - text.Append(")"); - - if (!string.IsNullOrEmpty(propertyName)) - { - text.AppendFormat(" as {0}", propertyName); - } - - lines.Add(text.ToString()); + ProcessObject(node, propertyName, lines); } else if (node.Type == JTokenType.Array) { - var items = new List(); - var text = new StringBuilder("(new [] { "); - - // In case of Array, loop all items. Do a ToArray() to avoid `Collection was modified` exceptions. - int idx = 0; - foreach (JToken child in node.Children().ToArray()) - { - WalkNode(child, $"{node.Path}[{idx}]", null, items); - idx++; - } - - text.Append(string.Join(", ", items)); - text.Append("})"); - - if (!string.IsNullOrEmpty(propertyName)) - { - text.AppendFormat(" as {0}", propertyName); - } - - lines.Add(text.ToString()); + ProcessArray(node, propertyName, lines); } else { - string castText = string.Empty; - switch (node.Type) - { - case JTokenType.Boolean: - castText = $"bool({path})"; - break; - - case JTokenType.Date: - castText = $"DateTime({path})"; - break; - - case JTokenType.Float: - castText = $"double({path})"; - break; - - case JTokenType.Guid: - castText = $"Guid({path})"; - break; - - case JTokenType.Integer: - castText = $"int({path})"; - break; - - case JTokenType.Null: - castText = "null"; - break; - - case JTokenType.String: - castText = $"string({path})"; - break; - - case JTokenType.TimeSpan: - castText = $"TimeSpan({path})"; - break; - - case JTokenType.Uri: - castText = $"Uri({path})"; - break; - - default: - throw new NotSupportedException($"JTokenType '{node.Type}' cannot be converted to a Dynamic Linq cast operator."); - } - - if (!string.IsNullOrEmpty(propertyName)) - { - castText += $" as {propertyName}"; - } - - lines.Add(castText); + ProcessItem(node, path, propertyName, lines); } } + + private static void ProcessObject(JToken node, string propertyName, List lines) + { + var items = new List(); + var text = new StringBuilder("new ("); + + // In case of Object, loop all children. Do a ToArray() to avoid `Collection was modified` exceptions. + foreach (JProperty child in node.Children().ToArray()) + { + WalkNode(child.Value, child.Path, child.Name, items); + } + + text.Append(string.Join(", ", items)); + text.Append(")"); + + if (!string.IsNullOrEmpty(propertyName)) + { + text.AppendFormat(" as {0}", propertyName); + } + + lines.Add(text.ToString()); + } + + private static void ProcessArray(JToken node, string propertyName, List lines) + { + var items = new List(); + var text = new StringBuilder("(new [] { "); + + // In case of Array, loop all items. Do a ToArray() to avoid `Collection was modified` exceptions. + int idx = 0; + foreach (JToken child in node.Children().ToArray()) + { + WalkNode(child, $"{node.Path}[{idx}]", null, items); + idx++; + } + + text.Append(string.Join(", ", items)); + text.Append("})"); + + if (!string.IsNullOrEmpty(propertyName)) + { + text.AppendFormat(" as {0}", propertyName); + } + + lines.Add(text.ToString()); + } + + private static void ProcessItem(JToken node, string path, string propertyName, List lines) + { + string castText = string.Empty; + switch (node.Type) + { + case JTokenType.Boolean: + castText = $"bool({path})"; + break; + + case JTokenType.Date: + castText = $"DateTime({path})"; + break; + + case JTokenType.Float: + castText = $"double({path})"; + break; + + case JTokenType.Guid: + castText = $"Guid({path})"; + break; + + case JTokenType.Integer: + castText = $"int({path})"; + break; + + case JTokenType.Null: + castText = "null"; + break; + + case JTokenType.String: + castText = $"string({path})"; + break; + + case JTokenType.TimeSpan: + castText = $"TimeSpan({path})"; + break; + + case JTokenType.Uri: + castText = $"Uri({path})"; + break; + + default: + throw new NotSupportedException( + $"JTokenType '{node.Type}' cannot be converted to a Dynamic Linq cast operator."); + } + + if (!string.IsNullOrEmpty(propertyName)) + { + castText += $" as {propertyName}"; + } + + lines.Add(castText); + } } } \ No newline at end of file