Beispiel #1
0
 public AsyncBlittableJsonTextWriter(JsonOperationContext context, Stream stream, CancellationToken cancellationToken) : base(context, context.CheckoutMemoryStream())
 {
     _outputStream      = stream;
     _cancellationToken = cancellationToken;
 }
        private void WriteParameterValue(object value, JsonSerializer serializer)
        {
            switch (value)
            {
            case string s:
                Write(s);
                break;

            case char ch:
                Write(ch);
                break;

            case long l:
                Write(l);
                break;

            case ulong ul:
                Write(ul);
                break;

            case int i:
                Write(i);
                break;

            case uint ui:
                Write(ui);
                break;

            case short sh:
                Write(sh);
                break;

            case ushort ush:
                Write(ush);
                break;

            case bool b:
                Write(b);
                break;

            case double d:
                Write(d.ToString("R"));
                break;

            case float f:
                Write(f.ToString("R"));
                break;

            case decimal dec:
                Write(dec.ToString("G"));
                break;

            case null:
                _buffer.WriteByte(0);
                break;

            case DateTime dt:
                Write(dt.ToString("o"));
                break;

            case DateTimeOffset dto:
                Write(dto.ToString("o"));
                break;

            case TimeSpan ts:
                Write(ts.ToString("c"));
                break;

            case Guid guid:
                Write(guid.ToString());
                break;

            case byte bt:
                _buffer.WriteByte(bt);
                break;

            case sbyte sbt:
                Write(sbt);
                break;

            case Enum enm:
                Write(enm.ToString());
                break;

            case Type t:
                Write(t.AssemblyQualifiedName);
                break;

            case IDictionary dict:
                bool hadDictionaryValues  = false;
                var  dictionaryEnumerator = dict.GetEnumerator();
                while (dictionaryEnumerator.MoveNext())
                {
                    WriteParameterValue(dictionaryEnumerator.Key, serializer);
                    WriteParameterValue(dictionaryEnumerator.Value, serializer);
                    hadDictionaryValues = true;
                }
                if (hadDictionaryValues == false)
                {
                    Write("empty-dictionary");
                }
                break;

            case IEnumerable e:
                bool hadEnumerableValues = false;
                var  enumerator          = e.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    WriteParameterValue(enumerator.Current, serializer);
                    hadEnumerableValues = true;
                }
                if (hadEnumerableValues == false)
                {
                    Write("empty-enumerator");
                }

                break;

            default:

                var valueType = value.GetType();
                if (valueType.IsPrimitive == false)
                {
                    var stream = _context.CheckoutMemoryStream();
                    stream.Position = 0;
                    try
                    {
                        using (var writer = new StreamWriter(stream, Encoding.UTF8, bufferSize: 1024, leaveOpen: true))
                        {
                            serializer.Serialize(writer, value);
                        }

                        stream.Position = 0;

                        Write(stream);
                    }
                    finally
                    {
                        _context.ReturnMemoryStream(stream);
                    }
                }
                else
                {
                    Write(value.ToString());
                }

                break;
            }
        }