Пример #1
0
        private void SerializeValue(object value, bool startEle)
        {
            var type = value.GetType();

            if (type.IsNullableType())
            {
                type = type.GetNonNullableType();
            }

            if (startEle)
            {
                xmlWriter.WriteStartElement(GetElementName(type));
            }

            if (type.IsEnum)
            {
                xmlWriter.WriteValue(((Enum)value).ToString("D"));
            }
            else
            {
                switch (Type.GetTypeCode(type))
                {
                case TypeCode.Boolean:
                    xmlWriter.WriteValue((bool)value == true ? "true" : "false");
                    break;

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Decimal:
                case TypeCode.Single:
                case TypeCode.Double:
                    xmlWriter.WriteValue(value.As <IFormattable>().ToString(context.SerializeInfo?.Formatter ?? "G", CultureInfo.InvariantCulture));
                    break;

                case TypeCode.DateTime:
                    SerializeDateTime((DateTime)value);
                    break;

                case TypeCode.Char:
                case TypeCode.String:
                    SerializeString(value);
                    break;

                default:
                    context.TrySerialize(value, () => SerializeObject(value, false));
                    break;
                }
            }

            if (startEle)
            {
                xmlWriter.WriteEndElement();
            }
        }
Пример #2
0
        private void SerializeValue(object value)
        {
            var type = value.GetType();

            if (type.IsNullableType())
            {
                type = type.GetNonNullableType();
            }

            if (type.IsEnum)
            {
                _jsonWriter.WriteValue(((Enum)value).ToString(_context.SerializeInfo?.Formatter ?? "D"));
                return;
            }
            else if (type == typeof(TimeSpan))
            {
                _jsonWriter.WriteString(((TimeSpan)value).ToString());
                return;
            }

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Boolean:
                _jsonWriter.WriteValue((bool)value ? "true" : "false");
                break;

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
            case TypeCode.Decimal:
            case TypeCode.Single:
            case TypeCode.Double:
                SerializeNumeric(value);
                break;

            case TypeCode.DateTime:
                SerializeDateTime((DateTime)value);
                break;

            case TypeCode.Char:
            case TypeCode.String:
                SerializeString(value);
                break;

            default:
                _context.TrySerialize(value, value => SerializeObject(value));
                break;
            }
        }
Пример #3
0
        private void SerializeValue(object value)
        {
            var type = value.GetType();

            if (type.IsNullableType())
            {
                type = type.GetNonNullableType();
            }

            if (type.IsEnum)
            {
                jsonWriter.WriteValue(((Enum)value).ToString("D"));
                return;
            }

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Boolean:
                jsonWriter.WriteValue((bool)value ? "true" : "false");
                break;

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
            case TypeCode.Decimal:
            case TypeCode.Single:
            case TypeCode.Double:
                jsonWriter.WriteValue(value.As <IFormattable>().ToString("G", CultureInfo.InvariantCulture));
                break;

            case TypeCode.DateTime:
                SerializeDateTime((DateTime)value);
                break;

            case TypeCode.Char:
            case TypeCode.String:
                SerializeString(value);
                break;

            default:
                context.TrySerialize(value, () => SerializeObject(value));
                break;
            }
        }