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");
                }
            }
        }
        public override void WriteTo(IJsonWriter jsonWriter)
        {
            if (jsonWriter == null)
            {
                throw new ArgumentNullException($"{nameof(jsonWriter)}");
            }

            jsonWriter.WriteBoolValue(this.Value);
        }
                public override string GetContinuationToken()
                {
                    IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Text);

                    jsonWriter.WriteObjectStart();
                    jsonWriter.WriteFieldName(nameof(this.initialized));
                    jsonWriter.WriteBoolValue(this.initialized);
                    if (this.value != null)
                    {
                        jsonWriter.WriteFieldName(nameof(this.value));
                        this.value.WriteTo(jsonWriter);
                    }
                    jsonWriter.WriteObjectEnd();

                    string continuationToken = Utf8StringHelpers.ToString(jsonWriter.GetResult());

                    return(continuationToken);
                }
Beispiel #4
0
        public void BooleanTest()
        {
            IJsonWriter jsonWriter = JsonWriter.Create(JsonSerializationFormat.Binary);

            jsonWriter.WriteBoolValue(true);
            ReadOnlyMemory <byte> buffer = jsonWriter.GetResult();

            {
                // positive
                TryCatch <bool> tryDeserialize = JsonSerializer.Monadic.Deserialize <bool>(buffer);
                Assert.IsTrue(tryDeserialize.Succeeded);
                Assert.AreEqual(true, tryDeserialize.Result);
            }

            {
                // negative
                TryCatch <int> tryDeserialize = JsonSerializer.Monadic.Deserialize <int>(buffer);
                Assert.IsFalse(tryDeserialize.Succeeded);
            }
        }
 private static void VisitCosmosBoolean(CosmosBoolean cosmosBoolean, IJsonWriter jsonWriter)
 {
     jsonWriter.WriteBoolValue(cosmosBoolean.Value);
 }
Beispiel #6
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}.");
                }
            }
Beispiel #7
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);
        }
 public override void WriteTo(IJsonWriter jsonWriter) => jsonWriter.WriteBoolValue(this.Value);
Beispiel #9
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);
        }
Beispiel #10
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}.");
                }
            }
        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;
            }
        }