public void Present(Span <byte> output, VariantValue value, ValuePresenterLimits limits, out int byteCount)
        {
            switch (value.Kind)
            {
            case VariantKind.Int32:
                AppendNumber(output, value.AsInt32Unchecked(), out byteCount);
                return;

            case VariantKind.Int64:
                Utf8Formatter.TryFormat(value.AsInt64Unchecked(), output, out byteCount);
                return;

            case VariantKind.Object:
                AppendValue(output, value.AsObjectUnchecked(), depth: 1, limits, out byteCount);
                return;

            default:
                throw new NotSupportedException($"Unsupported variant kind: {value.Kind}");
            }
        }
Exemple #2
0
        // Must be thread safe
        public void WriteValue <T>(T value, string?name, int lineNumber)
        {
            if (_valueCountsPerLine[lineNumber] > Limits.MaxValuesPerLine)
            {
                return;
            }

            var valueCountPerLine = Interlocked.Increment(ref _valueCountsPerLine[lineNumber]);

            if (valueCountPerLine > Limits.MaxValuesPerLine)
            {
                if (valueCountPerLine == Limits.MaxValuesPerLine + 1)
                {
                    TryAddRecord(new(lineNumber, null, VariantValue.From("…")));
                }
                return;
            }

            TryAddRecord(new (lineNumber, name, VariantValue.From(value)));
        }
Exemple #3
0
        private void WriteOtherValueToWriter(Utf8JsonWriter writer, FlowRecord record, VariantValue value)
        {
            var utf8Length = _valuePresenter.GetMaxOutputByteCount(Limits.Value);

            if (utf8Length > 100)   // just in case, don't want a StackOverflow if here is a bug somewhere
            {
                writer.WriteStringValue("(#error#)");
                return;
            }

            var utf8Bytes = (Span <byte>) stackalloc byte[utf8Length];

            _valuePresenter.Present(utf8Bytes, value, Limits.Value, out var byteCount);
            writer.WriteStringValue(utf8Bytes.Slice(0, byteCount));
        }
Exemple #4
0
        // Does NOT have to be thread-safe
        private void WriteRecordWithValueToWriter(Utf8JsonWriter writer, FlowRecord record, VariantValue value)
        {
            writer.WriteStartArray();
            writer.WriteNumberValue(record.LineNumber);
            switch (value.Kind)
            {
            case VariantKind.Int32:
                writer.WriteNumberValue(value.AsInt32Unchecked());
                break;

            default:
                WriteOtherValueToWriter(writer, record, value);
                break;
            }
            if (record.Name != null)
            {
                WriteNameToWriter(writer, record.Name);
            }
            writer.WriteEndArray();
        }