Example #1
0
        public void DefaultConstructorTest()
        {
            TypesFormatter <TestEntity> sut = new YamlFormatter <TestEntity>();

            Assert.NotNull(sut.KnownTypes);
            Assert.NotEmpty(sut.KnownTypes);
        }
Example #2
0
        public void ConstructorTest(Type[] types, Type[] expected)
        {
            TypesFormatter <TestEntity> sut = new YamlFormatter <TestEntity>(types);

            Assert.NotNull(sut.KnownTypes);
            Assert.NotEmpty(sut.KnownTypes);
            Assert.Equal(expected, sut.KnownTypes);
        }
        public override void Emit(ScalarEventInfo eventInfo)
        {
            eventInfo.IsPlainImplicit = true;
            eventInfo.Style           = ScalarStyle.Plain;

            var typeCode = eventInfo.Source.Value != null
                                ? Type.GetTypeCode(eventInfo.Source.Type)
                                : TypeCode.Empty;

            switch (typeCode)
            {
            case TypeCode.Boolean:
                eventInfo.RenderedValue = YamlFormatter.FormatBoolean(eventInfo.Source.Value);
                break;

            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                eventInfo.RenderedValue = YamlFormatter.FormatNumber(eventInfo.Source.Value);
                break;

            case TypeCode.String:
            case TypeCode.Char:
                eventInfo.RenderedValue = eventInfo.Source.Value.ToString();
                eventInfo.Style         = ScalarStyle.DoubleQuoted;
                break;

            case TypeCode.DateTime:
                eventInfo.RenderedValue = YamlFormatter.FormatDateTime(eventInfo.Source.Value);
                break;

            case TypeCode.Empty:
                eventInfo.RenderedValue = "null";
                break;

            default:
                if (eventInfo.Source.Type == typeof(TimeSpan))
                {
                    eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(eventInfo.Source.Value);
                    break;
                }

                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode));
            }

            base.Emit(eventInfo);
        }
Example #4
0
        public override void Emit(ScalarEventInfo eventInfo)
        {
            eventInfo.IsPlainImplicit = true;
            eventInfo.Style           = ScalarStyle.Plain;
            TypeCode code = (eventInfo.Source.Value == null) ? TypeCode.Empty : eventInfo.Source.Type.GetTypeCode();

            switch (code)
            {
            case TypeCode.Empty:
                eventInfo.RenderedValue = "null";
                break;

            case TypeCode.Boolean:
                eventInfo.RenderedValue = YamlFormatter.FormatBoolean(eventInfo.Source.Value);
                break;

            case TypeCode.Char:
            case TypeCode.String:
                eventInfo.RenderedValue = eventInfo.Source.Value.ToString();
                eventInfo.Style         = ScalarStyle.DoubleQuoted;
                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.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                eventInfo.RenderedValue = YamlFormatter.FormatNumber(eventInfo.Source.Value);
                break;

            case TypeCode.DateTime:
                eventInfo.RenderedValue = YamlFormatter.FormatDateTime(eventInfo.Source.Value);
                break;

            default:
            {
                if (ReferenceEquals(eventInfo.Source.Type, typeof(TimeSpan)))
                {
                    eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(eventInfo.Source.Value);
                    break;
                }
                object[] args = new object[] { code };
                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", args));
            }
            }
            base.Emit(eventInfo);
        }
Example #5
0
        public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
        {
            eventInfo.IsPlainImplicit = true;
            eventInfo.Style           = ScalarStyle.Plain;

            var value = eventInfo.Source.Value;

            if (value == null)
            {
                eventInfo.RenderedValue = "null";
            }
            else
            {
                var typeCode = eventInfo.Source.Type.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    eventInfo.RenderedValue = YamlFormatter.FormatBoolean(value);
                    break;

                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    var valueIsEnum = eventInfo.Source.Type.IsEnum();
                    if (valueIsEnum)
                    {
                        eventInfo.RenderedValue = value.ToString() !;
                        eventInfo.Style         = ScalarStyle.DoubleQuoted;
                        break;
                    }

                    eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
                    break;

                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
                    break;

                case TypeCode.String:
                case TypeCode.Char:
                    eventInfo.RenderedValue = value.ToString() !;
                    eventInfo.Style         = ScalarStyle.DoubleQuoted;
                    break;

                case TypeCode.DateTime:
                    eventInfo.RenderedValue = YamlFormatter.FormatDateTime(value);
                    break;

                case TypeCode.Empty:
                    eventInfo.RenderedValue = "null";
                    break;

                default:
                    if (eventInfo.Source.Type == typeof(TimeSpan))
                    {
                        eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(value);
                        break;
                    }

                    throw new NotSupportedException($"TypeCode.{typeCode} is not supported.");
                }
            }

            base.Emit(eventInfo, emitter);
        }
        public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
        {
            var suggestedStyle = ScalarStyle.Plain;

            var typeCode = eventInfo.Source.Value != null
                ? eventInfo.Source.Type.GetTypeCode()
                : TypeCode.Empty;

            switch (typeCode)
            {
            case TypeCode.Boolean:
                eventInfo.Tag           = "tag:yaml.org,2002:bool";
                eventInfo.RenderedValue = YamlFormatter.FormatBoolean(eventInfo.Source.Value);
                break;

            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.Int32:
            case TypeCode.Int64:
            case TypeCode.SByte:
            case TypeCode.UInt16:
            case TypeCode.UInt32:
            case TypeCode.UInt64:
                eventInfo.Tag           = "tag:yaml.org,2002:int";
                eventInfo.RenderedValue = YamlFormatter.FormatNumber(eventInfo.Source.Value);
                break;

            case TypeCode.Single:
                eventInfo.Tag           = "tag:yaml.org,2002:float";
                eventInfo.RenderedValue = YamlFormatter.FormatNumber((float)eventInfo.Source.Value);
                break;

            case TypeCode.Double:
                eventInfo.Tag           = "tag:yaml.org,2002:float";
                eventInfo.RenderedValue = YamlFormatter.FormatNumber((double)eventInfo.Source.Value);
                break;

            case TypeCode.Decimal:
                eventInfo.Tag           = "tag:yaml.org,2002:float";
                eventInfo.RenderedValue = YamlFormatter.FormatNumber(eventInfo.Source.Value);
                break;

            case TypeCode.String:
            case TypeCode.Char:
                eventInfo.Tag           = "tag:yaml.org,2002:str";
                eventInfo.RenderedValue = eventInfo.Source.Value.ToString();
                suggestedStyle          = ScalarStyle.Any;
                break;

            case TypeCode.DateTime:
                eventInfo.Tag           = "tag:yaml.org,2002:timestamp";
                eventInfo.RenderedValue = YamlFormatter.FormatDateTime(eventInfo.Source.Value);
                break;

            case TypeCode.Empty:
                eventInfo.Tag           = "tag:yaml.org,2002:null";
                eventInfo.RenderedValue = "";
                break;

            default:
                if (eventInfo.Source.Type == typeof(TimeSpan))
                {
                    eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(eventInfo.Source.Value);
                    break;
                }

                throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture, "TypeCode.{0} is not supported.", typeCode));
            }

            eventInfo.IsPlainImplicit = true;
            if (eventInfo.Style == ScalarStyle.Any)
            {
                eventInfo.Style = suggestedStyle;
            }

            base.Emit(eventInfo, emitter);
        }
        public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
        {
            var suggestedStyle = ScalarStyle.Plain;

            var value = eventInfo.Source.Value;

            if (value == null)
            {
                eventInfo.Tag           = JsonSchema.Tags.Null;
                eventInfo.RenderedValue = "";
            }
            else
            {
                var typeCode = eventInfo.Source.Type.GetTypeCode();
                switch (typeCode)
                {
                case TypeCode.Boolean:
                    eventInfo.Tag           = JsonSchema.Tags.Bool;
                    eventInfo.RenderedValue = YamlFormatter.FormatBoolean(value);
                    break;

                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                    eventInfo.Tag           = JsonSchema.Tags.Int;
                    eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
                    break;

                case TypeCode.Single:
                    eventInfo.Tag           = JsonSchema.Tags.Float;
                    eventInfo.RenderedValue = YamlFormatter.FormatNumber((float)value);
                    break;

                case TypeCode.Double:
                    eventInfo.Tag           = JsonSchema.Tags.Float;
                    eventInfo.RenderedValue = YamlFormatter.FormatNumber((double)value);
                    break;

                case TypeCode.Decimal:
                    eventInfo.Tag           = JsonSchema.Tags.Float;
                    eventInfo.RenderedValue = YamlFormatter.FormatNumber(value);
                    break;

                case TypeCode.String:
                case TypeCode.Char:
                    eventInfo.Tag           = FailsafeSchema.Tags.Str;
                    eventInfo.RenderedValue = value.ToString() !;
                    suggestedStyle          = ScalarStyle.Any;
                    break;

                case TypeCode.DateTime:
                    eventInfo.Tag           = DefaultSchema.Tags.Timestamp;
                    eventInfo.RenderedValue = YamlFormatter.FormatDateTime(value);
                    break;

                case TypeCode.Empty:
                    eventInfo.Tag           = JsonSchema.Tags.Null;
                    eventInfo.RenderedValue = "";
                    break;

                default:
                    if (eventInfo.Source.Type == typeof(TimeSpan))
                    {
                        eventInfo.RenderedValue = YamlFormatter.FormatTimeSpan(value);
                        break;
                    }

                    throw new NotSupportedException($"TypeCode.{typeCode} is not supported.");
                }
            }

            eventInfo.IsPlainImplicit = true;
            if (eventInfo.Style == ScalarStyle.Any)
            {
                eventInfo.Style = suggestedStyle;
            }

            base.Emit(eventInfo, emitter);
        }