IDictionary <string, object> PreprocessDictionary(IDictionary values, DictionaryContract contract) { var result = new Dictionary <string, object>(StringComparer.Ordinal); // Manual use of IDictionaryEnumerator instead of foreach to avoid DictionaryEntry box allocations. IDictionaryEnumerator e = values.GetEnumerator(); try { while (e.MoveNext()) { DictionaryEntry entry = e.Entry; string propertyName = GetPropertyName(entry.Key, contract.KeyContract, out bool escape); propertyName = (contract.DictionaryKeyResolver != null) ? contract.DictionaryKeyResolver(propertyName) : propertyName; result[propertyName] = entry.Value; } } finally { (e as IDisposable)?.Dispose(); } return(result); }
void SerializeDictionary(BitStreamWriter writer, IDictionary values, DictionaryContract contract, DsdlType derivedDsdlType, bool tailArrayOptimization) { object underlyingDictionary = values is IWrappedDictionary wrappedDictionary ? wrappedDictionary.UnderlyingDictionary : values; _serializeStack.Push(underlyingDictionary); if (contract.ItemContract == null) { contract.ItemContract = _serializer.ContractResolver.ResolveContract(contract.DictionaryValueType ?? typeof(object)); } if (contract.KeyContract == null) { contract.KeyContract = _serializer.ContractResolver.ResolveContract(contract.DictionaryKeyType ?? typeof(object)); } var dictionaryNormalized = PreprocessDictionary(values, contract); SerializeObjectCore( writer, name => ResolveDictionaryProperty(dictionaryNormalized, contract, name), contract, derivedDsdlType, tailArrayOptimization); _serializeStack.Pop(); }
private HeadwordListContract ConvertHeadwordSearchToContract(IList <HeadwordSearchResult> databaseResult) { var dictionaryList = new Dictionary <string, DictionaryContract>(); var headwordList = new List <HeadwordContract>(); var headwordContract = new HeadwordContract(); foreach (var headword in databaseResult) { DictionaryContract dictionaryContract; if (!dictionaryList.TryGetValue(headword.BookGuid, out dictionaryContract)) { dictionaryContract = new DictionaryContract { BookAcronym = headword.BookAcronym ?? headword.SourceAbbreviation, BookTitle = headword.BookTitle, BookXmlId = headword.BookGuid, BookVersionXmlId = headword.BookVersionId }; dictionaryList.Add(dictionaryContract.BookXmlId, dictionaryContract); } var bookInfoContract = new HeadwordBookInfoContract { BookXmlId = headword.BookGuid, EntryXmlId = headword.XmlEntryId, Image = headword.Image }; if (headword.Headword == headwordContract.Headword) { headwordContract.Dictionaries.Add(bookInfoContract); } else { headwordContract = new HeadwordContract { Dictionaries = new List <HeadwordBookInfoContract> { bookInfoContract }, Headword = headword.Headword }; headwordList.Add(headwordContract); } } return(new HeadwordListContract { BookList = dictionaryList, HeadwordList = headwordList }); }
private IDictionary CreateNewDictionary(BitStreamReader reader, DictionaryContract contract, out bool createdFromNonDefaultCreator) { if (contract.OverrideCreator != null) { if (contract.HasParameterizedCreator) { createdFromNonDefaultCreator = true; return(contract.CreateTemporaryDictionary()); } else { createdFromNonDefaultCreator = false; return((IDictionary)contract.OverrideCreator()); } } else if (contract.IsReadOnlyOrFixedSize) { createdFromNonDefaultCreator = true; return(contract.CreateTemporaryDictionary()); } else if (contract.DefaultCreator != null && !contract.DefaultCreatorNonPublic) { object dictionary = contract.DefaultCreator(); if (contract.ShouldCreateWrapper) { dictionary = contract.CreateWrapper(dictionary); } createdFromNonDefaultCreator = false; return((IDictionary)dictionary); } else if (contract.HasParameterizedCreatorInternal) { createdFromNonDefaultCreator = true; return(contract.CreateTemporaryDictionary()); } else { if (!contract.IsInstantiable) { throw new SerializationException("Could not create an instance of type {0}. Type is an interface or abstract class and cannot be instantiated.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); } throw new SerializationException("Unable to find a default constructor to use for type {0}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType)); } }
object PopulateDictionary( IDictionary dictionary, BitStreamReader reader, DictionaryContract contract, DsdlProperty containerProperty, CompositeDsdlTypeBase scheme, bool tailArrayOptimization) { object underlyingDictionary = dictionary is IWrappedDictionary wrappedDictionary ? wrappedDictionary.UnderlyingDictionary : dictionary; if (contract.KeyContract == null) { contract.KeyContract = GetContractSafe(contract.DictionaryKeyType); } if (contract.ItemContract == null) { contract.ItemContract = GetContractSafe(contract.DictionaryValueType); } foreach (var(field, tao) in EnumerateSchemeFields(reader, scheme, tailArrayOptimization)) { if (field.Type is VoidDsdlType t) { ReadAlignment(reader, t); continue; } var keyValue = EnsureType(reader, field.Name, CultureInfo.InvariantCulture, contract.KeyContract, contract.DictionaryKeyType); var itemValue = CreateValueInternal(reader, contract.ItemContract, null, contract, containerProperty, null, field.Type, null, tao); dictionary[keyValue] = itemValue; } return(underlyingDictionary); }
ResolvedProperty?ResolveDictionaryProperty(IDictionary <string, object> dictionary, DictionaryContract contract, string name) { if (!dictionary.TryGetValue(name, out var value)) { return(null); } var valueContract = contract.FinalItemContract ?? (value == null ? null : _serializer.ContractResolver.ResolveContract(value.GetType())); if (!CheckForCircularReference(value, null, valueContract)) { return(null); } return(new ResolvedProperty { Member = null, MemberContact = valueContract, MemberValue = value }); }