private static JsonRoot ParseJArrayIntoDataContract(JArray root, string rootTypeName) { if (root.Count == 0) { return(new JsonRoot(null, 1)); } JsonRoot first = ParseJsonIntoDataContract(root[0], rootTypeName); for (int i = 1; i < root.Count; i++) { JsonRoot next = ParseJsonIntoDataContract(root[i], rootTypeName); JsonRoot mergedType; if (first.CanMerge(next, out mergedType)) { first = mergedType; } else { throw new ArgumentException(string.Format("Cannot merge array elements {0} ({1}) and {2} ({3})", 0, root[0], i, root[i])); } } if (first.IsUserDefinedType) { return(new JsonRoot(first.UserDefinedTypeName, first.ArrayRank + 1, first.Members)); } else { return(new JsonRoot(first.ElementType, first.ArrayRank + 1)); } }
private bool CanMergeIntoUserDefinedType(JsonRoot other, out JsonRoot mergedType) { bool sameAsThis = true; mergedType = null; Dictionary <string, JsonRoot> members = new Dictionary <string, JsonRoot>(); foreach (var memberName in this.Members.Keys.Union(other.Members.Keys)) { if (this.Members.ContainsKey(memberName) && other.Members.ContainsKey(memberName)) { JsonRoot member1 = this.Members[memberName]; JsonRoot member2 = other.Members[memberName]; JsonRoot merged; if (!member1.CanMerge(member2, out merged)) { return(false); } else { if (merged != member1) { sameAsThis = false; } members.Add(memberName, merged); } } else if (this.Members.ContainsKey(memberName)) { members.Add(memberName, this.Members[memberName]); } else { sameAsThis = false; members.Add(memberName, other.Members[memberName]); } } if (sameAsThis) { mergedType = this; } else { mergedType = new JsonRoot(this.UserDefinedTypeName, this.ArrayRank, members); } return(true); }