public static IEnumerable ConvertGenericListToArray(DataContractJsonSerializer serializer, IEnumerable value, CollectionDataContract dataContract, XmlObjectSerializerWriteContextComplexJson context, bool writeServerType)
        {
            Type listArgumentType = dataContract.ItemType;
            if (listArgumentType.GetTypeInfo().IsGenericType)
            {
                listArgumentType = listArgumentType.GetGenericArguments()[0];
            }
            List<object> serializedList = new List<object>();
            MethodInfo getEnumeratorMethod = dataContract.GetEnumeratorMethod;

            IEnumerator enumerator = (getEnumeratorMethod == null) ? value.GetEnumerator() : (IEnumerator)getEnumeratorMethod.Invoke(value, Array.Empty<Type>());
            while (enumerator.MoveNext())
            {
                if (enumerator.Current == null || enumerator.Current.GetType().GetTypeInfo().IsPrimitive)
                {
                    serializedList.Add(enumerator.Current);
                }
                else
                {
                    Type currentItemType = enumerator.Current.GetType();
                    DataContract currentItemDataContract = DataContract.GetDataContract(currentItemType);
                    bool emitTypeInformation = EmitTypeInformation(dataContract.ItemContract, currentItemType);
                    if (writeServerType || emitTypeInformation)
                    {
                        context.CheckIfTypeNeedsVerifcation(dataContract.ItemContract, currentItemDataContract);
                    }
                    context.PushKnownTypes(dataContract);
                    serializedList.Add(serializer.ConvertDataContractToObject(enumerator.Current, currentItemDataContract, context, (writeServerType || emitTypeInformation), dataContract.ItemType.TypeHandle));
                    context.PopKnownTypes(dataContract);
                }
            }
            return serializedList;
        }
Beispiel #2
0
        public static IEnumerable ConvertGenericListToArray(DataContractJsonSerializer serializer, IEnumerable value, CollectionDataContract dataContract, XmlObjectSerializerWriteContextComplexJson context, bool writeServerType)
        {
            Type listArgumentType = dataContract.ItemType;

            if (listArgumentType.GetTypeInfo().IsGenericType)
            {
                listArgumentType = listArgumentType.GetGenericArguments()[0];
            }
            List <object> serializedList      = new List <object>();
            MethodInfo    getEnumeratorMethod = dataContract.GetEnumeratorMethod;

            IEnumerator enumerator = (getEnumeratorMethod == null) ? value.GetEnumerator() : (IEnumerator)getEnumeratorMethod.Invoke(value, Array.Empty <Type>());

            while (enumerator.MoveNext())
            {
                if (enumerator.Current == null || enumerator.Current.GetType().GetTypeInfo().IsPrimitive)
                {
                    serializedList.Add(enumerator.Current);
                }
                else
                {
                    Type         currentItemType         = enumerator.Current.GetType();
                    DataContract currentItemDataContract = DataContract.GetDataContract(currentItemType);
                    bool         emitTypeInformation     = EmitTypeInformation(dataContract.ItemContract, currentItemType);
                    if (writeServerType || emitTypeInformation)
                    {
                        context.CheckIfTypeNeedsVerifcation(dataContract.ItemContract, currentItemDataContract);
                    }
                    context.PushKnownTypes(dataContract);
                    serializedList.Add(serializer.ConvertDataContractToObject(enumerator.Current, currentItemDataContract, context, (writeServerType || emitTypeInformation), dataContract.ItemType.TypeHandle));
                    context.PopKnownTypes(dataContract);
                }
            }
            return(serializedList);
        }
Beispiel #3
0
        private static void WriteClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, ref Dictionary <string, object> classToDictionary, object value, XmlObjectSerializerWriteContextComplexJson context)
        {
            if (dataContract.BaseContract != null)
            {
                WriteClassDataContractMembers(serializer, dataContract.BaseContract, ref classToDictionary, value, context);
            }

            for (int i = 0; i < dataContract.Members.Count; i++)
            {
                DataMember member      = dataContract.Members[i];
                object     memberValue = GetMemberValue(value, dataContract.Members[i].MemberInfo, dataContract.UnderlyingType);
                string     memberName  = System.Xml.XmlConvert.DecodeName(member.Name);

                if (classToDictionary.ContainsKey(memberName))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.Format(SR.JsonDuplicateMemberNames,
                                                                                                                   DataContract.GetClrTypeFullName(dataContract.UnderlyingType), memberName)));
                }

                if (!member.EmitDefaultValue)
                {
                    //Dont emit value if its null or default(valuetype)
                    if (memberValue == null || (member.MemberTypeContract.IsValueType && object.Equals(memberValue, Activator.CreateInstance(member.MemberType))))
                    {
                        continue;
                    }
                }
                if (memberValue == null || IsTypePrimitive(member.MemberType))
                {
                    classToDictionary[memberName] = memberValue;
                }
                else
                {
                    Type memberValueType = memberValue.GetType();
                    if (member.MemberType == memberValueType ||
                        //Special case Nullable<DateTimeOffset> and Nullable<Struct>
                        (member.IsNullable && !EmitTypeInformation(member.MemberTypeContract, memberValueType)))
                    {
                        classToDictionary[memberName] = serializer.ConvertDataContractToObject(memberValue, member.MemberTypeContract, context, false, member.MemberTypeContract.UnderlyingType.TypeHandle);
                    }
                    else
                    {
                        //Push KnownTypes of this DataContract
                        context.PushKnownTypes(dataContract);
                        DataContract memberValueContract = DataContract.GetDataContract(memberValue.GetType());
                        if (member.MemberType.GetTypeInfo().IsInterface)
                        {
                            XmlObjectSerializerWriteContextComplexJson.VerifyObjectCompatibilityWithInterface(memberValueContract, memberValue, member.MemberType);
                        }
                        context.CheckIfTypeNeedsVerifcation(member.MemberTypeContract, memberValueContract);
                        classToDictionary[memberName] = serializer.ConvertDataContractToObject(memberValue, memberValueContract, context, true, member.MemberTypeContract.UnderlyingType.TypeHandle);
                        context.PopKnownTypes(dataContract);
                    }
                }
            }
        }
        private static void WriteClassDataContractMembers(DataContractJsonSerializer serializer, ClassDataContract dataContract, ref Dictionary<string, object> classToDictionary, object value, XmlObjectSerializerWriteContextComplexJson context)
        {
            if (dataContract.BaseContract != null)
            {
                WriteClassDataContractMembers(serializer, dataContract.BaseContract, ref classToDictionary, value, context);
            }

            for (int i = 0; i < dataContract.Members.Count; i++)
            {
                DataMember member = dataContract.Members[i];
                object memberValue = GetMemberValue(value, dataContract.Members[i].MemberInfo, dataContract.UnderlyingType);
                string memberName = System.Xml.XmlConvert.DecodeName(member.Name);

                if (classToDictionary.ContainsKey(memberName))
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SerializationException(SR.Format(SR.JsonDuplicateMemberNames,
    DataContract.GetClrTypeFullName(dataContract.UnderlyingType), memberName)));
                }

                if (!member.EmitDefaultValue)
                {
                    //Dont emit value if its null or default(valuetype)
                    if (memberValue == null || (member.MemberTypeContract.IsValueType && object.Equals(memberValue, Activator.CreateInstance(member.MemberType))))
                    {
                        continue;
                    }
                }
                if (memberValue == null || IsTypePrimitive(member.MemberType))
                {
                    classToDictionary[memberName] = memberValue;
                }
                else
                {
                    Type memberValueType = memberValue.GetType();
                    if (member.MemberType == memberValueType ||
                       //Special case Nullable<DateTimeOffset> and Nullable<Struct>
                       (member.IsNullable && !EmitTypeInformation(member.MemberTypeContract, memberValueType)))
                    {
                        classToDictionary[memberName] = serializer.ConvertDataContractToObject(memberValue, member.MemberTypeContract, context, false, member.MemberTypeContract.UnderlyingType.TypeHandle);
                    }
                    else
                    {
                        //Push KnownTypes of this DataContract
                        context.PushKnownTypes(dataContract);
                        DataContract memberValueContract = DataContract.GetDataContract(memberValue.GetType());
                        if (member.MemberType.GetTypeInfo().IsInterface)
                        {
                            XmlObjectSerializerWriteContextComplexJson.VerifyObjectCompatibilityWithInterface(memberValueContract, memberValue, member.MemberType);
                        }
                        context.CheckIfTypeNeedsVerifcation(member.MemberTypeContract, memberValueContract);
                        classToDictionary[memberName] = serializer.ConvertDataContractToObject(memberValue, memberValueContract, context, true, member.MemberTypeContract.UnderlyingType.TypeHandle);
                        context.PopKnownTypes(dataContract);
                    }
                }
            }
        }