internal object ConvertObjectToDataContract(DataContract contract, object value, XmlObjectSerializerReadContextComplexJson context)
        {
            if (value == null)
            {
                return(value);
            }
            else if (contract is PrimitiveDataContract)
            {
                return(ConvertObjectToPrimitiveDataContract(contract, value, context));
            }
            else if (contract is CollectionDataContract)
            {
                return(ObjectToDataContractConverter.ConvertICollectionToCollectionDataContract(this, (CollectionDataContract)contract, value, context));
            }
            else if (contract is ClassDataContract)
            {
                ClassDataContract classContract = contract as ClassDataContract;

                if (Globals.TypeOfScriptObject_IsAssignableFrom(classContract.UnderlyingType))
                {
                    return(ConvertObjectToScriptObject(value));
                }

                return(ObjectToDataContractConverter.ConvertDictionaryToClassDataContract(this, classContract, (Dictionary <string, object>)value, context));
            }
            else if (contract is EnumDataContract)
            {
                return(Enum.ToObject(contract.UnderlyingType, ((EnumDataContract)contract).IsULong ? ulong.Parse(String.Format(CultureInfo.InvariantCulture, "{0}", value), NumberStyles.Float, NumberFormatInfo.InvariantInfo) : value));
            }
            else if (contract is XmlDataContract)
            {
                DataContractSerializer dataContractSerializer = new DataContractSerializer(contract.UnderlyingType, GetKnownTypesFromContext(context, (context == null) ? null : context.SerializerKnownTypeList));
                MemoryStream           memoryStream           = new MemoryStream(Encoding.UTF8.GetBytes((string)value));
                return(dataContractSerializer.ReadObject(XmlDictionaryReader.CreateTextReader(memoryStream, XmlDictionaryReaderQuotas.Max)));
            }
            return(value);
        }
        private object ConvertObjectToPrimitiveDataContract(DataContract contract, object value, XmlObjectSerializerReadContextComplexJson context)
        {
            // Taking the right deserialized value for datetime string based on contract information
            var tuple = value as Tuple <DateTime, string>;

            if (tuple != null)
            {
                if (contract is StringDataContract || contract.UnderlyingType == typeof(object))
                {
                    value = tuple.Item2;
                }
                else
                {
                    value = tuple.Item1;
                }
            }

            if (contract is TimeSpanDataContract)
            {
                return(XmlConvert.ToTimeSpan(String.Format(CultureInfo.InvariantCulture, "{0}", value)));
            }
            else if (contract is ByteArrayDataContract)
            {
                return(ObjectToDataContractConverter.ConvertToArray(typeof(Byte), (IList)value));
            }
            else if (contract is GuidDataContract)
            {
                return(new Guid(String.Format(CultureInfo.InvariantCulture, "{0}", value)));
            }
            else if (contract is ObjectDataContract)
            {
                if (value is ICollection)
                {
                    return(ConvertObjectToDataContract(DataContract.GetDataContract(Globals.TypeOfObjectArray), value, context));
                }

                return(TryParseJsonNumber(value));
            }
            else if (contract is QNameDataContract)
            {
                return(XmlObjectSerializerReadContextComplexJson.ParseQualifiedName(value.ToString()));
            }
            else if (contract is StringDataContract)
            {
                if (value is bool)
                {
                    return(((bool)value) ? Globals.True : Globals.False);
                }
                return(value.ToString());
            }
            else if (contract is UriDataContract)
            {
                return(new Uri(value.ToString(), UriKind.RelativeOrAbsolute));
            }
            else if (contract is DoubleDataContract)
            {
                if (value is float)
                {
                    return((double)(float)value);
                }
                if (value is double)
                {
                    return((double)value);
                }
                return(double.Parse(String.Format(CultureInfo.InvariantCulture, "{0}", value), NumberStyles.Float, CultureInfo.InvariantCulture));
            }
            else if (contract is DecimalDataContract)
            {
                return(decimal.Parse(String.Format(CultureInfo.InvariantCulture, "{0}", value), NumberStyles.Float, CultureInfo.InvariantCulture));
            }

            return(Convert.ChangeType(value, contract.UnderlyingType, CultureInfo.InvariantCulture));
        }