public void WriteYaml(ref ObjectContext objectContext)
        {
            var value = objectContext.Instance;
            var typeOfValue = value.GetType();

            var context = objectContext.SerializerContext;

            var isSchemaImplicitTag = context.Schema.IsTagImplicit(objectContext.Tag);
            var scalar = new ScalarEventInfo(value, typeOfValue)
                {
                    IsPlainImplicit = isSchemaImplicitTag,
                    Style = ScalarStyle.Plain,
                    Anchor = objectContext.Anchor,
                    Tag = objectContext.Tag,
                };

            // Parse default types
            switch (Type.GetTypeCode(typeOfValue))
            {
                case TypeCode.Object:
                case TypeCode.String:
                case TypeCode.Char:
                    scalar.Style = ScalarStyle.Any;
                    break;
            }

            scalar.RenderedValue = ConvertTo(ref objectContext);

            // Emit the scalar
            WriteScalar(ref objectContext, scalar);
        }
Example #2
0
        public override void VisitScalar(IObjectDescriptor scalar)
        {
            ScalarEventInfo eventInfo = new ScalarEventInfo(scalar)
            {
                Anchor = this.aliasProvider.GetAlias(scalar.Value)
            };

            this.eventEmitter.Emit(eventInfo);
        }
Example #3
0
            protected override void WriteScalar(ref ObjectContext objectContext, ScalarEventInfo scalar)
            {
                // Remove the tag if one was added, which might happen if the concrete type is different from the container type.
                // NOTE: disabled for now, although it doesn't seem necessary anymore. To re-enable removing, just uncomment these two lines
                //scalar.Tag = null;
                //scalar.IsPlainImplicit = true;

                // Emit the scalar
                objectContext.SerializerContext.Writer.Emit(scalar);
            }
Example #4
0
        public override void VisitScalar(IObjectDescriptor scalar, IEmitter context)
        {
            var scalarInfo = new ScalarEventInfo(scalar);

            if (scalar.Value != null)
            {
                scalarInfo.Anchor = aliasProvider.GetAlias(scalar.Value);
            }
            eventEmitter.Emit(scalarInfo, context);
        }
        public override void Emit(ScalarEventInfo eventInfo)
        {
            eventInfo.IsPlainImplicit = true;
            eventInfo.Style = ScalarStyle.Plain;

            var typeCode = eventInfo.Source.Value != null
                ? eventInfo.Source.Type.GetTypeCode()
                : 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);
        }
        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 #7
0
 public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
 {
     if (eventInfo.Source.Value == null)
     {
         emitter.Emit(new Scalar("[null]"));
     }
     else
     {
         base.Emit(eventInfo, emitter);
     }
 }
 public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
 {
     if (eventInfo.Source.Type == typeof(string) && eventInfo.Source.Value == null)
     {
         emitter.Emit(new Scalar(string.Empty));
     }
     else
     {
         base.Emit(eventInfo, emitter);
     }
 }
    public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
    {
        if (eventInfo.Source.Value is string str)
        {
            if (str.Contains('\n') && !str.Contains(" \n") && !str.EndsWith(" "))
            {
                eventInfo.Style = ScalarStyle.Literal;
            }
        }

        base.Emit(eventInfo, emitter);
    }
Example #10
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 #11
0
        public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
        {
            if (eventInfo.Source.Value is string stringValue && !string.IsNullOrEmpty(stringValue))
            {
                if (NumberRegex.IsMatch(stringValue) || BoolRegex.IsMatch(stringValue))
                {
                    eventInfo.Style = ScalarStyle.DoubleQuoted;
                }
            }

            base.Emit(eventInfo, emitter);
        }
Example #12
0
        public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
        {
            if (this.state.Peek().VisitNext())
            {
                if (eventInfo.Source.Type == typeof(string))
                {
                    eventInfo.Style = ScalarStyle.DoubleQuoted;
                }
            }

            base.Emit(eventInfo, emitter);
        }
Example #13
0
 public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
 {
     if (eventInfo.Source.Type == typeof(string))
     {
         var value = (string)eventInfo.Source.Value;
         if (!String.IsNullOrEmpty(value) && value.Contains('\n'))
         {
             var ev = new Scalar(null, null, value, ScalarStyle.Literal, true, false);
             emitter.Emit(ev);
             return;
         }
     }
     base.Emit(eventInfo, emitter);
 }
Example #14
0
        public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
        {
            if (state.Peek().VisitNext())
            {
                // JSONのnullは""に変換される。
                eventInfo.Style = ScalarStyle.DoubleQuoted;
                //if (eventInfo.Source.Type == typeof(string))
                //{
                //    eventInfo.Style = ScalarStyle.DoubleQuoted;
                //}
            }

            base.Emit(eventInfo, emitter);
        }
Example #15
0
 public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
 {
     if (eventInfo.Source.Type == typeof(string))
     {
         if (!(eventInfo.Source.Value is string str))
         {
         }
         else if (str.Contains("\n") || str.Contains("\t"))
         {
             eventInfo.Style = ScalarStyle.DoubleQuoted;
         }
         else if (str.Contains("~"))
         {
             eventInfo.Style = ScalarStyle.SingleQuoted;
         }
     }
Example #16
0
            public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
            {
                // prefer the literal style for multi-line strings
                if (eventInfo.Source.Type == typeof(string) && eventInfo.Style == ScalarStyle.Any && ((string)eventInfo.Source.Value).IndexOf('\n') != -1)
                {
                    eventInfo.Style = ScalarStyle.Literal;
                }

                // ensure strings that look like numbers remain strings
                double unused;

                if (eventInfo.Source.Type == typeof(string) && eventInfo.Style == ScalarStyle.Any && double.TryParse((string)eventInfo.Source.Value, out unused))
                {
                    eventInfo.Style = ScalarStyle.SingleQuoted;
                }

                base.Emit(eventInfo, emitter);
            }
Example #17
0
        public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
        {
            if (eventInfo.Source.StaticType == typeof(object) &&
                eventInfo.Source.Type == typeof(string) &&
                eventInfo.Style == ScalarStyle.Any)
            {
                var scalarValue = (string)eventInfo.Source.Value;
                if (bool.TryParse(scalarValue, out var boolValue) ||
                    int.TryParse(scalarValue, out var intValue) ||
                    double.TryParse(scalarValue, out var doubleValue) ||
                    string.Equals(scalarValue, "null", StringComparison.Ordinal))
                {
                    eventInfo.Style = ScalarStyle.DoubleQuoted;
                }
            }

            base.Emit(eventInfo, emitter);
        }
Example #18
0
     public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
     {
         if (typeof(string).IsAssignableFrom(eventInfo.Source.Type))
         {
             string value = eventInfo.Source.Value as string;
             if (!string.IsNullOrEmpty(value))
             {
                 bool isMultiLine = value.IndexOfAny(new char[] { '\r', '\n', '\x85', '\x2028', '\x2029' }) >= 0;
                 if (isMultiLine)
                 {
                     eventInfo = new ScalarEventInfo(eventInfo.Source)
                     {
                         Style = ScalarStyle.Literal
                     }
                 }
                 ;
             }
         }
         nextEmitter.Emit(eventInfo, emitter);
     }
 }
Example #19
0
        public override void Emit(ScalarEventInfo evt, IEmitter emitter)
        {
            if (evt.Source.Type == typeof(string))
            {
                var value = evt.Source.Value?.ToString();
                if (Constants.YamlBoolRegex.IsMatch(value))
                {
                    evt.Style = ScalarStyle.DoubleQuoted;
                }
                else if (double.TryParse(value, out double _))
                {
                    evt.Style = ScalarStyle.DoubleQuoted;
                }
                else if (evt.Source.Value?.ToString().Contains('\n') == true)
                {
                    evt.Style = ScalarStyle.Literal;
                }
            }

            nextEmitter.Emit(evt, emitter);
        }
Example #20
0
        public void WriteYaml(ref ObjectContext objectContext)
        {
            var value       = objectContext.Instance;
            var typeOfValue = value.GetType();

            var context = objectContext.SerializerContext;

            var isSchemaImplicitTag = context.Schema.IsTagImplicit(objectContext.Tag);
            var scalar = new ScalarEventInfo(value, typeOfValue)
            {
                IsPlainImplicit = isSchemaImplicitTag,
                Style           = objectContext.ScalarStyle,
                Anchor          = objectContext.Anchor,
                Tag             = objectContext.Tag,
            };


            if (scalar.Style == ScalarStyle.Any)
            {
                // Parse default types
                switch (Type.GetTypeCode(typeOfValue))
                {
                case TypeCode.Object:
                case TypeCode.String:
                case TypeCode.Char:
                    break;

                default:
                    scalar.Style = ScalarStyle.Plain;
                    break;
                }
            }

            scalar.RenderedValue = ConvertTo(ref objectContext);

            // Emit the scalar
            WriteScalar(ref objectContext, scalar);
        }
Example #21
0
 protected virtual void WriteScalar(ref ObjectContext objectContext, ScalarEventInfo scalar)
 {
     // Emit the scalar
     objectContext.SerializerContext.Writer.Emit(scalar);
 }
Example #22
0
 public virtual void Emit(ScalarEventInfo eventInfo)
 {
     nextEmitter.Emit(eventInfo);
 }
Example #23
0
 public virtual void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
 {
     nextEmitter.Emit(eventInfo, emitter);
 }
Example #24
0
 public virtual void Emit(ScalarEventInfo eventInfo)
 {
     nextEmitter.Emit(eventInfo);
 }
Example #25
0
 public virtual void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
 {
     nextEmitter.Emit(eventInfo, emitter);
 }
 void IEventEmitter.Emit(ScalarEventInfo eventInfo, IEmitter emitter)
 {
     emitter.Emit(new Scalar(eventInfo.Anchor, eventInfo.Tag, eventInfo.RenderedValue, eventInfo.Style, eventInfo.IsPlainImplicit, eventInfo.IsQuotedImplicit));
 }
Example #27
0
		void IEventEmitter.Emit(ScalarEventInfo eventInfo)
		{
			emitter.Emit(new Scalar(eventInfo.Anchor, eventInfo.Tag, eventInfo.RenderedValue, eventInfo.Style, eventInfo.IsPlainImplicit, eventInfo.IsQuotedImplicit));
		}
        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);
        }
 /// <summary>
 /// Writes the scalar to the <see cref="SerializerContext.Writer"/>. See remarks.
 /// </summary>
 /// <param name="objectContext">The object context.</param>
 /// <param name="scalar">The scalar.</param>
 /// <remarks>
 /// This method can be overloaded to replace the converted scalar just before writing it.
 /// </remarks>
 protected virtual void WriteScalar(ref ObjectContext objectContext, ScalarEventInfo scalar)
 {
     // Emit the scalar
     objectContext.SerializerContext.Writer.Emit(scalar);
 }
Example #30
0
 protected override void WriteScalar(ref ObjectContext objectContext, ScalarEventInfo scalar)
 {
     realScalarSerializer.WriteScalar(ref objectContext, scalar);
 }
        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);
        }
Example #33
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)
        {
            eventInfo.IsPlainImplicit = true;
            eventInfo.Style = ScalarStyle.Plain;

            if (eventInfo.SourceValue == null)
            {
                eventInfo.Tag = "tag:yaml.org,2002:null";
                eventInfo.RenderedValue = "";
                return;
            }

            var typeCode = Type.GetTypeCode(eventInfo.SourceType);
            switch (typeCode)
            {
                case TypeCode.Boolean:
                    eventInfo.Tag = "tag:yaml.org,2002:bool";
                    eventInfo.RenderedValue = YamlFormatter.FormatBoolean(eventInfo.SourceValue);
                    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.SourceValue);
                    break;

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

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

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

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

            base.Emit(eventInfo);
        }