当前位置: 首页>>代码示例>>C#>>正文


C# DataContractJsonSerializer.ConvertDataContractToObject方法代码示例

本文整理汇总了C#中System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertDataContractToObject方法的典型用法代码示例。如果您正苦于以下问题:C# DataContractJsonSerializer.ConvertDataContractToObject方法的具体用法?C# DataContractJsonSerializer.ConvertDataContractToObject怎么用?C# DataContractJsonSerializer.ConvertDataContractToObject使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Runtime.Serialization.Json.DataContractJsonSerializer的用法示例。


在下文中一共展示了DataContractJsonSerializer.ConvertDataContractToObject方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConvertGenericListToArray

        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;
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:33,代码来源:DataContractToObjectConverter.cs

示例2: AddDictionaryEntryData

 private static void AddDictionaryEntryData(DataContractJsonSerializer serializer, Dictionary<string, object> currentEntry, string key, Type declaredType, object value, XmlObjectSerializerWriteContextComplexJson context)
 {
     if (value == null)
     {
         currentEntry[key] = value;
         return;
     }
     Type runtimeType = value.GetType();
     if (IsTypePrimitive(runtimeType))
     {
         currentEntry[key] = value;
     }
     else
     {
         if (declaredType.GetTypeInfo().IsGenericType && declaredType.GetGenericTypeDefinition() == Globals.TypeOfNullable)
         {
             declaredType = Nullable.GetUnderlyingType(declaredType);
         }
         DataContract runtimeDataContract = DataContract.GetDataContract(runtimeType);
         if (declaredType != runtimeType)
         {
             context.VerifyType(runtimeDataContract);
         }
         currentEntry[key] = serializer.ConvertDataContractToObject(value, runtimeDataContract, context, EmitTypeInformation(runtimeDataContract, runtimeType), runtimeDataContract.UnderlyingType.TypeHandle);
     }
 }
开发者ID:noahfalk,项目名称:corefx,代码行数:26,代码来源:DataContractToObjectConverter.cs

示例3: WriteClassDataContractMembers

        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);
                    }
                }
            }
        }
开发者ID:noahfalk,项目名称:corefx,代码行数:56,代码来源:DataContractToObjectConverter.cs


注:本文中的System.Runtime.Serialization.Json.DataContractJsonSerializer.ConvertDataContractToObject方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。