コード例 #1
0
 public override void Write(object value, IJsonConsumer writer)
 {
     if (!mEnumValueToString.TryGetValue(value, out string str))
     {
         throw new Exception($"Unrecognized enumeration value '{value}'. Valid values are {string.Join(", ", mStringToEnumValue.Keys)}.");
     }
     writer.String(str);
 }
コード例 #2
0
        protected override void InternalWrite(DateTime value, IJsonConsumer writer)
        {
            StringBuilder sb = new StringBuilder(50);

            if (value.Year < 10)
            {
                sb.Append("000");
            }
            else if (value.Year < 100)
            {
                sb.Append("00");
            }
            else if (value.Year < 1000)
            {
                sb.Append('0');
            }
            sb.Append(value.Year);

            sb.Append('-');
            Append2Chars(sb, value.Month);
            sb.Append('-');
            Append2Chars(sb, value.Day);
            sb.Append('T');
            Append2Chars(sb, value.Hour);
            sb.Append(':');
            Append2Chars(sb, value.Minute);
            sb.Append(':');
            Append2Chars(sb, value.Second);

            if (value.Millisecond > 0)
            {
                sb.Append('.');
                Append3Chars(sb, value.Millisecond);
            }

            if (value.Kind == DateTimeKind.Utc)
            {
                sb.Append('Z');
            }
            else if (value.Kind == DateTimeKind.Local)
            {
                TimeSpan offset = TimeZoneInfo.Local.GetUtcOffset(value);
                if (offset >= TimeSpan.Zero)
                {
                    sb.Append('+');
                }
                else
                {
                    sb.Append('-');
                    offset = offset.Negate();
                }
                Append2Chars(sb, offset.Hours);
                sb.Append(':');
                Append2Chars(sb, offset.Minutes);
            }
            writer.String(sb.ToString());
        }
コード例 #3
0
 protected override void InternalWrite(float value, IJsonConsumer writer)
 {
     if (float.IsNaN(value))
     {
         writer.String(DoubleConverter.NAN);
     }
     else if (float.IsPositiveInfinity(value))
     {
         writer.String(DoubleConverter.POS_INFINITY);
     }
     else if (float.IsNegativeInfinity(value))
     {
         writer.String(DoubleConverter.NEG_INFINITY);
     }
     else
     {
         writer.Number((double)value);
     }
 }
コード例 #4
0
 protected override void InternalWrite(double value, IJsonConsumer writer)
 {
     if (double.IsNaN(value))
     {
         writer.String(NAN);
     }
     else if (double.IsPositiveInfinity(value))
     {
         writer.String(POS_INFINITY);
     }
     else if (double.IsNegativeInfinity(value))
     {
         writer.String(NEG_INFINITY);
     }
     else
     {
         writer.Number(value);
     }
 }
コード例 #5
0
 public override void Write(object value, IJsonConsumer writer)
 {
     if (value == null)
     {
         writer.Null();
     }
     else
     {
         writer.String(System.Convert.ToBase64String((byte[])value, System.Base64FormattingOptions.None));
     }
 }
コード例 #6
0
 /// <summary>
 /// Writes the string or null value to the <see cref="IJsonConsumer"/>.
 /// </summary>
 /// <param name="value">The string value or null.</param>
 /// <param name="writer">The <see cref="IJsonConsumer"/></param>
 public override void Write(object value, IJsonConsumer writer)
 {
     if (value == null)
     {
         writer.Null();
     }
     else
     {
         writer.String((string)value);
     }
 }
コード例 #7
0
            public static void ParseValue(Tokenizer tokenizer, IJsonConsumer consumer)
            {
                if (tokenizer.CurrentToken.Type == Tokenizer.Token.None)
                {
                    throw new ParserException($"Unexpected end of stream reached in line {tokenizer.CurrentToken.LineNo} at position {tokenizer.CurrentToken.Position}.", tokenizer.CurrentToken.LineNo, tokenizer.CurrentToken.Position);
                }

                if (tokenizer.CurrentToken.Type == Tokenizer.Token.CurlyOpen)
                {
                    ParseObject(tokenizer, consumer.Object());
                    return;
                }
                if (tokenizer.CurrentToken.Type == Tokenizer.Token.SquaredOpen)
                {
                    ParseArray(tokenizer, consumer.Array());
                    return;
                }

                if (tokenizer.CurrentToken.Type == Tokenizer.Token.String)
                {
                    consumer.String(tokenizer.CurrentToken.StringValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.Boolean)
                {
                    consumer.Boolean(tokenizer.CurrentToken.BooleanValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.NumberInteger)
                {
                    consumer.Number(tokenizer.CurrentToken.IntegerValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.NumberUnsignedInteger)
                {
                    consumer.Number(tokenizer.CurrentToken.UnsignedIntegerValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.NumberFloat)
                {
                    consumer.Number(tokenizer.CurrentToken.FloatValue);
                }
                else if (tokenizer.CurrentToken.Type == Tokenizer.Token.Null)
                {
                    consumer.Null();
                }
                else
                {
                    throw new ParserException($"Expected value in line {tokenizer.CurrentToken.LineNo} at position {tokenizer.CurrentToken.Position}, but found '{tokenizer.CurrentToken}'.", tokenizer.CurrentToken.LineNo, tokenizer.CurrentToken.Position);
                }

                tokenizer.MoveNext(); // skip value literal
            }
コード例 #8
0
 public void Write(object value, IJsonConsumer writer)
 {
     writer.String(((int)value).ToString("X"));
 }
コード例 #9
0
ファイル: GuidConverter.cs プロジェクト: pupsette/CompactJson
 protected override void InternalWrite(Guid value, IJsonConsumer writer)
 {
     writer.String(value.ToString());
 }
コード例 #10
0
ファイル: JsonString.cs プロジェクト: pupsette/CompactJson
 /// <summary>
 /// Writes the string contents to the given <see cref="IJsonConsumer"/>.
 /// </summary>
 /// <param name="consumer">The consumer.</param>
 public override void Write(IJsonConsumer consumer)
 {
     consumer.String(Value);
 }
コード例 #11
0
 protected override void InternalWrite(char value, IJsonConsumer writer)
 {
     writer.String(new string(value, 1));
 }