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);
                    }
                }
            }
        }
        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 WriteObject(Stream stream, object graph)
        {
            _jsonSerializer = new JavaScriptSerializer(stream);
            DataContract contract     = RootContract;
            Type         declaredType = contract.UnderlyingType;
            Type         graphType    = (graph == null) ? declaredType : graph.GetType();

            System.Runtime.Serialization.XmlWriterDelegator writer = null;
            if (graph == null)
            {
                _jsonSerializer.SerializeObject(null);
            }
            else
            {
                if (declaredType == graphType)
                {
                    if (contract.CanContainReferences)
                    {
                        XmlObjectSerializerWriteContextComplexJson context = XmlObjectSerializerWriteContextComplexJson.CreateContext(this, contract);
                        context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle);
                    }
                    else
                    {
                        WriteObjectInternal(graph, contract, null, false, declaredType.TypeHandle);
                    }
                }
                else
                {
                    XmlObjectSerializerWriteContextComplexJson context = XmlObjectSerializerWriteContextComplexJson.CreateContext(this, RootContract);
                    contract = DataContractSerializer.GetDataContract(contract, declaredType, graphType);

                    if (contract.CanContainReferences)
                    {
                        context.SerializeWithXsiTypeAtTopLevel(contract, writer, graph, declaredType.TypeHandle, graphType);
                    }
                    else
                    {
                        context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle);
                    }
                }
            }
        }