예제 #1
0
        //todo: those additional parameter are probably useless now.
        static List <XNode> SerializeToXNodes_ValueTypeOrString(object obj, Type objExpectedType, Type objectType, IReadOnlyList <Type> knownTypes, bool useXmlSerializerFormat, string nodeDefaultNamespaceIfAny, bool isRoot, bool isContainedInsideEnumerable, TypeInformation parentTypeInformation)
        {
            string str;

            if (obj is Double)
            {
#if SILVERLIGHT
                str = ((Double)obj).ToString(CultureInfo.InvariantCulture);
#else
                str = ((Double)obj).ToString();
#endif
            }
            else if (obj is char && objExpectedType != typeof(string)) //the second part is required because JSIL is bad at differentiating between char and string.
            {
                str = ((int)((char)obj)).ToString();                   //that looks really bad but it works...
            }
            else if (obj is DateTime)
            {
                DateTime dt = (DateTime)obj;
                str = INTERNAL_DateTimeHelpers.FromDateTime(dt);
            }
            else if (obj is bool)
            {
                str = obj.ToString().ToLower(); //this is required for WCF calls.
            }
            else
            {
                str = obj.ToString();
            }

            XText xtext = new XText(str);

            // If the value is the root or if it is inside an Enumerable, we add an XElement to surround it:
            if (isRoot || isContainedInsideEnumerable)
            {
                return(AddSurroundingXElement(xtext, DataContractSerializer_ValueTypesHandler.TypesToNames[obj.GetType()], isRoot, isContainedInsideEnumerable));
            }
            else
            {
                return new List <XNode>()
                       {
                           xtext
                       }
            };
        }
        //todo: those additional parameter are probably useless now.
        static List <XObject> SerializeToXObjects_ValueTypeOrString(object obj, Type objExpectedType, Type objectType, IReadOnlyList <Type> knownTypes, bool useXmlSerializerFormat, string nodeDefaultNamespaceIfAny, bool isRoot, bool isContainedInsideEnumerable, TypeInformation parentTypeInformation)
        {
            string str;

            if (obj is Double)
            {
#if SILVERLIGHT
                str = ((Double)obj).ToString(CultureInfo.InvariantCulture);
#else
                str = ((Double)obj).ToString();
#endif
            }
            else if (obj is char && objExpectedType != typeof(string)) //the second part is required because JSIL is bad at differentiating between char and string.
            {
                str = ((int)((char)obj)).ToString();                   //that looks really bad but it works...
            }
            else if (obj is DateTime)
            {
                DateTime dt = (DateTime)obj;
                str = INTERNAL_DateTimeHelpers.FromDateTime(dt);
            }
            else if (obj is bool)
            {
                str = obj.ToString().ToLower(); //this is required for WCF calls.
            }
            else
            {
                str = obj.ToString();
            }

            XText xtext = new XText(str);

            // If the value is the root or if it is inside an Enumerable, we add an XElement to surround it.
            // For example, if it is an integer 45, we surround 45 with <int xmlns="http://schemas.microsoft.com/2003/10/Serialization/">45</int>
            if (isRoot || isContainedInsideEnumerable)
            {
                // Determine the name of the type to use in the surrounding XElement:
                Type   objType = obj.GetType();
                string typeName;
                string typeNamespace;
                if (DataContractSerializer_ValueTypesHandler.TypesToNames.ContainsKey(objType))
                {
                    //-----------
                    // System built-in value type (int, bool, etc.)
                    //-----------
                    typeName      = DataContractSerializer_ValueTypesHandler.TypesToNames[objType];
                    typeNamespace = "http://schemas.microsoft.com/2003/10/Serialization/";
                }
                else
                {
                    //-----------
                    // User custom value type or enum
                    //-----------
                    typeName      = objType.Name;
                    typeNamespace = DataContractSerializer_Helpers.GetDefaultNamespace(objType.Namespace, useXmlSerializerFormat);
                }

                return(AddSurroundingXElement(xtext, typeName, typeNamespace, isRoot, isContainedInsideEnumerable));
            }
            else
            {
                return new List <XObject>()
                       {
                           xtext
                       }
            };
        }