예제 #1
0
        public BenchmarkPayload(Action <IJsonWriter> writeToken)
        {
            if (writeToken == null)
            {
                throw new ArgumentNullException(nameof(writeToken));
            }

            IJsonWriter jsonTextWriter       = JsonWriter.Create(JsonSerializationFormat.Text);
            IJsonWriter jsonBinaryWriter     = JsonWriter.Create(JsonSerializationFormat.Binary);
            IJsonWriter jsonNewtonsoftWriter = NewtonsoftToCosmosDBWriter.CreateTextWriter();

            jsonTextWriter.WriteArrayStart();
            jsonBinaryWriter.WriteArrayStart();
            jsonNewtonsoftWriter.WriteArrayStart();

            for (int i = 0; i < 1000000; i++)
            {
                writeToken(jsonTextWriter);
                writeToken(jsonBinaryWriter);
                writeToken(jsonNewtonsoftWriter);
            }

            jsonTextWriter.WriteArrayEnd();
            jsonBinaryWriter.WriteArrayEnd();
            jsonNewtonsoftWriter.WriteArrayEnd();

            this.Text       = jsonTextWriter.GetResult();
            this.Binary     = jsonTextWriter.GetResult();
            this.Newtonsoft = jsonNewtonsoftWriter.GetResult();
        }
            public static Payload CreateStringLength(int length)
            {
                string      stringValue      = new string('a', length);
                IJsonWriter jsonTextWriter   = JsonWriter.Create(JsonSerializationFormat.Text);
                IJsonWriter jsonBinaryWriter = JsonWriter.Create(JsonSerializationFormat.Binary);

                jsonTextWriter.WriteArrayStart();
                jsonBinaryWriter.WriteArrayStart();

                for (int i = 0; i < 100000; i++)
                {
                    jsonTextWriter.WriteStringValue(stringValue);
                }

                jsonTextWriter.WriteArrayEnd();
                jsonBinaryWriter.WriteArrayEnd();

                ReadOnlyMemory <byte> text   = jsonTextWriter.GetResult();
                ReadOnlyMemory <byte> binary = jsonTextWriter.GetResult();

                return(new Payload(
                           text: text,
                           binary: binary,
                           utf8StringToken: Encoding.UTF8.GetBytes(stringValue),
                           utf16StringToken: stringValue));
            }
 private static void VisitCosmosArrayEnumerable(CosmosArray cosmosArray, IJsonWriter jsonWriter)
 {
     jsonWriter.WriteArrayStart();
     foreach (CosmosElement arrayItem in cosmosArray)
     {
         LazyCosmosElementTests.VisitCosmosElementIndexer(arrayItem, jsonWriter);
     }
     jsonWriter.WriteArrayEnd();
 }
예제 #4
0
        /// <summary>
        /// DEVNOTE: Need to refactor to use CosmosJsonSerializer
        /// Todo: This method can be optimized by not writing the result out to text.
        /// </summary>
        public static DocumentFeedResponse <T> ConvertCosmosElementFeed <T>(
            DocumentFeedResponse <CosmosElement> dynamicFeed,
            ResourceType resourceType,
            JsonSerializerSettings settings)
        {
            if (dynamicFeed.Count == 0)
            {
                return(new DocumentFeedResponse <T>(
                           new List <T>(),
                           dynamicFeed.Count,
                           dynamicFeed.Headers,
                           dynamicFeed.UseETagAsContinuation,
                           dynamicFeed.QueryMetrics,
                           dynamicFeed.RequestStatistics,
                           dynamicFeed.DisallowContinuationTokenMessage,
                           dynamicFeed.ResponseLengthBytes));
            }

            IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text);

            jsonWriter.WriteArrayStart();

            foreach (CosmosElement cosmosElement in dynamicFeed)
            {
                cosmosElement.WriteTo(jsonWriter);
            }

            jsonWriter.WriteArrayEnd();

            ReadOnlyMemory <byte> buffer = jsonWriter.GetResult();
            string jsonText = Utf8StringHelpers.ToString(buffer);

            IEnumerable <T> typedResults;

            // If the resource type is an offer and the requested type is either a Offer or OfferV2 or dynamic
            // create a OfferV2 object and cast it to T. This is a temporary fix until offers is moved to v3 API.
            if (resourceType == ResourceType.Offer &&
                (typeof(T).IsSubclassOf(typeof(Documents.Resource)) || typeof(T) == typeof(object)))
            {
                typedResults = JsonConvert.DeserializeObject <List <OfferV2> >(jsonText, settings).Cast <T>();
            }
            else
            {
                typedResults = JsonConvert.DeserializeObject <List <T> >(jsonText, settings);
            }

            return(new DocumentFeedResponse <T>(
                       typedResults,
                       dynamicFeed.Count,
                       dynamicFeed.Headers,
                       dynamicFeed.UseETagAsContinuation,
                       dynamicFeed.QueryMetrics,
                       dynamicFeed.RequestStatistics,
                       dynamicFeed.DisallowContinuationTokenMessage,
                       dynamicFeed.ResponseLengthBytes));
        }
 private static void VisitCosmosArrayIndexer(CosmosArray cosmosArray, IJsonWriter jsonWriter)
 {
     jsonWriter.WriteArrayStart();
     for (int i = 0; i < cosmosArray.Count; i++)
     {
         CosmosElement arrayItem = cosmosArray[i];
         LazyCosmosElementTests.VisitCosmosElementIndexer(arrayItem, jsonWriter);
     }
     jsonWriter.WriteArrayEnd();
 }
예제 #6
0
        public static void FlushToWriter(IJsonWriter jsonWriter, IReadOnlyList <JsonToken> tokensToWrite)
        {
            foreach (JsonToken token in tokensToWrite)
            {
                switch (token.JsonTokenType)
                {
                case JsonTokenType.BeginArray:
                    jsonWriter.WriteArrayStart();
                    break;

                case JsonTokenType.EndArray:
                    jsonWriter.WriteArrayEnd();
                    break;

                case JsonTokenType.BeginObject:
                    jsonWriter.WriteObjectStart();
                    break;

                case JsonTokenType.EndObject:
                    jsonWriter.WriteObjectEnd();
                    break;

                case JsonTokenType.String:
                    string stringValue = (token as JsonStringToken).Value;
                    jsonWriter.WriteStringValue(stringValue);
                    break;

                case JsonTokenType.Number:
                    Number64 numberValue = (token as JsonNumberToken).Value;
                    jsonWriter.WriteNumber64Value(numberValue);
                    break;

                case JsonTokenType.True:
                    jsonWriter.WriteBoolValue(true);
                    break;

                case JsonTokenType.False:
                    jsonWriter.WriteBoolValue(false);
                    break;

                case JsonTokenType.Null:
                    jsonWriter.WriteNullValue();
                    break;

                case JsonTokenType.FieldName:
                    string fieldNameValue = (token as JsonFieldNameToken).Value;
                    jsonWriter.WriteFieldName(fieldNameValue);
                    break;

                case JsonTokenType.NotStarted:
                default:
                    throw new ArgumentException("invalid jsontoken");
                }
            }
        }
예제 #7
0
        protected virtual void WriteList(Expression Expression)
        {
            ArrayExpression list = (ArrayExpression)Expression;

            jsonWriter.WriteArrayStart();
            foreach (Expression item in list.Items)
            {
                Write(item);
            }
            jsonWriter.WriteArrayEnd();
        }
예제 #8
0
            public override void WriteTo(IJsonWriter jsonWriter)
            {
                jsonWriter.WriteArrayStart();

                foreach (CosmosElement arrayItem in this)
                {
                    arrayItem.WriteTo(jsonWriter);
                }

                jsonWriter.WriteArrayEnd();
            }
예제 #9
0
        private void WriteList(Expression Expression)
        {
            ArrayExpression list = (ArrayExpression)Expression;

            _jsonWriter.WriteArrayStart();
            foreach (Expression item in list.Items)
            {
                Write(item);
            }
            _jsonWriter.WriteArrayEnd();
        }
        /// <summary>
        /// DEVNOTE: Need to refactor to use CosmosJsonSerializer
        /// </summary>
        public static FeedResponse <T> ConvertCosmosElementFeed <T>(
            FeedResponse <CosmosElement> dynamicFeed,
            ResourceType resourceType,
            CosmosJsonSerializer jsonSerializer)
        {
            if (dynamicFeed.Count == 0)
            {
                return(new FeedResponse <T>(
                           new List <T>(),
                           dynamicFeed.Count,
                           dynamicFeed.Headers,
                           dynamicFeed.UseETagAsContinuation,
                           dynamicFeed.QueryMetrics,
                           dynamicFeed.RequestStatistics,
                           dynamicFeed.DisallowContinuationTokenMessage,
                           dynamicFeed.ResponseLengthBytes));
            }

            IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text);

            jsonWriter.WriteArrayStart();

            foreach (CosmosElement cosmosElement in dynamicFeed)
            {
                cosmosElement.WriteTo(jsonWriter);
            }

            jsonWriter.WriteArrayEnd();
            MemoryStream    stream = new MemoryStream(jsonWriter.GetResult());
            IEnumerable <T> typedResults;

            // If the resource type is an offer and the requested type is either a Offer or OfferV2 or dynamic
            // create a OfferV2 object and cast it to T. This is a temporary fix until offers is moved to v3 API.
            if (resourceType == ResourceType.Offer &&
                (typeof(T).IsSubclassOf(typeof(Resource)) || typeof(T) == typeof(object)))
            {
                typedResults = jsonSerializer.FromStream <List <OfferV2> >(stream).Cast <T>();
            }
            else
            {
                typedResults = jsonSerializer.FromStream <List <T> >(stream);
            }

            return(new FeedResponse <T>(
                       typedResults,
                       dynamicFeed.Count,
                       dynamicFeed.Headers,
                       dynamicFeed.UseETagAsContinuation,
                       dynamicFeed.QueryMetrics,
                       dynamicFeed.RequestStatistics,
                       dynamicFeed.DisallowContinuationTokenMessage,
                       dynamicFeed.ResponseLengthBytes));
        }
예제 #11
0
            public override void WriteTo(IJsonWriter jsonWriter)
            {
                if (jsonWriter == null)
                {
                    throw new ArgumentNullException($"{nameof(jsonWriter)}");
                }

                jsonWriter.WriteArrayStart();

                foreach (CosmosElement arrayItem in this)
                {
                    arrayItem.WriteTo(jsonWriter);
                }

                jsonWriter.WriteArrayEnd();
            }
예제 #12
0
        private static void WriteTraceDatum(IJsonWriter writer, object value)
        {
            if (value is TraceDatum traceDatum)
            {
                TraceDatumJsonWriter traceJsonWriter = new TraceDatumJsonWriter(writer);
                traceDatum.Accept(traceJsonWriter);
            }
            else if (value is double doubleValue)
            {
                writer.WriteNumber64Value(doubleValue);
            }
            else if (value is long longValue)
            {
                writer.WriteNumber64Value(longValue);
            }
            else if (value is IEnumerable <object> enumerable)
            {
                writer.WriteArrayStart();

                foreach (object item in enumerable)
                {
                    WriteTraceDatum(writer, item);
                }

                writer.WriteArrayEnd();
            }
            else if (value is IDictionary <string, object> dictionary)
            {
                writer.WriteObjectStart();

                foreach (KeyValuePair <string, object> kvp in dictionary)
                {
                    writer.WriteFieldName(kvp.Key);
                    WriteTraceDatum(writer, kvp.Value);
                }

                writer.WriteObjectEnd();
            }
            else if (value is string stringValue)
            {
                writer.WriteStringValue(stringValue);
            }
            else
            {
                writer.WriteStringValue(value.ToString());
            }
        }
예제 #13
0
        public void ArrayTest()
        {
            {
                // Schemaless array
                IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Binary);
                object[]    arrayValue = new object[] { (Number64)1, (Number64)2, (Number64)3, (Number64)4 };
                jsonWriter.WriteArrayStart();
                jsonWriter.WriteNumber64Value(1);
                jsonWriter.WriteNumber64Value(2);
                jsonWriter.WriteNumber64Value(3);
                jsonWriter.WriteNumber64Value(4);
                jsonWriter.WriteArrayEnd();
                ReadOnlyMemory <byte> buffer = jsonWriter.GetResult();

                {
                    // positive
                    TryCatch <IReadOnlyList <object> > tryDeserialize = JsonSerializer.Monadic.Deserialize <IReadOnlyList <object> >(buffer);
                    Assert.IsTrue(tryDeserialize.Succeeded);
                    Assert.IsTrue(tryDeserialize.Result.SequenceEqual(arrayValue));
                }

                {
                    // negative
                    TryCatch <int> tryDeserialize = JsonSerializer.Monadic.Deserialize <int>(buffer);
                    Assert.IsFalse(tryDeserialize.Succeeded);
                }
            }

            {
                // Array with schema
                IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Binary);
                Person[]    arrayValue = new Person[] { new Person("John", 24) };
                jsonWriter.WriteArrayStart();
                jsonWriter.WriteObjectStart();
                jsonWriter.WriteFieldName("name");
                jsonWriter.WriteStringValue("John");
                jsonWriter.WriteFieldName("age");
                jsonWriter.WriteNumber64Value(24);
                jsonWriter.WriteObjectEnd();
                jsonWriter.WriteArrayEnd();
                ReadOnlyMemory <byte> buffer = jsonWriter.GetResult();

                TryCatch <IReadOnlyList <Person> > tryDeserialize = JsonSerializer.Monadic.Deserialize <IReadOnlyList <Person> >(buffer);
                Assert.IsTrue(tryDeserialize.Succeeded);
                Assert.IsTrue(tryDeserialize.Result.SequenceEqual(arrayValue));
            }
        }
        private static void RunWriteBenchmark(
            Payload payload,
            bool useUtf8)
        {
            IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Binary);

            jsonWriter.WriteArrayStart();

            for (int i = 0; i < 100000; i++)
            {
                if (useUtf8)
                {
                    jsonWriter.WriteStringValue(payload.Utf8StringToken.Span);
                }
                else
                {
                    jsonWriter.WriteStringValue(payload.Utf16StringToken);
                }
            }

            jsonWriter.WriteArrayEnd();
        }
예제 #15
0
        public void Write(IJsonWriter writer, ITypeWriterRegistry registry, object value)
        {
            var enumerable = value as IEnumerable;
            if (enumerable == null)
            {
                writer.WriteNull();
            }
            else
            {
                writer.WriteArrayStart();

                var index = 0;
                foreach (var item in enumerable)
                {
                    if (index++ > 0)
                    {
                        writer.WriteArrayElementDelimiter();
                    }
                    registry.Write(writer, item);
                }
                writer.WriteArrayEnd(index);
            }
        }
예제 #16
0
        public static TimeSpan MeasureWritePerformance(JsonToken[] tokensToWrite, IJsonWriter jsonWriter, int numberOfIterations = 1)
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();

            Stopwatch stopwatch = new Stopwatch();

            foreach (JsonToken token in tokensToWrite)
            {
                switch (token.JsonTokenType)
                {
                case JsonTokenType.BeginArray:
                    stopwatch.Start();
                    jsonWriter.WriteArrayStart();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.EndArray:
                    stopwatch.Start();
                    jsonWriter.WriteArrayEnd();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.BeginObject:
                    stopwatch.Start();
                    jsonWriter.WriteObjectStart();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.EndObject:
                    stopwatch.Start();
                    jsonWriter.WriteObjectEnd();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.String:
                    string stringValue = (token as JsonStringToken).Value;
                    stopwatch.Start();
                    jsonWriter.WriteStringValue(stringValue);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.Number:
                    Number64 numberValue = (token as JsonNumberToken).Value;
                    stopwatch.Start();
                    jsonWriter.WriteNumberValue(numberValue);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.True:
                    stopwatch.Start();
                    jsonWriter.WriteBoolValue(true);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.False:
                    stopwatch.Start();
                    jsonWriter.WriteBoolValue(false);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.Null:
                    stopwatch.Start();
                    jsonWriter.WriteNullValue();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.FieldName:
                    string fieldNameValue = (token as JsonFieldNameToken).Value;
                    stopwatch.Start();
                    jsonWriter.WriteFieldName(fieldNameValue);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.NotStarted:
                default:
                    throw new ArgumentException("invalid jsontoken");
                }
            }

            return(stopwatch.Elapsed);
        }
예제 #17
0
            private void WriteToInternal(BinaryNavigatorNode binaryNavigatorNode, IJsonWriter jsonWriter, bool sameEncoding)
            {
                ReadOnlyMemory <byte> buffer   = binaryNavigatorNode.Buffer;
                JsonNodeType          nodeType = binaryNavigatorNode.JsonNodeType;

                if (sameEncoding && this.TryGetBufferedRawJsonInternal(binaryNavigatorNode, out ReadOnlyMemory <byte> bufferedRawJson))
                {
                    // Token type doesn't make any difference other than whether it's a value or field name
                    JsonTokenType tokenType = nodeType == JsonNodeType.FieldName ? JsonTokenType.FieldName : JsonTokenType.String;
                    jsonWriter.WriteRawJsonToken(tokenType, bufferedRawJson.Span);
                    return;
                }

                switch (nodeType)
                {
                case JsonNodeType.Null:
                    jsonWriter.WriteNullValue();
                    break;

                case JsonNodeType.False:
                    jsonWriter.WriteBoolValue(false);
                    break;

                case JsonNodeType.True:
                    jsonWriter.WriteBoolValue(true);
                    break;

                case JsonNodeType.Number64:
                {
                    Number64 value = JsonBinaryEncoding.GetNumberValue(buffer.Span);
                    jsonWriter.WriteNumber64Value(value);
                }
                break;

                case JsonNodeType.String:
                case JsonNodeType.FieldName:
                    bool fieldName = binaryNavigatorNode.JsonNodeType == JsonNodeType.FieldName;

                    Utf8Memory utf8Buffer = Utf8Memory.UnsafeCreateNoValidation(buffer);
                    if (JsonBinaryEncoding.TryGetBufferedStringValue(
                            utf8Buffer,
                            this.jsonStringDictionary,
                            out Utf8Memory bufferedStringValue))
                    {
                        if (fieldName)
                        {
                            jsonWriter.WriteFieldName(bufferedStringValue.Span);
                        }
                        else
                        {
                            jsonWriter.WriteStringValue(bufferedStringValue.Span);
                        }
                    }
                    else
                    {
                        string value = JsonBinaryEncoding.GetStringValue(
                            utf8Buffer,
                            this.jsonStringDictionary);
                        if (fieldName)
                        {
                            jsonWriter.WriteFieldName(value);
                        }
                        else
                        {
                            jsonWriter.WriteStringValue(value);
                        }
                    }
                    break;

                case JsonNodeType.Array:
                {
                    jsonWriter.WriteArrayStart();

                    foreach (BinaryNavigatorNode arrayItem in this.GetArrayItemsInternal(buffer))
                    {
                        this.WriteToInternal(arrayItem, jsonWriter, sameEncoding);
                    }

                    jsonWriter.WriteArrayEnd();
                }
                break;

                case JsonNodeType.Object:
                {
                    jsonWriter.WriteObjectStart();

                    foreach (ObjectPropertyInternal objectProperty in this.GetObjectPropertiesInternal(buffer))
                    {
                        this.WriteToInternal(objectProperty.NameNode, jsonWriter, sameEncoding);
                        this.WriteToInternal(objectProperty.ValueNode, jsonWriter, sameEncoding);
                    }

                    jsonWriter.WriteObjectEnd();
                }
                break;

                case JsonNodeType.Int8:
                {
                    sbyte value = JsonBinaryEncoding.GetInt8Value(buffer.Span);
                    jsonWriter.WriteInt8Value(value);
                }
                break;

                case JsonNodeType.Int16:
                {
                    short value = JsonBinaryEncoding.GetInt16Value(buffer.Span);
                    jsonWriter.WriteInt16Value(value);
                }
                break;

                case JsonNodeType.Int32:
                {
                    int value = JsonBinaryEncoding.GetInt32Value(buffer.Span);
                    jsonWriter.WriteInt32Value(value);
                }
                break;

                case JsonNodeType.Int64:
                {
                    long value = JsonBinaryEncoding.GetInt64Value(buffer.Span);
                    jsonWriter.WriteInt64Value(value);
                }
                break;

                case JsonNodeType.UInt32:
                {
                    uint value = JsonBinaryEncoding.GetUInt32Value(buffer.Span);
                    jsonWriter.WriteUInt32Value(value);
                }
                break;

                case JsonNodeType.Float32:
                {
                    float value = JsonBinaryEncoding.GetFloat32Value(buffer.Span);
                    jsonWriter.WriteFloat32Value(value);
                }
                break;

                case JsonNodeType.Float64:
                {
                    double value = JsonBinaryEncoding.GetFloat64Value(buffer.Span);
                    jsonWriter.WriteFloat64Value(value);
                }
                break;

                case JsonNodeType.Binary:
                {
                    ReadOnlyMemory <byte> value = JsonBinaryEncoding.GetBinaryValue(buffer);
                    jsonWriter.WriteBinaryValue(value.Span);
                }
                break;

                case JsonNodeType.Guid:
                {
                    Guid value = JsonBinaryEncoding.GetGuidValue(buffer.Span);
                    jsonWriter.WriteGuidValue(value);
                }
                break;

                default:
                    throw new ArgumentOutOfRangeException($"Unknown {nameof(JsonNodeType)}: {nodeType}.");
                }
            }
예제 #18
0
            public static void WriteTrace(
                IJsonWriter writer,
                ITrace trace)
            {
                if (writer == null)
                {
                    throw new ArgumentNullException(nameof(writer));
                }

                if (trace == null)
                {
                    throw new ArgumentNullException(nameof(trace));
                }

                writer.WriteObjectStart();

                writer.WriteFieldName("name");
                writer.WriteStringValue(trace.Name);

                writer.WriteFieldName("id");
                writer.WriteStringValue(trace.Id.ToString());

                // Request handler use the base class to create the trace.
                // This makes it pointless to log the caller info because
                // it is always just the base class info.
                if (trace.Component != TraceComponent.RequestHandler)
                {
                    writer.WriteFieldName("caller info");
                    writer.WriteObjectStart();

                    writer.WriteFieldName("member");
                    writer.WriteStringValue(trace.CallerInfo.MemberName);

                    writer.WriteFieldName("file");
                    writer.WriteStringValue(GetFileNameFromPath(trace.CallerInfo.FilePath));

                    writer.WriteFieldName("line");
                    writer.WriteNumber64Value(trace.CallerInfo.LineNumber);

                    writer.WriteObjectEnd();
                }

                writer.WriteFieldName("start time");
                writer.WriteStringValue(trace.StartTime.ToString("hh:mm:ss:fff"));

                writer.WriteFieldName("duration in milliseconds");
                writer.WriteNumber64Value(trace.Duration.TotalMilliseconds);

                if (trace.Data.Any())
                {
                    writer.WriteFieldName("data");
                    writer.WriteObjectStart();

                    foreach (KeyValuePair <string, object> kvp in trace.Data)
                    {
                        string key   = kvp.Key;
                        object value = kvp.Value;

                        writer.WriteFieldName(key);
                        WriteTraceDatum(writer, value);
                    }

                    writer.WriteObjectEnd();
                }

                if (trace.Children.Any())
                {
                    writer.WriteFieldName("children");
                    writer.WriteArrayStart();

                    foreach (ITrace child in trace.Children)
                    {
                        WriteTrace(writer, child);
                    }

                    writer.WriteArrayEnd();
                }
                writer.WriteObjectEnd();
            }
예제 #19
0
            public static void WriteTrace(
                IJsonWriter writer,
                ITrace trace)
            {
                if (writer == null)
                {
                    throw new ArgumentNullException(nameof(writer));
                }

                if (trace == null)
                {
                    throw new ArgumentNullException(nameof(trace));
                }

                writer.WriteObjectStart();

                writer.WriteFieldName("name");
                writer.WriteStringValue(trace.Name);

                writer.WriteFieldName("id");
                writer.WriteStringValue(trace.Id.ToString());

                writer.WriteFieldName("component");
                writer.WriteStringValue(trace.Component.ToString());

                writer.WriteFieldName("caller information");
                writer.WriteObjectStart();

                writer.WriteFieldName("member name");
                writer.WriteStringValue(trace.CallerInfo.MemberName);

                writer.WriteFieldName("file path");
                writer.WriteStringValue(trace.CallerInfo.FilePath);

                writer.WriteFieldName("line number");
                writer.WriteNumber64Value(trace.CallerInfo.LineNumber);

                writer.WriteObjectEnd();

                writer.WriteFieldName("start time");
                writer.WriteStringValue(trace.StartTime.ToString("hh:mm:ss:fff"));

                writer.WriteFieldName("duration in milliseconds");
                writer.WriteNumber64Value(trace.Duration.TotalMilliseconds);

                writer.WriteFieldName("data");
                writer.WriteObjectStart();

                foreach (KeyValuePair <string, object> kvp in trace.Data)
                {
                    string key   = kvp.Key;
                    object value = kvp.Value;

                    writer.WriteFieldName(key);
                    WriteTraceDatum(writer, value);
                }

                writer.WriteObjectEnd();

                writer.WriteFieldName("children");
                writer.WriteArrayStart();

                foreach (ITrace child in trace.Children)
                {
                    WriteTrace(writer, child);
                }

                writer.WriteArrayEnd();

                writer.WriteObjectEnd();
            }
예제 #20
0
            public override string GetContinuationToken()
            {
                IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Binary);

                jsonWriter.WriteObjectStart();

                jsonWriter.WriteFieldName(UnorderdDistinctMap.NumbersName);
                jsonWriter.WriteArrayStart();
                foreach (Number64 number in this.numbers)
                {
                    jsonWriter.WriteNumberValue(number);
                }
                jsonWriter.WriteArrayEnd();

                jsonWriter.WriteFieldName(UnorderdDistinctMap.StringsLength4Name);
                jsonWriter.WriteArrayStart();
                foreach (uint stringLength4 in this.stringsLength4)
                {
                    jsonWriter.WriteUInt32Value(stringLength4);
                }
                jsonWriter.WriteArrayEnd();

                jsonWriter.WriteFieldName(UnorderdDistinctMap.StringsLength8Name);
                jsonWriter.WriteArrayStart();
                foreach (ulong stringLength8 in this.stringsLength8)
                {
                    jsonWriter.WriteInt64Value((long)stringLength8);
                }
                jsonWriter.WriteArrayEnd();

                jsonWriter.WriteFieldName(UnorderdDistinctMap.StringsLength16Name);
                jsonWriter.WriteArrayStart();
                foreach (UInt128 stringLength16 in this.stringsLength16)
                {
                    jsonWriter.WriteBinaryValue(UInt128.ToByteArray(stringLength16));
                }
                jsonWriter.WriteArrayEnd();

                jsonWriter.WriteFieldName(UnorderdDistinctMap.StringsLength16PlusName);
                jsonWriter.WriteArrayStart();
                foreach (UInt128 stringLength16Plus in this.stringsLength16Plus)
                {
                    jsonWriter.WriteBinaryValue(UInt128.ToByteArray(stringLength16Plus));
                }
                jsonWriter.WriteArrayEnd();

                jsonWriter.WriteFieldName(UnorderdDistinctMap.ArraysName);
                jsonWriter.WriteArrayStart();
                foreach (UInt128 array in this.arrays)
                {
                    jsonWriter.WriteBinaryValue(UInt128.ToByteArray(array));
                }
                jsonWriter.WriteArrayEnd();

                jsonWriter.WriteFieldName(UnorderdDistinctMap.ObjectName);
                jsonWriter.WriteArrayStart();
                foreach (UInt128 objectHash in this.objects)
                {
                    jsonWriter.WriteBinaryValue(UInt128.ToByteArray(objectHash));
                }
                jsonWriter.WriteArrayEnd();

                jsonWriter.WriteFieldName(UnorderdDistinctMap.SimpleValuesName);
                jsonWriter.WriteStringValue(this.simpleValues.ToString());

                jsonWriter.WriteObjectEnd();

                ReadOnlyMemory <byte> memory = jsonWriter.GetResult();

                if (!MemoryMarshal.TryGetArray(memory, out ArraySegment <byte> buffer))
                {
                    buffer = new ArraySegment <byte>(memory.ToArray());
                }

                return(Convert.ToBase64String(buffer.Array, buffer.Offset, buffer.Count));
            }
예제 #21
0
            private void WriteToInternal(BinaryNavigatorNode binaryNavigatorNode, IJsonWriter jsonWriter)
            {
                ReadOnlyMemory <byte> buffer   = binaryNavigatorNode.Buffer;
                JsonNodeType          nodeType = binaryNavigatorNode.JsonNodeType;

                switch (nodeType)
                {
                case JsonNodeType.Null:
                    jsonWriter.WriteNullValue();
                    break;

                case JsonNodeType.False:
                    jsonWriter.WriteBoolValue(false);
                    break;

                case JsonNodeType.True:
                    jsonWriter.WriteBoolValue(true);
                    break;

                case JsonNodeType.Number64:
                {
                    Number64 value = JsonBinaryEncoding.GetNumberValue(buffer.Span);
                    jsonWriter.WriteNumber64Value(value);
                }
                break;

                case JsonNodeType.String:
                case JsonNodeType.FieldName:
                    bool fieldName = binaryNavigatorNode.JsonNodeType == JsonNodeType.FieldName;

                    if (JsonBinaryEncoding.TryGetBufferedStringValue(
                            this.rootBuffer,
                            buffer,
                            out Utf8Memory bufferedStringValue))
                    {
                        if (fieldName)
                        {
                            jsonWriter.WriteFieldName(bufferedStringValue.Span);
                        }
                        else
                        {
                            jsonWriter.WriteStringValue(bufferedStringValue.Span);
                        }
                    }
                    else
                    {
                        string value = JsonBinaryEncoding.GetStringValue(
                            this.rootBuffer,
                            buffer);
                        if (fieldName)
                        {
                            jsonWriter.WriteFieldName(value);
                        }
                        else
                        {
                            jsonWriter.WriteStringValue(value);
                        }
                    }
                    break;

                case JsonNodeType.Array:
                {
                    jsonWriter.WriteArrayStart();

                    foreach (BinaryNavigatorNode arrayItem in this.GetArrayItemsInternal(buffer))
                    {
                        this.WriteToInternal(arrayItem, jsonWriter);
                    }

                    jsonWriter.WriteArrayEnd();
                }
                break;

                case JsonNodeType.Object:
                {
                    jsonWriter.WriteObjectStart();

                    foreach (ObjectPropertyInternal objectProperty in this.GetObjectPropertiesInternal(buffer))
                    {
                        this.WriteToInternal(objectProperty.NameNode, jsonWriter);
                        this.WriteToInternal(objectProperty.ValueNode, jsonWriter);
                    }

                    jsonWriter.WriteObjectEnd();
                }
                break;

                case JsonNodeType.Int8:
                {
                    sbyte value = JsonBinaryEncoding.GetInt8Value(buffer.Span);
                    jsonWriter.WriteInt8Value(value);
                }
                break;

                case JsonNodeType.Int16:
                {
                    short value = JsonBinaryEncoding.GetInt16Value(buffer.Span);
                    jsonWriter.WriteInt16Value(value);
                }
                break;

                case JsonNodeType.Int32:
                {
                    int value = JsonBinaryEncoding.GetInt32Value(buffer.Span);
                    jsonWriter.WriteInt32Value(value);
                }
                break;

                case JsonNodeType.Int64:
                {
                    long value = JsonBinaryEncoding.GetInt64Value(buffer.Span);
                    jsonWriter.WriteInt64Value(value);
                }
                break;

                case JsonNodeType.UInt32:
                {
                    uint value = JsonBinaryEncoding.GetUInt32Value(buffer.Span);
                    jsonWriter.WriteUInt32Value(value);
                }
                break;

                case JsonNodeType.Float32:
                {
                    float value = JsonBinaryEncoding.GetFloat32Value(buffer.Span);
                    jsonWriter.WriteFloat32Value(value);
                }
                break;

                case JsonNodeType.Float64:
                {
                    double value = JsonBinaryEncoding.GetFloat64Value(buffer.Span);
                    jsonWriter.WriteFloat64Value(value);
                }
                break;

                case JsonNodeType.Binary:
                {
                    ReadOnlyMemory <byte> value = JsonBinaryEncoding.GetBinaryValue(buffer);
                    jsonWriter.WriteBinaryValue(value.Span);
                }
                break;

                case JsonNodeType.Guid:
                {
                    Guid value = JsonBinaryEncoding.GetGuidValue(buffer.Span);
                    jsonWriter.WriteGuidValue(value);
                }
                break;

                default:
                    throw new ArgumentOutOfRangeException($"Unknown {nameof(JsonNodeType)}: {nodeType}.");
                }
            }
        private static void PerformRoundTrip(
            SerializationFormat sourceFormat,
            SerializationFormat destinationFormat,
            string json,
            bool writeAsRootNode)
        {
            IJsonReader reader = sourceFormat switch
            {
                SerializationFormat.Text => JsonReader.Create(Encoding.UTF8.GetBytes(json)),
                SerializationFormat.Binary => JsonReader.Create(JsonTestUtils.ConvertTextToBinary(json)),
                SerializationFormat.NewtonsoftText => NewtonsoftToCosmosDBReader.CreateFromString(json),
                _ => throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"),
            };

            IJsonNavigator navigator = sourceFormat switch
            {
                SerializationFormat.Text => JsonNavigator.Create(Encoding.UTF8.GetBytes(json)),
                SerializationFormat.Binary => JsonNavigator.Create(JsonTestUtils.ConvertTextToBinary(json)),
                SerializationFormat.NewtonsoftText => new JsonNewtonsoftNavigator(json),
                _ => throw new ArgumentException($"Unexpected {nameof(sourceFormat)} of type: {sourceFormat}"),
            };

            foreach (object source in new object[] { reader, navigator })
            {
                IJsonWriter writer = destinationFormat switch
                {
                    SerializationFormat.Text => JsonWriter.Create(JsonSerializationFormat.Text),
                    SerializationFormat.Binary => JsonWriter.Create(JsonSerializationFormat.Binary),
                    SerializationFormat.NewtonsoftText => NewtonsoftToCosmosDBWriter.CreateTextWriter(),
                    _ => throw new ArgumentException($"Unexpected {nameof(destinationFormat)} of type: {destinationFormat}"),
                };

                switch (source)
                {
                case IJsonReader sourceReader:
                    sourceReader.WriteAll(writer);
                    break;

                case IJsonNavigator sourceNavigator:
                    if (writeAsRootNode)
                    {
                        sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer);
                    }
                    else
                    {
                        IJsonNavigatorNode rootNode     = sourceNavigator.GetRootNode();
                        JsonNodeType       jsonNodeType = sourceNavigator.GetNodeType(rootNode);
                        switch (jsonNodeType)
                        {
                        case JsonNodeType.Array:
                            writer.WriteArrayStart();

                            foreach (IJsonNavigatorNode arrayItem in sourceNavigator.GetArrayItems(rootNode))
                            {
                                sourceNavigator.WriteNode(arrayItem, writer);
                            }

                            writer.WriteArrayEnd();
                            break;

                        case JsonNodeType.Object:
                            writer.WriteObjectStart();

                            foreach (ObjectProperty objectProperty in sourceNavigator.GetObjectProperties(rootNode))
                            {
                                sourceNavigator.WriteNode(objectProperty.NameNode, writer);
                                sourceNavigator.WriteNode(objectProperty.ValueNode, writer);
                            }

                            writer.WriteObjectEnd();
                            break;

                        default:
                            sourceNavigator.WriteNode(sourceNavigator.GetRootNode(), writer);
                            break;
                        }
                    }
                    break;

                default:
                    Assert.Fail("Failed to downcast source type.");
                    break;
                }

                string result = writer.SerializationFormat switch
                {
                    JsonSerializationFormat.Text => Utf8String.UnsafeFromUtf8BytesNoValidation(writer.GetResult()).ToString(),
                    JsonSerializationFormat.Binary => JsonTestUtils.ConvertBinaryToText(writer.GetResult()),
                    _ => throw new ArgumentException(),
                };
                string normalizedResult = JsonRoundTripsTests.NewtonsoftFormat(result);
                string normalizedJson   = JsonRoundTripsTests.NewtonsoftFormat(json);

                Assert.AreEqual(normalizedJson, normalizedResult);
            }
        }
예제 #23
0
        public static TimeSpan MeasureWritePerformance(JsonTokenInfo[] tokensToWrite, IJsonWriter jsonWriter, int numberOfIterations = 1)
        {
            Stopwatch stopwatch = new Stopwatch();

            foreach (JsonTokenInfo token in tokensToWrite)
            {
                switch (token.JsonTokenType)
                {
                case JsonTokenType.BeginArray:
                    stopwatch.Start();
                    jsonWriter.WriteArrayStart();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.EndArray:
                    stopwatch.Start();
                    jsonWriter.WriteArrayEnd();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.BeginObject:
                    stopwatch.Start();
                    jsonWriter.WriteObjectStart();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.EndObject:
                    stopwatch.Start();
                    jsonWriter.WriteObjectEnd();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.String:
                    string stringWithQuotes = Encoding.Unicode.GetString(token.BufferedToken.ToArray());
                    string value            = stringWithQuotes.Substring(1, stringWithQuotes.Length - 2);
                    stopwatch.Start();
                    jsonWriter.WriteStringValue(value);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.Number:
                    stopwatch.Start();
                    jsonWriter.WriteNumberValue(token.Value);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.True:
                    stopwatch.Start();
                    jsonWriter.WriteBoolValue(true);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.False:
                    stopwatch.Start();
                    jsonWriter.WriteBoolValue(false);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.Null:
                    stopwatch.Start();
                    jsonWriter.WriteNullValue();
                    stopwatch.Stop();
                    break;

                case JsonTokenType.FieldName:
                    string fieldNameWithQuotes = Encoding.Unicode.GetString(token.BufferedToken.ToArray());
                    string fieldName           = fieldNameWithQuotes.Substring(1, fieldNameWithQuotes.Length - 2);
                    stopwatch.Start();
                    jsonWriter.WriteFieldName(fieldName);
                    stopwatch.Stop();
                    break;

                case JsonTokenType.NotStarted:
                default:
                    throw new ArgumentException("invalid jsontoken");
                }
            }

            return(stopwatch.Elapsed);
        }
        public static void SerializeInternal(
            object value,
            IJsonWriter jsonWriter)
        {
            if (jsonWriter == null)
            {
                throw new ArgumentNullException(nameof(jsonWriter));
            }

            switch (value)
            {
            case null:
                jsonWriter.WriteNullValue();
                break;

            case bool boolValue:
                jsonWriter.WriteBoolValue(boolValue);
                break;

            case string stringValue:
                jsonWriter.WriteStringValue(stringValue);
                break;

            case Number64 numberValue:
                jsonWriter.WriteNumber64Value(numberValue);
                break;

            case sbyte signedByteValue:
                jsonWriter.WriteInt8Value(signedByteValue);
                break;

            case short shortValue:
                jsonWriter.WriteInt16Value(shortValue);
                break;

            case int intValue:
                jsonWriter.WriteInt32Value(intValue);
                break;

            case long longValue:
                jsonWriter.WriteInt64Value(longValue);
                break;

            case uint uintValue:
                jsonWriter.WriteUInt32Value(uintValue);
                break;

            case float floatValue:
                jsonWriter.WriteFloat32Value(floatValue);
                break;

            case double doubleValue:
                jsonWriter.WriteFloat64Value(doubleValue);
                break;

            case ReadOnlyMemory <byte> binaryValue:
                jsonWriter.WriteBinaryValue(binaryValue.Span);
                break;

            case Guid guidValue:
                jsonWriter.WriteGuidValue(guidValue);
                break;

            case IEnumerable enumerableValue:
                jsonWriter.WriteArrayStart();

                foreach (object arrayItem in enumerableValue)
                {
                    JsonSerializer.SerializeInternal(arrayItem, jsonWriter);
                }

                jsonWriter.WriteArrayEnd();
                break;

            case CosmosElement cosmosElementValue:
                cosmosElementValue.WriteTo(jsonWriter);
                break;

            case ValueType valueType:
                throw new ArgumentOutOfRangeException($"Unable to serialize type: {valueType.GetType()}");

            default:
                Type           type       = value.GetType();
                PropertyInfo[] properties = type.GetProperties();

                jsonWriter.WriteObjectStart();

                foreach (PropertyInfo propertyInfo in properties)
                {
                    jsonWriter.WriteFieldName(propertyInfo.Name);
                    object propertyValue = propertyInfo.GetValue(value);
                    JsonSerializer.SerializeInternal(propertyValue, jsonWriter);
                }

                jsonWriter.WriteObjectEnd();
                break;
            }
        }
예제 #25
0
            public static void WriteTrace(
                IJsonWriter writer,
                ITrace trace,
                bool isRootTrace)
            {
                if (writer == null)
                {
                    throw new ArgumentNullException(nameof(writer));
                }

                if (trace == null)
                {
                    throw new ArgumentNullException(nameof(trace));
                }

                writer.WriteObjectStart();

                if (isRootTrace)
                {
                    writer.WriteFieldName("Summary");
                    SummaryDiagnostics summaryDiagnostics = new SummaryDiagnostics(trace);
                    summaryDiagnostics.WriteSummaryDiagnostics(writer);
                }

                writer.WriteFieldName("name");
                writer.WriteStringValue(trace.Name);

                writer.WriteFieldName("id");
                writer.WriteStringValue(trace.Id.ToString());

                writer.WriteFieldName("start time");
                writer.WriteStringValue(trace.StartTime.ToString("hh:mm:ss:fff"));

                writer.WriteFieldName("duration in milliseconds");
                writer.WriteNumber64Value(trace.Duration.TotalMilliseconds);

                if (trace.Data.Any())
                {
                    writer.WriteFieldName("data");
                    writer.WriteObjectStart();

                    foreach (KeyValuePair <string, object> kvp in trace.Data)
                    {
                        string key   = kvp.Key;
                        object value = kvp.Value;

                        writer.WriteFieldName(key);
                        WriteTraceDatum(writer, value);
                    }

                    writer.WriteObjectEnd();
                }

                if (trace.Children.Any())
                {
                    writer.WriteFieldName("children");
                    writer.WriteArrayStart();

                    foreach (ITrace child in trace.Children)
                    {
                        WriteTrace(writer,
                                   child,
                                   isRootTrace: false);
                    }

                    writer.WriteArrayEnd();
                }
                writer.WriteObjectEnd();
            }