private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract) { lock (createDataContractLock) { JsonDataContract contract = dataContractCache[id]; if (contract == null) { Type type = traditionalDataContract.GetType(); if (type == typeof(ObjectDataContract)) { contract = new JsonObjectDataContract(traditionalDataContract); } else if (type == typeof(StringDataContract)) { contract = new JsonStringDataContract((StringDataContract)traditionalDataContract); } else if (type == typeof(UriDataContract)) { contract = new JsonUriDataContract((UriDataContract)traditionalDataContract); } else if (type == typeof(QNameDataContract)) { contract = new JsonQNameDataContract((QNameDataContract)traditionalDataContract); } else if (type == typeof(ByteArrayDataContract)) { contract = new JsonByteArrayDataContract((ByteArrayDataContract)traditionalDataContract); } else if (traditionalDataContract.IsPrimitive || (traditionalDataContract.UnderlyingType == Globals.TypeOfXmlQualifiedName)) { contract = new JsonDataContract(traditionalDataContract); } else if (type == typeof(ClassDataContract)) { contract = new JsonClassDataContract((ClassDataContract)traditionalDataContract); } else if (type == typeof(EnumDataContract)) { contract = new JsonEnumDataContract((EnumDataContract)traditionalDataContract); } else if ((type == typeof(GenericParameterDataContract)) || (type == typeof(SpecialTypeDataContract))) { contract = new JsonDataContract(traditionalDataContract); } else if (type == typeof(CollectionDataContract)) { contract = new JsonCollectionDataContract((CollectionDataContract)traditionalDataContract); } else { if (type != typeof(XmlDataContract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("traditionalDataContract", System.Runtime.Serialization.SR.GetString("JsonTypeNotSupportedByDataContractJsonSerializer", new object[] { traditionalDataContract.UnderlyingType })); } contract = new JsonXmlDataContract((XmlDataContract)traditionalDataContract); } } return(contract); } }
///<summary> ///convert string response to html format ///</summary> public String WrapToHTML(String data, JsonDataContract helper, int width, int height) { string strHTML = "<html><head>" + "<meta name='viewport' content='" + "width=" + width.ToString() + ", height=" + height.ToString() + ", initial-scale=1.0, maximum-scale=1.0, user-scalable=no' />" + "<title>Advertisement</title> " + "</head>" + "<body style=\"margin:0; padding:0; overflow:hidden; background-color:black;\">" + "<table style=\"width: " + (width - 2).ToString() + "px; height: " + (height - 2).ToString() + "px; vertical-align:central; background-color: black;\">" + "<tr>" + "<td style=\"text-align:center;\">" + "<style type=\"text/css\">a img {border:none;}</style>" + data + "</td>" + "</tr>" + "</table>" + "</body> " + "</html> "; return strHTML; }
protected override void WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle declaredTypeHandle) { JsonDataContract jsonDataContract = JsonDataContract.GetJsonDataContract(dataContract); if (_emitXsiType == EmitTypeInformation.Always && !_perCallXsiTypeAlreadyEmitted && RequiresJsonTypeInfo(dataContract)) { WriteTypeInfo(xmlWriter, jsonDataContract.TypeName !); } _perCallXsiTypeAlreadyEmitted = false; DataContractJsonSerializerImpl.WriteJsonValue(jsonDataContract, xmlWriter, obj, this, declaredTypeHandle); }
protected override void WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle declaredTypeHandle) { JsonDataContract jsonDataContract = JsonDataContract.GetJsonDataContract(dataContract); if ((this.alwaysEmitXsiType && !this.perCallXsiTypeAlreadyEmitted) && RequiresJsonTypeInfo(dataContract)) { this.WriteTypeInfo(xmlWriter, jsonDataContract.TypeName); } this.perCallXsiTypeAlreadyEmitted = false; DataContractJsonSerializer.WriteJsonValue(jsonDataContract, xmlWriter, obj, this, declaredTypeHandle); }
public override object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) { if (context != null) { return(JsonDataContract.HandleReadValue(jsonReader.ReadElementContentAsUri(), context)); } if (!JsonDataContract.TryReadNullAtTopLevel(jsonReader)) { return(jsonReader.ReadElementContentAsUri()); } return(null); }
public static JsonDataContract GetJsonDataContract(DataContract traditionalDataContract) { int id = GetId(traditionalDataContract.UnderlyingType.TypeHandle); JsonDataContract contract = dataContractCache[id]; if (contract == null) { contract = CreateJsonDataContract(id, traditionalDataContract); dataContractCache[id] = contract; } return(contract); }
public static JsonDataContract GetJsonDataContract(DataContract traditionalDataContract) { int id = JsonDataContractCriticalHelper.GetId(traditionalDataContract.UnderlyingType.TypeHandle); JsonDataContract dataContract = s_dataContractCache[id]; if (dataContract == null) { dataContract = CreateJsonDataContract(id, traditionalDataContract); s_dataContractCache[id] = dataContract; } return(dataContract); }
internal override void InternalWriteObjectContent(XmlWriterDelegator writer, object graph) { if (MaxItemsInObjectGraph == 0) { throw XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ExceededMaxItemsQuota, MaxItemsInObjectGraph)); } DataContract contract = RootContract; Type declaredType = contract.UnderlyingType; Type graphType = (graph == null) ? declaredType : graph.GetType(); //if (dataContractSurrogate != null) //{ // graph = DataContractSerializer.SurrogateToDataContractType(dataContractSurrogate, graph, declaredType, ref graphType); //} if (graph == null) { WriteJsonNull(writer); } else { if (declaredType == graphType) { if (contract.CanContainReferences) { XmlObjectSerializerWriteContextComplexJson context = XmlObjectSerializerWriteContextComplexJson.CreateContext(this, contract); context.OnHandleReference(writer, graph, true); // canContainReferences context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle); } else { DataContractJsonSerializerImpl.WriteJsonValue(JsonDataContract.GetJsonDataContract(contract), writer, graph, null, declaredType.TypeHandle); // XmlObjectSerializerWriteContextComplexJson } } else { XmlObjectSerializerWriteContextComplexJson context = XmlObjectSerializerWriteContextComplexJson.CreateContext(this, RootContract); contract = DataContractJsonSerializerImpl.GetDataContract(contract, declaredType, graphType); if (contract.CanContainReferences) { context.OnHandleReference(writer, graph, true); // canContainCyclicReference context.SerializeWithXsiTypeAtTopLevel(contract, writer, graph, declaredType.TypeHandle, graphType); } else { context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle); } } } }
protected override void WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle declaredTypeHandle) { #if NET_NATIVE || MERGE_DCJS JsonDataContract jsonDataContract = JsonDataContract.GetJsonDataContract(dataContract); if (_emitXsiType == EmitTypeInformation.Always && !_perCallXsiTypeAlreadyEmitted && RequiresJsonTypeInfo(dataContract)) { WriteTypeInfo(xmlWriter, jsonDataContract.TypeName); } _perCallXsiTypeAlreadyEmitted = false; DataContractJsonSerializerImpl.WriteJsonValue(jsonDataContract, xmlWriter, obj, this, declaredTypeHandle); #else _jsonSerializer.WriteObjectInternal(obj, dataContract, this, WriteTypeInfo(null, dataContract, DataContract.GetDataContract(declaredTypeHandle, obj.GetType())), declaredTypeHandle); #endif }
internal override void InternalWriteObjectContent(XmlWriterDelegator writer, object graph) { if (this.MaxItemsInObjectGraph == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString("ExceededMaxItemsQuota", new object[] { this.MaxItemsInObjectGraph }))); } DataContract rootContract = this.RootContract; Type underlyingType = rootContract.UnderlyingType; Type objType = (graph == null) ? underlyingType : graph.GetType(); if (this.dataContractSurrogate != null) { graph = DataContractSerializer.SurrogateToDataContractType(this.dataContractSurrogate, graph, underlyingType, ref objType); } if (graph == null) { WriteJsonNull(writer); } else if (underlyingType == objType) { if (rootContract.CanContainReferences) { XmlObjectSerializerWriteContextComplexJson json = XmlObjectSerializerWriteContextComplexJson.CreateContext(this, rootContract); json.OnHandleReference(writer, graph, true); json.SerializeWithoutXsiType(rootContract, writer, graph, underlyingType.TypeHandle); } else { WriteJsonValue(JsonDataContract.GetJsonDataContract(rootContract), writer, graph, null, underlyingType.TypeHandle); } } else { XmlObjectSerializerWriteContextComplexJson json2 = XmlObjectSerializerWriteContextComplexJson.CreateContext(this, this.RootContract); rootContract = GetDataContract(rootContract, underlyingType, objType); if (rootContract.CanContainReferences) { json2.OnHandleReference(writer, graph, true); json2.SerializeWithXsiTypeAtTopLevel(rootContract, writer, graph, underlyingType.TypeHandle, objType); } else { json2.SerializeWithoutXsiType(rootContract, writer, graph, underlyingType.TypeHandle); } } }
public void ReflectionWriteCollection(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContextComplexJson context, CollectionDataContract collectionContract) { JsonWriterDelegator jsonWriter = xmlWriter as JsonWriterDelegator; if (jsonWriter == null) { throw new ArgumentException(nameof(xmlWriter)); } XmlDictionaryString itemName = context.CollectionItemName; if (collectionContract.Kind == CollectionKind.Array) { context.IncrementArrayCount(jsonWriter, (Array)obj); Type itemType = collectionContract.ItemType; if (!ReflectionTryWritePrimitiveArray(jsonWriter, obj, collectionContract.UnderlyingType, itemType, itemName)) { ReflectionWriteArrayAttribute(jsonWriter); Array array = (Array)obj; PrimitiveDataContract primitiveContract = PrimitiveDataContract.GetPrimitiveDataContract(itemType); for (int i = 0; i < array.Length; ++i) { _reflectionClassWriter.ReflectionWriteStartElement(jsonWriter, itemName); _reflectionClassWriter.ReflectionWriteValue(jsonWriter, context, itemType, array.GetValue(i), false, primitiveContract); _reflectionClassWriter.ReflectionWriteEndElement(jsonWriter); } } } else { collectionContract.IncrementCollectionCount(jsonWriter, obj, context); IEnumerator enumerator = collectionContract.GetEnumeratorForCollection(obj); bool canWriteSimpleDictionary = collectionContract.Kind == CollectionKind.GenericDictionary || collectionContract.Kind == CollectionKind.Dictionary; bool useSimpleDictionaryFormat = context.UseSimpleDictionaryFormat; if (canWriteSimpleDictionary && useSimpleDictionaryFormat) { ReflectionWriteObjectAttribute(jsonWriter); Type[] itemTypeGenericArguments = collectionContract.ItemType.GetGenericArguments(); Type dictionaryValueType = itemTypeGenericArguments.Length == 2 ? itemTypeGenericArguments[1] : null; while (enumerator.MoveNext()) { object current = enumerator.Current; object key = ((IKeyValue)current).Key; object value = ((IKeyValue)current).Value; _reflectionClassWriter.ReflectionWriteStartElement(jsonWriter, key.ToString()); _reflectionClassWriter.ReflectionWriteValue(jsonWriter, context, dictionaryValueType ?? value.GetType(), value, false, primitiveContractForParamType: null); _reflectionClassWriter.ReflectionWriteEndElement(jsonWriter); } } else { ReflectionWriteArrayAttribute(jsonWriter); PrimitiveDataContract primitiveContractForType = PrimitiveDataContract.GetPrimitiveDataContract(collectionContract.UnderlyingType); if (primitiveContractForType != null && primitiveContractForType.UnderlyingType != Globals.TypeOfObject) { while (enumerator.MoveNext()) { object current = enumerator.Current; context.IncrementItemCount(1); primitiveContractForType.WriteXmlElement(jsonWriter, current, context, itemName, null /*namespace*/); } } else { Type elementType = collectionContract.GetCollectionElementType(); bool isDictionary = collectionContract.Kind == CollectionKind.Dictionary || collectionContract.Kind == CollectionKind.GenericDictionary; DataContract itemContract = null; JsonDataContract jsonDataContract = null; if (isDictionary) { itemContract = XmlObjectSerializerWriteContextComplexJson.GetRevisedItemContract(collectionContract.ItemContract); jsonDataContract = JsonDataContract.GetJsonDataContract(itemContract); } while (enumerator.MoveNext()) { object current = enumerator.Current; context.IncrementItemCount(1); _reflectionClassWriter.ReflectionWriteStartElement(jsonWriter, itemName); if (isDictionary) { jsonDataContract.WriteJsonValue(jsonWriter, current, context, collectionContract.ItemType.TypeHandle); } else { _reflectionClassWriter.ReflectionWriteValue(jsonWriter, context, elementType, current, false, primitiveContractForParamType: null); } _reflectionClassWriter.ReflectionWriteEndElement(jsonWriter); } } } } }
internal static void WriteJsonValue(JsonDataContract contract, XmlWriterDelegator writer, object graph, XmlObjectSerializerWriteContextComplexJson context, RuntimeTypeHandle declaredTypeHandle) { contract.WriteJsonValue(writer, graph, context, declaredTypeHandle); }
internal static object ReadJsonValue(DataContract contract, XmlReaderDelegator reader, XmlObjectSerializerReadContextComplexJson context) { return(JsonDataContract.GetJsonDataContract(contract).ReadJsonValue(reader, context)); }
private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract) { lock (s_createDataContractLock) { JsonDataContract dataContract = s_dataContractCache[id]; if (dataContract == null) { Type traditionalDataContractType = traditionalDataContract.GetType(); if (traditionalDataContractType == typeof(ObjectDataContract)) { dataContract = new JsonObjectDataContract(traditionalDataContract); } else if (traditionalDataContractType == typeof(StringDataContract)) { dataContract = new JsonStringDataContract((StringDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(UriDataContract)) { dataContract = new JsonUriDataContract((UriDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(QNameDataContract)) { dataContract = new JsonQNameDataContract((QNameDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(ByteArrayDataContract)) { dataContract = new JsonByteArrayDataContract((ByteArrayDataContract)traditionalDataContract); } else if (traditionalDataContract.IsPrimitive || traditionalDataContract.UnderlyingType == Globals.TypeOfXmlQualifiedName) { dataContract = new JsonDataContract(traditionalDataContract); } else if (traditionalDataContractType == typeof(ClassDataContract)) { dataContract = new JsonClassDataContract((ClassDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(EnumDataContract)) { dataContract = new JsonEnumDataContract((EnumDataContract)traditionalDataContract); } else if ((traditionalDataContractType == typeof(GenericParameterDataContract)) || (traditionalDataContractType == typeof(SpecialTypeDataContract))) { dataContract = new JsonDataContract(traditionalDataContract); } else if (traditionalDataContractType == typeof(CollectionDataContract)) { dataContract = new JsonCollectionDataContract((CollectionDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(XmlDataContract)) { dataContract = new JsonXmlDataContract((XmlDataContract)traditionalDataContract); } else { throw new ArgumentException(SR.Format(SR.JsonTypeNotSupportedByDataContractJsonSerializer, traditionalDataContract.UnderlyingType), "traditionalDataContract"); } } return dataContract; } }
private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract) { lock (s_createDataContractLock) { JsonDataContract dataContract = s_dataContractCache[id]; if (dataContract == null) { Type traditionalDataContractType = traditionalDataContract.GetType(); if (traditionalDataContractType == typeof(ObjectDataContract)) { dataContract = new JsonObjectDataContract(traditionalDataContract); } else if (traditionalDataContractType == typeof(StringDataContract)) { dataContract = new JsonStringDataContract((StringDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(UriDataContract)) { dataContract = new JsonUriDataContract((UriDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(QNameDataContract)) { dataContract = new JsonQNameDataContract((QNameDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(ByteArrayDataContract)) { dataContract = new JsonByteArrayDataContract((ByteArrayDataContract)traditionalDataContract); } else if (traditionalDataContract.IsPrimitive || traditionalDataContract.UnderlyingType == Globals.TypeOfXmlQualifiedName) { dataContract = new JsonDataContract(traditionalDataContract); } else if (traditionalDataContractType == typeof(ClassDataContract)) { dataContract = new JsonClassDataContract((ClassDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(EnumDataContract)) { dataContract = new JsonEnumDataContract((EnumDataContract)traditionalDataContract); } else if ((traditionalDataContractType == typeof(GenericParameterDataContract)) || (traditionalDataContractType == typeof(SpecialTypeDataContract))) { dataContract = new JsonDataContract(traditionalDataContract); } else if (traditionalDataContractType == typeof(CollectionDataContract)) { dataContract = new JsonCollectionDataContract((CollectionDataContract)traditionalDataContract); } else if (traditionalDataContractType == typeof(XmlDataContract)) { dataContract = new JsonXmlDataContract((XmlDataContract)traditionalDataContract); } else { throw new ArgumentException(SR.Format(SR.JsonTypeNotSupportedByDataContractJsonSerializer, traditionalDataContract.UnderlyingType), nameof(traditionalDataContract)); } } return(dataContract); } }
private static JsonDataContract CreateJsonDataContract(int id, DataContract traditionalDataContract) { lock (createDataContractLock) { JsonDataContract contract = dataContractCache[id]; if (contract == null) { Type type = traditionalDataContract.GetType(); if (type == typeof(ObjectDataContract)) { contract = new JsonObjectDataContract(traditionalDataContract); } else if (type == typeof(StringDataContract)) { contract = new JsonStringDataContract((StringDataContract) traditionalDataContract); } else if (type == typeof(UriDataContract)) { contract = new JsonUriDataContract((UriDataContract) traditionalDataContract); } else if (type == typeof(QNameDataContract)) { contract = new JsonQNameDataContract((QNameDataContract) traditionalDataContract); } else if (type == typeof(ByteArrayDataContract)) { contract = new JsonByteArrayDataContract((ByteArrayDataContract) traditionalDataContract); } else if (traditionalDataContract.IsPrimitive || (traditionalDataContract.UnderlyingType == Globals.TypeOfXmlQualifiedName)) { contract = new JsonDataContract(traditionalDataContract); } else if (type == typeof(ClassDataContract)) { contract = new JsonClassDataContract((ClassDataContract) traditionalDataContract); } else if (type == typeof(EnumDataContract)) { contract = new JsonEnumDataContract((EnumDataContract) traditionalDataContract); } else if ((type == typeof(GenericParameterDataContract)) || (type == typeof(SpecialTypeDataContract))) { contract = new JsonDataContract(traditionalDataContract); } else if (type == typeof(CollectionDataContract)) { contract = new JsonCollectionDataContract((CollectionDataContract) traditionalDataContract); } else { if (type != typeof(XmlDataContract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument("traditionalDataContract", System.Runtime.Serialization.SR.GetString("JsonTypeNotSupportedByDataContractJsonSerializer", new object[] { traditionalDataContract.UnderlyingType })); } contract = new JsonXmlDataContract((XmlDataContract) traditionalDataContract); } } return contract; } }
void WriteCollection(CollectionDataContract collectionContract) { XmlDictionaryString itemName = context.CollectionItemName; if (collectionContract.Kind == CollectionKind.Array) { Type itemType = collectionContract.ItemType; int i; // This check does not exist in the original dynamic code, // but there is no other way to check type mismatch. // CollectionSerialization.ArrayContract() shows that it is required. if (objLocal.GetType().GetElementType() != itemType) { throw new InvalidCastException(string.Format("Cannot cast array of {0} to array of {1}", objLocal.GetType().GetElementType(), itemType)); } context.IncrementArrayCount(writer, (Array)objLocal); if (!TryWritePrimitiveArray(collectionContract.UnderlyingType, itemType, () => objLocal, itemName)) { WriteArrayAttribute(); var arr = (Array)objLocal; var idx = new int [1]; for (i = 0; i < arr.Length; i++) { if (!TryWritePrimitive(itemType, null, null, i, itemName, 0)) { WriteStartElement(itemName, 0); idx [0] = i; var mbrVal = arr.GetValue(idx); WriteValue(itemType, mbrVal); WriteEndElement(); } } } } else { // This check does not exist in the original dynamic code, // but there is no other way to check type mismatch. // CollectionSerialization.ArrayContract() shows that it is required. if (!collectionContract.UnderlyingType.IsAssignableFrom(objLocal.GetType())) { throw new InvalidCastException(string.Format("Cannot cast {0} to {1}", objLocal.GetType(), collectionContract.UnderlyingType)); } MethodInfo incrementCollectionCountMethod = null; switch (collectionContract.Kind) { case CollectionKind.Collection: case CollectionKind.List: case CollectionKind.Dictionary: incrementCollectionCountMethod = XmlFormatGeneratorStatics.IncrementCollectionCountMethod; break; case CollectionKind.GenericCollection: case CollectionKind.GenericList: incrementCollectionCountMethod = XmlFormatGeneratorStatics.IncrementCollectionCountGenericMethod.MakeGenericMethod(collectionContract.ItemType); break; case CollectionKind.GenericDictionary: incrementCollectionCountMethod = XmlFormatGeneratorStatics.IncrementCollectionCountGenericMethod.MakeGenericMethod(Globals.TypeOfKeyValuePair.MakeGenericType(collectionContract.ItemType.GetGenericArguments())); break; } if (incrementCollectionCountMethod != null) { incrementCollectionCountMethod.Invoke(context, new object [] { writer, objLocal }); } bool isDictionary = false, isGenericDictionary = false; Type enumeratorType = null; Type [] keyValueTypes = null; if (collectionContract.Kind == CollectionKind.GenericDictionary) { isGenericDictionary = true; keyValueTypes = collectionContract.ItemType.GetGenericArguments(); enumeratorType = Globals.TypeOfGenericDictionaryEnumerator.MakeGenericType(keyValueTypes); } else if (collectionContract.Kind == CollectionKind.Dictionary) { isDictionary = true; keyValueTypes = new Type[] { Globals.TypeOfObject, Globals.TypeOfObject }; enumeratorType = Globals.TypeOfDictionaryEnumerator; } else { enumeratorType = collectionContract.GetEnumeratorMethod.ReturnType; } MethodInfo moveNextMethod = enumeratorType.GetMethod(Globals.MoveNextMethodName, BindingFlags.Instance | BindingFlags.Public, null, Globals.EmptyTypeArray, null); MethodInfo getCurrentMethod = enumeratorType.GetMethod(Globals.GetCurrentMethodName, BindingFlags.Instance | BindingFlags.Public, null, Globals.EmptyTypeArray, null); if (moveNextMethod == null || getCurrentMethod == null) { if (enumeratorType.IsInterface) { if (moveNextMethod == null) { moveNextMethod = JsonFormatGeneratorStatics.MoveNextMethod; } if (getCurrentMethod == null) { getCurrentMethod = JsonFormatGeneratorStatics.GetCurrentMethod; } } else { Type ienumeratorInterface = Globals.TypeOfIEnumerator; CollectionKind kind = collectionContract.Kind; if (kind == CollectionKind.GenericDictionary || kind == CollectionKind.GenericCollection || kind == CollectionKind.GenericEnumerable) { Type[] interfaceTypes = enumeratorType.GetInterfaces(); foreach (Type interfaceType in interfaceTypes) { if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == Globals.TypeOfIEnumeratorGeneric && interfaceType.GetGenericArguments()[0] == collectionContract.ItemType) { ienumeratorInterface = interfaceType; break; } } } if (moveNextMethod == null) { moveNextMethod = CollectionDataContract.GetTargetMethodWithName(Globals.MoveNextMethodName, enumeratorType, ienumeratorInterface); } if (getCurrentMethod == null) { getCurrentMethod = CollectionDataContract.GetTargetMethodWithName(Globals.GetCurrentMethodName, enumeratorType, ienumeratorInterface); } } } Type elementType = getCurrentMethod.ReturnType; object currentValue = null; // of elementType var enumerator = (IEnumerator)collectionContract.GetEnumeratorMethod.Invoke(objLocal, new object [0]); if (isDictionary) { ConstructorInfo dictEnumCtor = enumeratorType.GetConstructor(Globals.ScanAllMembers, null, new Type[] { Globals.TypeOfIDictionaryEnumerator }, null); enumerator = (IEnumerator)dictEnumCtor.Invoke(new object [] { enumerator }); } else if (isGenericDictionary) { Type ctorParam = Globals.TypeOfIEnumeratorGeneric.MakeGenericType(Globals.TypeOfKeyValuePair.MakeGenericType(keyValueTypes)); ConstructorInfo dictEnumCtor = enumeratorType.GetConstructor(Globals.ScanAllMembers, null, new Type[] { ctorParam }, null); enumerator = (IEnumerator)Activator.CreateInstance(enumeratorType, new object [] { enumerator }); } bool canWriteSimpleDictionary = isDictionary || isGenericDictionary; bool writeSimpleDictionary = canWriteSimpleDictionary && context.UseSimpleDictionaryFormat; PropertyInfo genericDictionaryKeyProperty = null, genericDictionaryValueProperty = null; if (canWriteSimpleDictionary) { Type genericDictionaryKeyValueType = Globals.TypeOfKeyValue.MakeGenericType(keyValueTypes); genericDictionaryKeyProperty = genericDictionaryKeyValueType.GetProperty(JsonGlobals.KeyString); genericDictionaryValueProperty = genericDictionaryKeyValueType.GetProperty(JsonGlobals.ValueString); } if (writeSimpleDictionary) { WriteObjectAttribute(); object key, value; var empty_args = new object [0]; while ((bool)moveNextMethod.Invoke(enumerator, empty_args)) { currentValue = getCurrentMethod.Invoke(enumerator, empty_args); key = CodeInterpreter.GetMember(genericDictionaryKeyProperty, currentValue); value = CodeInterpreter.GetMember(genericDictionaryValueProperty, currentValue); WriteStartElement(key, 0 /*nameIndex*/); WriteValue(genericDictionaryValueProperty.PropertyType, value); WriteEndElement(); } } else { WriteArrayAttribute(); var emptyArray = new object [0]; while (enumerator != null && enumerator.MoveNext()) { currentValue = getCurrentMethod.Invoke(enumerator, emptyArray); if (incrementCollectionCountMethod == null) { XmlFormatGeneratorStatics.IncrementItemCountMethod.Invoke(context, new object [] { 1 }); } if (!TryWritePrimitive(elementType, () => currentValue, null, null, itemName, 0)) { WriteStartElement(itemName, 0); if (isGenericDictionary || isDictionary) { var jc = JsonDataContract.GetJsonDataContract(XmlObjectSerializerWriteContextComplexJson.GetRevisedItemContract( collectionDataContract.ItemContract)); // FIXME: this TypeHandle might be wrong; there is no easy way to get Type for currentValue though. DataContractJsonSerializer.WriteJsonValue(jc, writer, currentValue, context, currentValue.GetType().TypeHandle); } else { WriteValue(elementType, currentValue); } WriteEndElement(); } } } } }