Summary

Class:WireMock.Util.JsonUtils
Assembly:WireMock.Net
File(s):C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Util\JsonUtils.cs
Covered lines:84
Uncovered lines:0
Coverable lines:84
Total lines:147
Line coverage:100%
Branch coverage:100%

Metrics

MethodCyclomatic complexity NPath complexity Sequence coverage Branch coverage
ParseJTokenToObject(...)0011
GenerateDynamicLinqStatement(...)0010
WalkNode(...)0011
ProcessObject(...)0011
ProcessArray(...)0011
ProcessItem(...)0011

File(s)

C:\Users\StefHeyenrath\Documents\GitHub\WireMock.Net\src\WireMock.Net\Util\JsonUtils.cs

#LineLine coverage
 1using Newtonsoft.Json.Linq;
 2using System;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Text;
 6
 7namespace WireMock.Util
 8{
 9    internal static class JsonUtils
 10    {
 11        public static T ParseJTokenToObject<T>(object value)
 1112        {
 1113            switch (value)
 14            {
 15                case JToken tokenValue:
 1016                    return tokenValue.ToObject<T>();
 17
 18                default:
 119                    return default(T);
 20            }
 1121        }
 22
 23        public static string GenerateDynamicLinqStatement(JToken jsonObject)
 1124        {
 1125            var lines = new List<string>();
 1126            WalkNode(jsonObject, null, null, lines);
 27
 1028            return lines.First();
 1029        }
 30
 31        private static void WalkNode(JToken node, string path, string propertyName, List<string> lines)
 4132        {
 4133            if (node.Type == JTokenType.Object)
 1034            {
 1035                ProcessObject(node, propertyName, lines);
 936            }
 3137            else if (node.Type == JTokenType.Array)
 138            {
 139                ProcessArray(node, propertyName, lines);
 140            }
 41            else
 3042            {
 3043                ProcessItem(node, path ?? "it", propertyName, lines);
 2944            }
 3945        }
 46
 47        private static void ProcessObject(JToken node, string propertyName, List<string> lines)
 1048        {
 1049            var items = new List<string>();
 1050            var text = new StringBuilder("new (");
 51
 52            // In case of Object, loop all children. Do a ToArray() to avoid `Collection was modified` exceptions.
 8553            foreach (JProperty child in node.Children<JProperty>().ToArray())
 2854            {
 2855                WalkNode(child.Value, child.Path, child.Name, items);
 2756            }
 57
 958            text.Append(string.Join(", ", items));
 959            text.Append(")");
 60
 961            if (!string.IsNullOrEmpty(propertyName))
 162            {
 163                text.AppendFormat(" as {0}", propertyName);
 164            }
 65
 966            lines.Add(text.ToString());
 967        }
 68
 69        private static void ProcessArray(JToken node, string propertyName, List<string> lines)
 170        {
 171            var items = new List<string>();
 172            var text = new StringBuilder("(new [] { ");
 73
 74            // In case of Array, loop all items. Do a ToArray() to avoid `Collection was modified` exceptions.
 175            int idx = 0;
 776            foreach (JToken child in node.Children().ToArray())
 277            {
 278                WalkNode(child, $"{node.Path}[{idx}]", null, items);
 279                idx++;
 280            }
 81
 182            text.Append(string.Join(", ", items));
 183            text.Append("})");
 84
 185            if (!string.IsNullOrEmpty(propertyName))
 186            {
 187                text.AppendFormat(" as {0}", propertyName);
 188            }
 89
 190            lines.Add(text.ToString());
 191        }
 92
 93        private static void ProcessItem(JToken node, string path, string propertyName, List<string> lines)
 3094        {
 3095            string castText = string.Empty;
 3096            switch (node.Type)
 97            {
 98                case JTokenType.Boolean:
 199                    castText = $"bool({path})";
 1100                    break;
 101
 102                case JTokenType.Date:
 1103                    castText = $"DateTime({path})";
 1104                    break;
 105
 106                case JTokenType.Float:
 2107                    castText = $"double({path})";
 2108                    break;
 109
 110                case JTokenType.Guid:
 1111                    castText = $"Guid({path})";
 1112                    break;
 113
 114                case JTokenType.Integer:
 11115                    castText = $"int({path})";
 11116                    break;
 117
 118                case JTokenType.Null:
 1119                    castText = "null";
 1120                    break;
 121
 122                case JTokenType.String:
 10123                    castText = $"string({path})";
 10124                    break;
 125
 126                case JTokenType.TimeSpan:
 1127                    castText = $"TimeSpan({path})";
 1128                    break;
 129
 130                case JTokenType.Uri:
 1131                    castText = $"Uri({path})";
 1132                    break;
 133
 134                default:
 1135                    throw new NotSupportedException(
 1136                        $"JTokenType '{node.Type}' cannot be converted to a Dynamic Linq cast operator.");
 137            }
 138
 29139            if (!string.IsNullOrEmpty(propertyName))
 25140            {
 25141                castText += $" as {propertyName}";
 25142            }
 143
 29144            lines.Add(castText);
 29145        }
 146    }
 147}