public void matchWithJsonEntry(JsonEntry jsonEntry, bool isInList) { MatchStructure orgMatchStructure = this.matchStruct; this.matchStruct.responseJsonType = jsonEntry.type; switch (jsonEntry.type) { case JsonEntryType.BOOL: if (this.type == ResponseParamType.BOOL) { this.matchStruct.matchResult = MatchResult.MATCHED; mergeListMatchResult(orgMatchStructure, isInList); } break; case JsonEntryType.DICT: if (this.type == ResponseParamType.DICT) { matchDict(jsonEntry); mergeListMatchResult(orgMatchStructure, isInList); } break; case JsonEntryType.DICT_NULL: if (this.type == ResponseParamType.DICT_NULL) { this.matchStruct.matchResult = MatchResult.MATCHED; mergeListMatchResult(orgMatchStructure, isInList); } if (this.type == ResponseParamType.DICT) { this.matchStruct.matchResult = MatchResult.NULL; mergeListMatchResult(orgMatchStructure, isInList); } break; case JsonEntryType.INT: if (this.type == ResponseParamType.INT) { this.matchStruct.matchResult = MatchResult.MATCHED; mergeListMatchResult(orgMatchStructure, isInList); } break; case JsonEntryType.LIST: if (this.type == ResponseParamType.LIST || this.type == ResponseParamType.LIST_STRING) { matchList(jsonEntry); mergeListMatchResult(orgMatchStructure, isInList); } break; case JsonEntryType.LIST_NULL: if (this.type == ResponseParamType.LIST) { this.matchStruct.matchResult = MatchResult.MATCHED; mergeListMatchResult(orgMatchStructure, isInList); } break; case JsonEntryType.NULL: this.matchStruct.matchResult = MatchResult.MATCHED; mergeListMatchResult(orgMatchStructure, isInList); break; case JsonEntryType.STRING: if (this.type == ResponseParamType.STRING) { this.matchStruct.matchResult = MatchResult.MATCHED; mergeListMatchResult(orgMatchStructure, isInList); } break; default: break; } if (this.matchStruct.matchResult == MatchResult.DEFAULT) { this.matchStruct.matchResult = MatchResult.TYPE_NOT_MATCH; mergeListMatchResult(orgMatchStructure, isInList); this.matchStruct.isSubItemNotMatch = false; } }
private void matchDict(JsonEntry jsonEntry) { matchDict(jsonEntry, false); }
private void matchDict(JsonEntry jsonEntry, bool isInList) { HashSet<String> subItemsKeys = new HashSet<string>(); foreach (String key in this.subItems.Keys) { subItemsKeys.Add(key); } //遍历每一个json中的数据格式,和XML对比,找到差异和是否有json中多余的部分, //匹配成功则在XML对象中移除相关Key,剩余的便是XML有但是json没有的部分了 foreach (String key in jsonEntry.entryDict.Keys) { if (this.subItems.ContainsKey(key)) { this.subItems[key].matchWithJsonEntry(jsonEntry.entryDict[key], isInList); } else { this.matchStruct.jsonEntryNotFound.Add(key); } subItemsKeys.Remove(key); } foreach (String key in subItemsKeys) { this.matchStruct.xmlItemNotFound.Add(key); } //json有没有被覆盖的数据(通常有问题,需要更新XML) if (this.matchStruct.jsonEntryNotFound.Count > 0) { this.matchStruct.matchResult = MatchResult.TYPE_NOT_MATCH; } //XML有没有被覆盖到的数据(常见的,定义的规则中是有字段不是必须返回的) else if (this.matchStruct.xmlItemNotFound.Count > 0) { this.matchStruct.matchResult = MatchResult.PARTLY_MATCHED; } //针对每个匹配的item再做下对比 foreach (String key in this.subItems.Keys) { if (this.matchStruct.matchResult < this.subItems[key].matchStruct.matchResult) { this.matchStruct.matchResult = this.subItems[key].matchStruct.matchResult; } } subItemsKeys = null; }
private void matchList(JsonEntry jsonEntry) { if (jsonEntry.entryList[0].type == JsonEntryType.STRING || jsonEntry.entryList[0].type == JsonEntryType.INT) { if (this.type == ResponseParamType.LIST_STRING) { this.matchStruct.matchResult = MatchResult.MATCHED; } } else if (jsonEntry.entryList[0].type == JsonEntryType.DICT) { if (this.type == ResponseParamType.LIST) { foreach (JsonEntry jsonListSubEntry in jsonEntry.entryList) { //foreach (String key in jsonListSubEntry.entryDict.Keys) //{ // this.subItems[key].matchDict(jsonListSubEntry.entryDict[key], true); //} this.matchDict(jsonListSubEntry, true); } } } }
public void matchWithJsonEntry(JsonEntry jsonEntry) { matchWithJsonEntry(jsonEntry, false); }
public static JsonEntry analyzeFromJsonToken(JToken jt) { JsonEntry result = new JsonEntry(); //try //{ switch (jt.Type) { case JTokenType.Array: if ((jt as JArray).Count > 0) { result.type = JsonEntryType.LIST; foreach (JToken jtItem in (jt as JArray)) { result.entryList.Add(analyzeFromJsonToken(jtItem)); } } else { result.type = JsonEntryType.LIST_NULL; } break; case JTokenType.Object: if ((jt as JObject).HasValues) { result.type = JsonEntryType.DICT; foreach (JProperty jpItem in (jt as JObject).Children()) { String key = jpItem.Name; JToken jtValue = jpItem.Value; result.entryDict.Add(key, analyzeFromJsonToken(jtValue)); } } else { result.type = JsonEntryType.DICT_NULL; } break; case JTokenType.Null: result.type = JsonEntryType.NULL; break; case JTokenType.Integer: result.type = JsonEntryType.INT; result.intValue = Convert.ToInt64((jt as JValue).Value); break; case JTokenType.Boolean: result.type = JsonEntryType.BOOL; result.boolValue = Convert.ToBoolean((jt as JValue).Value); break; default: result.type = JsonEntryType.STRING; result.stringValue = (jt as JValue).Value.ToString(); break; } //} //catch (Exception e) //{ //} return result; }