Inheritance: System.Runtime.Serialization.XmlObjectSerializer
        internal override object InternalReadObject(XmlReaderDelegator xmlReader, bool verifyObjectName)
        {
            if (MaxItemsInObjectGraph == 0)
            {
                throw XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ExceededMaxItemsQuota, MaxItemsInObjectGraph));
            }

            if (verifyObjectName)
            {
                if (!InternalIsStartObject(xmlReader))
                {
                    throw XmlObjectSerializer.CreateSerializationExceptionWithReaderDetails(SR.Format(SR.ExpectingElement, XmlDictionaryString.Empty, RootName), xmlReader);
                }
            }
            else if (!IsStartElement(xmlReader))
            {
                throw XmlObjectSerializer.CreateSerializationExceptionWithReaderDetails(SR.Format(SR.ExpectingElementAtDeserialize, XmlNodeType.Element), xmlReader);
            }

            DataContract contract = RootContract;

            if (contract.IsPrimitive && object.ReferenceEquals(contract.UnderlyingType, _rootType))// handle Nullable<T> differently
            {
                return(DataContractJsonSerializerImpl.ReadJsonValue(contract, xmlReader, null));
            }

            XmlObjectSerializerReadContextComplexJson context = XmlObjectSerializerReadContextComplexJson.CreateContext(this, contract);

            return(context.InternalDeserialize(xmlReader, _rootType, contract, null, null));
        }
 internal XmlObjectSerializerReadContextComplexJson(DataContractJsonSerializerImpl serializer, DataContract rootTypeDataContract)
     : base(serializer, serializer.MaxItemsInObjectGraph, new StreamingContext(), false)
 {
     this.rootTypeDataContract    = rootTypeDataContract;
     this.serializerKnownTypeList = serializer.knownTypeList;
     _dateTimeFormat            = serializer.DateTimeFormat;
     _useSimpleDictionaryFormat = serializer.UseSimpleDictionaryFormat;
 }
 internal XmlObjectSerializerReadContextComplexJson(DataContractJsonSerializerImpl serializer, DataContract rootTypeDataContract)
     : base(serializer, serializer.MaxItemsInObjectGraph, new StreamingContext(), false)
 {
     this.rootTypeDataContract = rootTypeDataContract;
     this.serializerKnownTypeList = serializer.knownTypeList;
     _dateTimeFormat = serializer.DateTimeFormat;
     _useSimpleDictionaryFormat = serializer.UseSimpleDictionaryFormat;
 }
        public DataContractJsonSerializer(Type type, IEnumerable<Type> knownTypes)
        {
#if NET_NATIVE || MERGE_DCJS
            _serializer = new DataContractJsonSerializerImpl(type, knownTypes);
#else
            Initialize(type, knownTypes);
#endif
        }
Example #5
0
            private int WriteMembers(ClassDataContract classContract, LocalBuilder extensionDataLocal, ClassDataContract derivedMostClassContract)
            {
                int memberCount = (classContract.BaseContract == null) ? 0 :
                                  WriteMembers(classContract.BaseContract, extensionDataLocal, derivedMostClassContract);

                _ilg.Call(_contextArg, XmlFormatGeneratorStatics.IncrementItemCountMethod, classContract.Members.Count);

                for (int i = 0; i < classContract.Members.Count; i++, memberCount++)
                {
                    DataMember   member      = classContract.Members[i];
                    Type         memberType  = member.MemberType;
                    LocalBuilder memberValue = null;
                    if (member.IsGetOnlyCollection)
                    {
                        _ilg.Load(_contextArg);
                        _ilg.Call(XmlFormatGeneratorStatics.StoreIsGetOnlyCollectionMethod);
                    }
                    if (!member.EmitDefaultValue)
                    {
                        memberValue = LoadMemberValue(member);
                        _ilg.IfNotDefaultValue(memberValue);
                    }

                    bool requiresNameAttribute = DataContractJsonSerializerImpl.CheckIfXmlNameRequiresMapping(classContract.MemberNames[i]);
                    if (requiresNameAttribute || !TryWritePrimitive(memberType, memberValue, member.MemberInfo, null /*arrayItemIndex*/, null /*nameLocal*/, i + _childElementIndex))
                    {
                        // Note: DataContractSerializer has member-conflict logic here to deal with the schema export
                        //       requirement that the same member can't be of two different types.
                        if (requiresNameAttribute)
                        {
                            _ilg.Call(null, JsonFormatGeneratorStatics.WriteJsonNameWithMappingMethod, _xmlWriterArg, _memberNamesArg, i + _childElementIndex);
                        }
                        else
                        {
                            WriteStartElement(null /*nameLocal*/, i + _childElementIndex);
                        }
                        if (memberValue == null)
                        {
                            memberValue = LoadMemberValue(member);
                        }
                        WriteValue(memberValue);
                        WriteEndElement();
                    }
                    if (!member.EmitDefaultValue)
                    {
                        if (member.IsRequired)
                        {
                            _ilg.Else();
                            _ilg.Call(null, XmlFormatGeneratorStatics.ThrowRequiredMemberMustBeEmittedMethod, member.Name, classContract.UnderlyingType);
                        }
                        _ilg.EndIf();
                    }
                }

                _typeIndex++;
                _childElementIndex += classContract.Members.Count;
                return(memberCount);
            }
Example #6
0
 internal XmlObjectSerializerWriteContextComplexJson(DataContractJsonSerializerImpl serializer, DataContract rootTypeDataContract)
     : base(serializer, serializer.MaxItemsInObjectGraph, default(StreamingContext), false)
 {
     _emitXsiType = serializer.EmitTypeInformation;
     this.rootTypeDataContract    = rootTypeDataContract;
     this.serializerKnownTypeList = serializer.knownTypeList;
     this.serializeReadOnlyTypes  = serializer.SerializeReadOnlyTypes;
     _useSimpleDictionaryFormat   = serializer.UseSimpleDictionaryFormat;
 }
 internal XmlObjectSerializerWriteContextComplexJson(DataContractJsonSerializerImpl serializer, DataContract rootTypeDataContract)
     : base(serializer, serializer.MaxItemsInObjectGraph, new StreamingContext(), false)
 {
     _emitXsiType = serializer.EmitTypeInformation;
     this.rootTypeDataContract = rootTypeDataContract;
     this.serializerKnownTypeList = serializer.knownTypeList;
     this.serializeReadOnlyTypes = serializer.SerializeReadOnlyTypes;
     _useSimpleDictionaryFormat = serializer.UseSimpleDictionaryFormat;
 }
Example #8
0
        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);
        }
        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);
                    }
                }
            }
        }
Example #10
0
        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
        }
        public DataContractJsonSerializer(Type type, DataContractJsonSerializerSettings settings)
        {
#if NET_NATIVE || MERGE_DCJS
            _serializer = new DataContractJsonSerializerImpl(type, settings);
#else
            if (settings == null)
            {
                settings = new DataContractJsonSerializerSettings();
            }

            XmlDictionaryString rootName = (settings.RootName == null) ? null : new XmlDictionary(1).Add(settings.RootName);
            Initialize(type, rootName, settings.KnownTypes, settings.MaxItemsInObjectGraph, settings.IgnoreExtensionDataObject,
                settings.EmitTypeInformation, settings.SerializeReadOnlyTypes, settings.DateTimeFormat, settings.UseSimpleDictionaryFormat);
#endif
        }
        public override object ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context)
        {
            object obj;
            string contentMode = jsonReader.GetAttribute(JsonGlobals.typeString);

            switch (contentMode)
            {
            case JsonGlobals.nullString:
                jsonReader.Skip();
                obj = null;
                break;

            case JsonGlobals.booleanString:
                obj = jsonReader.ReadElementContentAsBoolean();
                break;

            case JsonGlobals.stringString:
            case null:
                obj = jsonReader.ReadElementContentAsString();
                break;

            case JsonGlobals.numberString:
                obj = ParseJsonNumber(jsonReader.ReadElementContentAsString());
                break;

            case JsonGlobals.objectString:
                jsonReader.Skip();
                obj = new object();
                break;

            case JsonGlobals.arrayString:
                // Read as object array
                return(DataContractJsonSerializerImpl.ReadJsonValue(DataContract.GetDataContract(Globals.TypeOfObjectArray), jsonReader, context));

            default:
                throw XmlObjectSerializer.CreateSerializationException(SR.Format(SR.JsonUnexpectedAttributeValue, contentMode));
            }

            if (context != null)
            {
                context.AddNewObject(obj);
            }
            return(obj);
        }
 private void CopyMembersAndCheckDuplicateNames()
 {
     if (_traditionalClassDataContract.MemberNames != null)
     {
         int memberCount = _traditionalClassDataContract.MemberNames.Length;
         Dictionary <string, object> memberTable        = new Dictionary <string, object>(memberCount);
         XmlDictionaryString[]       decodedMemberNames = new XmlDictionaryString[memberCount];
         for (int i = 0; i < memberCount; i++)
         {
             if (memberTable.ContainsKey(_traditionalClassDataContract.MemberNames[i].Value))
             {
                 throw new SerializationException(SR.Format(SR.JsonDuplicateMemberNames, DataContract.GetClrTypeFullName(_traditionalClassDataContract.UnderlyingType), _traditionalClassDataContract.MemberNames[i].Value));
             }
             else
             {
                 memberTable.Add(_traditionalClassDataContract.MemberNames[i].Value, null);
                 decodedMemberNames[i] = DataContractJsonSerializerImpl.ConvertXmlNameToJsonName(_traditionalClassDataContract.MemberNames[i]);
             }
         }
         _memberNames = decodedMemberNames;
     }
 }
 internal static XmlObjectSerializerWriteContextComplexJson CreateContext(DataContractJsonSerializerImpl serializer, DataContract rootTypeDataContract)
 {
     return new XmlObjectSerializerWriteContextComplexJson(serializer, rootTypeDataContract);
 }
 public DataContractJsonSerializer(Type type, DataContractJsonSerializerSettings settings)
 {
     _serializer = new DataContractJsonSerializerImpl(type, settings);
 }
 public DataContractJsonSerializer(Type type, IEnumerable <Type> knownTypes)
 {
     _serializer = new DataContractJsonSerializerImpl(type, knownTypes);
 }
 public DataContractJsonSerializer(Type type)
 {
     _serializer = new DataContractJsonSerializerImpl(type);
 }
 protected override object ReadDataContractValue(DataContract dataContract, XmlReaderDelegator reader)
 {
     return(DataContractJsonSerializerImpl.ReadJsonValue(dataContract, reader, this));
 }
 public DataContractJsonSerializer(Type type)
 {
     _serializer = new DataContractJsonSerializerImpl(type);
 }
Example #20
0
 protected override void WriteNull(XmlWriterDelegator xmlWriter)
 {
     DataContractJsonSerializerImpl.WriteJsonNull(xmlWriter);
 }
 public DataContractJsonSerializer(Type type, IEnumerable<Type> knownTypes)
 {
     _serializer = new DataContractJsonSerializerImpl(type, knownTypes);
 }
Example #22
0
 public DataContractJsonSerializer(Type type, XmlDictionaryString?rootName, IEnumerable <Type>?knownTypes)
 {
     _serializer = new DataContractJsonSerializerImpl(type, rootName, knownTypes);
 }
 public DataContractJsonSerializer(Type type, XmlDictionaryString rootName, IEnumerable<Type> knownTypes)
 {
     _serializer = new DataContractJsonSerializerImpl(type, rootName, knownTypes);
 }
 internal static XmlObjectSerializerReadContextComplexJson CreateContext(DataContractJsonSerializerImpl serializer, DataContract rootTypeDataContract)
 {
     return(new XmlObjectSerializerReadContextComplexJson(serializer, rootTypeDataContract));
 }
Example #25
0
        protected override void WriteNull(XmlWriterDelegator xmlWriter)
        {
#if NET_NATIVE || MERGE_DCJS
            DataContractJsonSerializerImpl.WriteJsonNull(xmlWriter);
#endif
        }
Example #26
0
        protected override int ReflectionWriteMembers(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, ClassDataContract classContract, ClassDataContract derivedMostClassContract, int childElementIndex, XmlDictionaryString[] memberNames)
        {
            int memberCount = (classContract.BaseContract == null) ? 0 :
                              ReflectionWriteMembers(xmlWriter, obj, context, classContract.BaseContract, derivedMostClassContract, childElementIndex, memberNames);

            childElementIndex += memberCount;

            context.IncrementItemCount(classContract.Members.Count);
            for (int i = 0; i < classContract.Members.Count; i++, memberCount++)
            {
                DataMember member     = classContract.Members[i];
                Type       memberType = member.MemberType;
                if (member.IsGetOnlyCollection)
                {
                    context.StoreIsGetOnlyCollection();
                }
                else
                {
                    context.ResetIsGetOnlyCollection();
                }


                bool   shouldWriteValue = true;
                object memberValue      = null;
                if (!member.EmitDefaultValue)
                {
                    memberValue = ReflectionGetMemberValue(obj, member);
                    object defaultValue = XmlFormatGeneratorStatics.GetDefaultValue(memberType);
                    if ((memberValue == null && defaultValue == null) ||
                        (memberValue != null && memberValue.Equals(defaultValue)))
                    {
                        shouldWriteValue = false;

                        if (member.IsRequired)
                        {
                            XmlObjectSerializerWriteContext.ThrowRequiredMemberMustBeEmitted(member.Name, classContract.UnderlyingType);
                        }
                    }
                }

                if (shouldWriteValue)
                {
                    if (memberValue == null)
                    {
                        memberValue = ReflectionGetMemberValue(obj, member);
                    }
                    bool requiresNameAttribute = DataContractJsonSerializerImpl.CheckIfXmlNameRequiresMapping(classContract.MemberNames[i]);
                    PrimitiveDataContract primitiveContract = member.MemberPrimitiveContract;
                    if (requiresNameAttribute || !ReflectionTryWritePrimitive(xmlWriter, context, memberType, memberValue, memberNames[i + childElementIndex] /*name*/, null /*ns*/, primitiveContract))
                    {
                        // Note: DataContractSerializer has member-conflict logic here to deal with the schema export
                        //       requirement that the same member can't be of two different types.
                        if (requiresNameAttribute)
                        {
                            XmlObjectSerializerWriteContextComplexJson.WriteJsonNameWithMapping(xmlWriter, memberNames, i + childElementIndex);
                        }
                        else
                        {
                            ReflectionWriteStartElement(xmlWriter, memberNames[i + childElementIndex]);
                        }

                        ReflectionWriteValue(xmlWriter, context, memberType, memberValue, false /*writeXsiType*/, primitiveContractForParamType: null);
                        ReflectionWriteEndElement(xmlWriter);
                    }

                    if (classContract.HasExtensionData)
                    {
                        context.WriteExtensionData(xmlWriter, ((IExtensibleDataObject)obj).ExtensionData, memberCount);
                    }
                }
            }

            return(memberCount);
        }
 public DataContractJsonSerializer(Type type, DataContractJsonSerializerSettings settings)
 {
     _serializer = new DataContractJsonSerializerImpl(type, settings);
 }