Пример #1
0
        public static Dictionary<String, JsonResponseEntry> analyzeResponseEntry(JToken jToken)
        {
            Dictionary<String, JsonResponseEntry> responseEntry = new Dictionary<string, JsonResponseEntry>();

            if (jToken is JArray)
            {
                //约定key为""且dict仅有1项表示被解析的JToken是一个Array,暂时仅支持ENTRY_LIST的根
                String key = "";
                JsonResponseEntry entry = new JsonResponseEntry();
                JArray ja = jToken as JArray;
                if (ja.Count != 0)
                {
                    if (ja[0] is JObject)
                    {
                        entry.type = JsonResponseEntry.ResponseEntryType.LIST_ENTRY;
                        List<Dictionary<String, JsonResponseEntry>> list = new List<Dictionary<string, JsonResponseEntry>>();
                        for (int i = 0; i < ja.Count; i++)
                        {
                            JObject jo = (JObject)ja[i];
                            list.Add(analyzeResponseEntry(jo));
                        }
                        entry.listEntry = list;
                        responseEntry.Add(key, entry);
                    }
                }
            }
            else if (jToken is JObject)
            {
                foreach (JProperty jpItem in ((JObject)jToken).Children())
                {
                    String key = jpItem.Name;
                    JToken jt = jpItem.Value;
                    String value = jpItem.Value.ToString();
                    JsonResponseEntry entry = new JsonResponseEntry();
                    entry.key = key;

                    //null类型
                    if (jpItem.Value is JValue && ((JValue)(jpItem.Value)).Value == null)
                    {
                        entry.type = JsonResponseEntry.ResponseEntryType.NULL;
                    }
                    //dict类型,内容为空
                    else if (value == "{}")
                    {
                        entry.type = ApiTestExtension.DataStructure.JsonAnalyzer.JsonResponseEntry.ResponseEntryType.DICT_NULL;
                    }
                    //value为String空
                    else if (value == "")
                    {
                        entry.type = JsonResponseEntry.ResponseEntryType.STRING;
                        entry.stringValue = value;
                    }
                    //dict类型
                    else if (jpItem.Value is JObject)
                    {
                        entry.type = JsonResponseEntry.ResponseEntryType.DICT;
                        Dictionary<String, JsonResponseEntry> subEntry = new Dictionary<string, JsonResponseEntry>();
                        entry.subEntry = analyzeResponseEntry(JObject.Parse(value));
                    }
                    //list类型
                    else if (jpItem.Value is JArray)
                    {
                        JArray ja = JArray.Parse(value);
                        if (ja.Count == 0)
                        {
                            entry.type = JsonResponseEntry.ResponseEntryType.LIST_NULL;
                        }
                        else
                        {
                            if (ja[0] is JObject)
                            {
                                entry.type = JsonResponseEntry.ResponseEntryType.LIST_ENTRY;
                                List<Dictionary<String, JsonResponseEntry>> list = new List<Dictionary<string, JsonResponseEntry>>();
                                for (int i = 0; i < ja.Count; i++)
                                {
                                    JObject jo = (JObject)ja[i];
                                    list.Add(analyzeResponseEntry(jo));
                                }
                                entry.listEntry = list;
                            }
                            else
                            {
                                entry.type = JsonResponseEntry.ResponseEntryType.LIST_STRING;
                                List<String> list = new List<string>();
                                for (int i = 0; i < ja.Count; i++)
                                {
                                    list.Add(ja[i].ToString());
                                }
                                entry.listString = list;
                            }
                        }
                    }
                    else
                    {
                        if (jpItem.ToString().Contains("\": \""))
                        {
                            entry.type = JsonResponseEntry.ResponseEntryType.STRING;
                            entry.stringValue = value;
                        }
                        else
                        {
                            //Int类型
                            try
                            {
                                entry.type = JsonResponseEntry.ResponseEntryType.INT;
                                entry.intValue = long.Parse(value, System.Globalization.NumberStyles.Integer);
                            }
                            //String类型
                            catch
                            {
                                entry.type = JsonResponseEntry.ResponseEntryType.STRING;
                                entry.stringValue = value;
                            }
                        }
                    }
                    responseEntry.Add(key, entry);
                }
            }

            return responseEntry;
        }
Пример #2
0
 static JProperty jsonResponseEntryToJson(JsonResponseEntry responseEntry)
 {
     JArray array = new JArray();
     switch (responseEntry.type)
     {
         case JsonResponseEntry.ResponseEntryType.DICT:
             JObject jObject = new JObject();
             return new JProperty(responseEntry.key, assembleJson(responseEntry.subEntry));
         case JsonResponseEntry.ResponseEntryType.DICT_NULL:
             return new JProperty(responseEntry.key, new JObject());
         case JsonResponseEntry.ResponseEntryType.LIST_ENTRY:
             foreach (Dictionary<String, JsonResponseEntry> subListEntries in responseEntry.listEntry)
             {
                 array.Add(assembleJson(subListEntries));
             }
             return new JProperty(responseEntry.key, array);
         case JsonResponseEntry.ResponseEntryType.LIST_NULL:
             return new JProperty(responseEntry.key, new JArray());
         case JsonResponseEntry.ResponseEntryType.LIST_STRING:
             foreach (String str in responseEntry.listString)
             {
                 array.Add(new JValue(StringToUnicode(str)));
             }
             return new JProperty(responseEntry.key, array);
         case JsonResponseEntry.ResponseEntryType.STRING:
             return new JProperty(responseEntry.key, new JValue(StringToUnicode(responseEntry.stringValue)));
         case JsonResponseEntry.ResponseEntryType.INT:
             return new JProperty(responseEntry.key, new JValue(responseEntry.intValue));
         case JsonResponseEntry.ResponseEntryType.NULL:
             return new JProperty(responseEntry.key, new JValue("").Value = null);
         default:
             return null;
     }
 }
Пример #3
0
        static JsonXMLItemMatcher matchJsonEntryWithXMLItem(JsonResponseEntry jsonEntry, XMLResponseItem xmlItem)
        {
            String itemKey = xmlItem.ItemName;
            JsonXMLItemMatcher itemMatcher = new JsonXMLItemMatcher();
            switch (jsonEntry.type)
            {
                case JsonResponseEntry.ResponseEntryType.DICT:
                    if (xmlItem.Type == ResponseItemType.DICT)
                    {
                        JsonXMLMatcher matcher = matchJsonWithXML(jsonEntry.subEntry, xmlItem.SubItems, false);
                        switch (matcher.matchResult)
                        {
                            case JsonXMLMatcher.MatchResult.LIST_OR_DICT_NULL:
                                itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL;
                                itemMatcher.strResult = itemKey + "->dict null\n    " + matcher.ToString().Replace("\n", "\n    ");
                                break;
                            case JsonXMLMatcher.MatchResult.MATCH:
                                itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_MATCHED;
                                break;
                            case JsonXMLMatcher.MatchResult.MATCH_PARTLY:
                                itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_OR_DICT_PARTLY_MATCHED;
                                itemMatcher.strResult = itemKey + "-> dict partly match\n    " + matcher.ToString().Replace("\n", "\n    ");
                                break;
                            case JsonXMLMatcher.MatchResult.NOT_MATCH:
                                itemMatcher.strResult = itemKey + "-> Not match\n    " + matcher.ToString().Replace("\n", "\n    ");
                                itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                                break;
                        }
                    }
                    else
                    {
                        itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                            + XMLResponseItemTypeToString(xmlItem.Type)
                            + " but in json is DICT\n";
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                    }
                    break;
                case JsonResponseEntry.ResponseEntryType.DICT_NULL:
                    if (xmlItem.Type == ResponseItemType.DICT)
                    {
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL;
                        itemMatcher.strResult = itemKey + "-> Dict null\n";
                    }
                    else
                    {
                        itemMatcher.strResult = itemKey + "-> Type not match in xml is"
                            + XMLResponseItemTypeToString(xmlItem.Type)
                            + "but in json is DICT_NULL\n";
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                    }
                    break;
                case JsonResponseEntry.ResponseEntryType.LIST_ENTRY:
                    if (xmlItem.Type == ResponseItemType.LIST)
                    {
                        JsonXMLMatcher matcher = matchJsonWithXML(jsonEntry.listEntry[0], xmlItem.SubItems, false);
                        switch (matcher.matchResult)
                        {
                            case JsonXMLMatcher.MatchResult.LIST_OR_DICT_NULL:
                                itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_NULL;
                                itemMatcher.strResult = itemKey + "->list null\n" + matcher.ToString().Replace("\n", "\n    ");
                                break;
                            case JsonXMLMatcher.MatchResult.MATCH:
                                itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.DICT_MATCHED;
                                break;
                            case JsonXMLMatcher.MatchResult.MATCH_PARTLY:
                                itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_OR_DICT_PARTLY_MATCHED;
                                itemMatcher.strResult = itemKey + "-> List partly match\n    " + matcher.ToString().Replace("\n", "\n    ");
                                break;
                            case JsonXMLMatcher.MatchResult.NOT_MATCH:
                                itemMatcher.strResult = itemKey + "-> Not match\n    " + matcher.ToString().Replace("\n", "\n    ");
                                itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                                break;
                        }
                    }
                    else
                    {
                        itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                            + XMLResponseItemTypeToString(xmlItem.Type)
                            + " but in json is LIST_ENTRY\n";
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                    }
                    break;
                case JsonResponseEntry.ResponseEntryType.LIST_STRING:
                    if (xmlItem.Type == ResponseItemType.LIST)
                    {
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_STRING_MATCHED;
                    }
                    else
                    {
                        itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                            + XMLResponseItemTypeToString(xmlItem.Type)
                            + " but in json is LIST_STRING\n";
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                    }
                    break;
                case JsonResponseEntry.ResponseEntryType.LIST_NULL:
                    if (xmlItem.Type == ResponseItemType.LIST)
                    {
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.LIST_NULL;
                        itemMatcher.strResult = itemKey + "-> List null\n";
                    }
                    else
                    {
                        itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                            + XMLResponseItemTypeToString(xmlItem.Type)
                            + " but in json is LIST_NULL\n";
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                    }
                    break;
                case JsonResponseEntry.ResponseEntryType.STRING:
                    if (xmlItem.Type == ResponseItemType.STRING)
                    {
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.STRING_MATCHED;
                    }
                    else
                    {
                        itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                            + XMLResponseItemTypeToString(xmlItem.Type)
                            + " but in json is STRING\n";
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                    }
                    break;
                case JsonResponseEntry.ResponseEntryType.INT:
                    if (xmlItem.Type == ResponseItemType.INT)
                    {
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.INT_MATCHED;
                    }
                    else
                    {
                        itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                            + XMLResponseItemTypeToString(xmlItem.Type)
                            + " but in json is INT\n";
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                    }
                    break;
                case JsonResponseEntry.ResponseEntryType.NULL:
                    if (xmlItem.Type == ResponseItemType.INT || xmlItem.Type == ResponseItemType.STRING)
                    {
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.NULL_MATCHED;
                    }
                    else
                    {
                        itemMatcher.strResult = itemKey + "-> Type not match in xml is "
                            + XMLResponseItemTypeToString(xmlItem.Type)
                            + " but in json is NULL\n";
                        itemMatcher.itemMatchResult = JsonXMLItemMatcher.ItemMatchResult.TYPE_NOT_MATCH;
                    }
                    break;
                default:
                    break;
            }

            if (itemMatcher.itemMatchResult > xmlItem.matchResult)
            {
                xmlItem.matchResult = itemMatcher.itemMatchResult;
            }
            return itemMatcher;
        }