private static JsonToken[] GetTokensWithReader(IJsonReader jsonReader) { List <JsonToken> tokens = new List <JsonToken>(); while (jsonReader.Read()) { JsonToken token; switch (jsonReader.CurrentTokenType) { case JsonTokenType.NotStarted: throw new InvalidOperationException(); case JsonTokenType.BeginArray: token = JsonToken.ArrayStart(); break; case JsonTokenType.EndArray: token = JsonToken.ArrayEnd(); break; case JsonTokenType.BeginObject: token = JsonToken.ObjectStart(); break; case JsonTokenType.EndObject: token = JsonToken.ObjectEnd(); break; case JsonTokenType.String: token = JsonToken.String(jsonReader.GetStringValue()); break; case JsonTokenType.Number: token = JsonToken.Number(jsonReader.GetNumberValue()); break; case JsonTokenType.True: token = JsonToken.Boolean(true); break; case JsonTokenType.False: token = JsonToken.Boolean(false); break; case JsonTokenType.Null: token = JsonToken.Null(); break; case JsonTokenType.FieldName: token = JsonToken.FieldName(jsonReader.GetStringValue()); break; case JsonTokenType.Int8: token = JsonToken.Int8(jsonReader.GetInt8Value()); break; case JsonTokenType.Int16: token = JsonToken.Int16(jsonReader.GetInt16Value()); break; case JsonTokenType.Int32: token = JsonToken.Int32(jsonReader.GetInt32Value()); break; case JsonTokenType.Int64: token = JsonToken.Int64(jsonReader.GetInt64Value()); break; case JsonTokenType.UInt32: token = JsonToken.UInt32(jsonReader.GetUInt32Value()); break; case JsonTokenType.Float32: token = JsonToken.Float32(jsonReader.GetFloat32Value()); break; case JsonTokenType.Float64: token = JsonToken.Float64(jsonReader.GetFloat64Value()); break; case JsonTokenType.Guid: token = JsonToken.Guid(jsonReader.GetGuidValue()); break; case JsonTokenType.Binary: token = JsonToken.Binary(jsonReader.GetBinaryValue()); break; default: throw new ArgumentException($"Unknown {nameof(JsonTokenType)}: {jsonReader.CurrentTokenType}"); } tokens.Add(token); } return(tokens.ToArray()); }
public static void DrainReader(IJsonReader jsonReader, bool materializeValue) { if (jsonReader == null) { throw new ArgumentNullException(nameof(jsonReader)); } while (jsonReader.Read()) { // Materialize the value switch (jsonReader.CurrentTokenType) { case JsonTokenType.BeginArray: case JsonTokenType.EndArray: case JsonTokenType.BeginObject: case JsonTokenType.EndObject: case JsonTokenType.Null: case JsonTokenType.True: case JsonTokenType.False: // Single byte tokens break; case JsonTokenType.String: case JsonTokenType.FieldName: if (materializeValue) { string stringValue = jsonReader.GetStringValue(); } break; case JsonTokenType.Number: if (materializeValue) { Number64 number64Value = jsonReader.GetNumberValue(); } break; case JsonTokenType.Int8: if (materializeValue) { sbyte int8Value = jsonReader.GetInt8Value(); } break; case JsonTokenType.Int16: if (materializeValue) { short int16Value = jsonReader.GetInt16Value(); } break; case JsonTokenType.Int32: if (materializeValue) { int int32Value = jsonReader.GetInt32Value(); } break; case JsonTokenType.Int64: if (materializeValue) { long int64Value = jsonReader.GetInt64Value(); } break; case JsonTokenType.UInt32: if (materializeValue) { uint uInt32Value = jsonReader.GetUInt32Value(); } break; case JsonTokenType.Float32: if (materializeValue) { float float32Value = jsonReader.GetFloat32Value(); } break; case JsonTokenType.Float64: if (materializeValue) { double doubleValue = jsonReader.GetFloat64Value(); } break; case JsonTokenType.Guid: if (materializeValue) { Guid guidValue = jsonReader.GetGuidValue(); } break; case JsonTokenType.Binary: if (materializeValue) { ReadOnlyMemory <byte> binaryValue = jsonReader.GetBinaryValue(); } break; default: throw new ArgumentException("$Unknown token type."); } } }
/// <summary> /// Writes current token from a json reader to the internal buffer. /// </summary> /// <param name="jsonReader">The JsonReader to the get the current token from.</param> public void WriteCurrentToken(IJsonReader jsonReader) { if (jsonReader == null) { throw new ArgumentNullException("jsonReader can not be null"); } // For now we don't optimize for text, since the reader could be UTF-8 and the writer could be UTF-16. // We need to add more enums for the different serialization formats. bool sameFormat = jsonReader.SerializationFormat == this.SerializationFormat && (this.SerializationFormat == JsonSerializationFormat.Binary || this.SerializationFormat == JsonSerializationFormat.HybridRow); JsonTokenType jsonTokenType = jsonReader.CurrentTokenType; switch (jsonTokenType) { case JsonTokenType.NotStarted: break; case JsonTokenType.BeginArray: this.WriteArrayStart(); break; case JsonTokenType.EndArray: this.WriteArrayEnd(); break; case JsonTokenType.BeginObject: this.WriteObjectStart(); break; case JsonTokenType.EndObject: this.WriteObjectEnd(); break; case JsonTokenType.True: this.WriteBoolValue(true); break; case JsonTokenType.False: this.WriteBoolValue(false); break; case JsonTokenType.Null: this.WriteNullValue(); break; default: { if (sameFormat) { ReadOnlySpan <byte> bufferedRawJson = jsonReader.GetBufferedRawJsonToken().Span; this.WriteRawJsonToken(jsonTokenType, bufferedRawJson); } else { switch (jsonTokenType) { case JsonTokenType.String: case JsonTokenType.FieldName: { string value = jsonReader.GetStringValue(); if (jsonTokenType == JsonTokenType.FieldName) { this.WriteFieldName(value); } else { this.WriteStringValue(value); } break; } case JsonTokenType.Number: { Number64 value = jsonReader.GetNumberValue(); this.WriteNumberValue(value); } break; case JsonTokenType.Int8: { sbyte value = jsonReader.GetInt8Value(); this.WriteInt8Value(value); } break; case JsonTokenType.Int16: { short value = jsonReader.GetInt16Value(); this.WriteInt16Value(value); } break; case JsonTokenType.Int32: { int value = jsonReader.GetInt32Value(); this.WriteInt32Value(value); } break; case JsonTokenType.Int64: { long value = jsonReader.GetInt64Value(); this.WriteInt64Value(value); } break; case JsonTokenType.UInt32: { uint value = jsonReader.GetUInt32Value(); this.WriteUInt32Value(value); } break; case JsonTokenType.Float32: { float value = jsonReader.GetFloat32Value(); this.WriteFloat32Value(value); } break; case JsonTokenType.Float64: { double value = jsonReader.GetFloat64Value(); this.WriteFloat64Value(value); } break; case JsonTokenType.Guid: { Guid value = jsonReader.GetGuidValue(); this.WriteGuidValue(value); } break; case JsonTokenType.Binary: { ReadOnlyMemory <byte> value = jsonReader.GetBinaryValue(); this.WriteBinaryValue(value.Span); } break; default: throw new ArgumentException($"Unexpected JsonTokenType: {jsonTokenType}"); } } } break; } }
public static TimeSpan MeasureReadPerformance(IJsonReader jsonReader, int numberOfIterations = 1) { Stopwatch stopwatch = new Stopwatch(); for (int i = 0; i < numberOfIterations; i++) { GC.Collect(); GC.WaitForPendingFinalizers(); stopwatch.Start(); while (jsonReader.Read()) { // Materialize the value switch (jsonReader.CurrentTokenType) { case JsonTokenType.BeginArray: case JsonTokenType.EndArray: case JsonTokenType.BeginObject: case JsonTokenType.EndObject: case JsonTokenType.Null: case JsonTokenType.True: case JsonTokenType.False: // Single byte tokens break; case JsonTokenType.String: case JsonTokenType.FieldName: string stringValue = jsonReader.GetStringValue(); break; case JsonTokenType.Number: Number64 number64Value = jsonReader.GetNumberValue(); break; case JsonTokenType.Int8: sbyte int8Value = jsonReader.GetInt8Value(); break; case JsonTokenType.Int16: short int16Value = jsonReader.GetInt16Value(); break; case JsonTokenType.Int32: int int32Value = jsonReader.GetInt32Value(); break; case JsonTokenType.Int64: long int64Value = jsonReader.GetInt64Value(); break; case JsonTokenType.UInt32: uint uInt32Value = jsonReader.GetUInt32Value(); break; case JsonTokenType.Float32: float float32Value = jsonReader.GetFloat32Value(); break; case JsonTokenType.Float64: double doubleValue = jsonReader.GetFloat64Value(); break; case JsonTokenType.Guid: Guid guidValue = jsonReader.GetGuidValue(); break; case JsonTokenType.Binary: ReadOnlyMemory <byte> binaryValue = jsonReader.GetBinaryValue(); break; default: throw new ArgumentException("$Unknown token type."); } } stopwatch.Stop(); } return(stopwatch.Elapsed); }
/// <summary> /// Writes current token from a json reader to the internal buffer. /// </summary> /// <param name="jsonReader">The JsonReader to the get the current token from.</param> public void WriteCurrentToken(IJsonReader jsonReader) { if (jsonReader == null) { throw new ArgumentNullException("jsonReader can not be null"); } bool sameFormat = jsonReader.SerializationFormat == this.SerializationFormat; JsonTokenType jsonTokenType = jsonReader.CurrentTokenType; switch (jsonTokenType) { case JsonTokenType.NotStarted: break; case JsonTokenType.BeginArray: this.WriteArrayStart(); break; case JsonTokenType.EndArray: this.WriteArrayEnd(); break; case JsonTokenType.BeginObject: this.WriteObjectStart(); break; case JsonTokenType.EndObject: this.WriteObjectEnd(); break; case JsonTokenType.True: this.WriteBoolValue(true); break; case JsonTokenType.False: this.WriteBoolValue(false); break; case JsonTokenType.Null: this.WriteNullValue(); break; default: { if (sameFormat && jsonReader.TryGetBufferedRawJsonToken(out ReadOnlyMemory <byte> bufferedRawJsonToken)) { this.WriteRawJsonToken(jsonTokenType, bufferedRawJsonToken.Span); } else { switch (jsonTokenType) { case JsonTokenType.String: case JsonTokenType.FieldName: { string value = jsonReader.GetStringValue(); if (jsonTokenType == JsonTokenType.FieldName) { this.WriteFieldName(value); } else { this.WriteStringValue(value); } break; } case JsonTokenType.Number: { Number64 value = jsonReader.GetNumberValue(); this.WriteNumber64Value(value); } break; case JsonTokenType.Int8: { sbyte value = jsonReader.GetInt8Value(); this.WriteInt8Value(value); } break; case JsonTokenType.Int16: { short value = jsonReader.GetInt16Value(); this.WriteInt16Value(value); } break; case JsonTokenType.Int32: { int value = jsonReader.GetInt32Value(); this.WriteInt32Value(value); } break; case JsonTokenType.Int64: { long value = jsonReader.GetInt64Value(); this.WriteInt64Value(value); } break; case JsonTokenType.UInt32: { uint value = jsonReader.GetUInt32Value(); this.WriteUInt32Value(value); } break; case JsonTokenType.Float32: { float value = jsonReader.GetFloat32Value(); this.WriteFloat32Value(value); } break; case JsonTokenType.Float64: { double value = jsonReader.GetFloat64Value(); this.WriteFloat64Value(value); } break; case JsonTokenType.Guid: { Guid value = jsonReader.GetGuidValue(); this.WriteGuidValue(value); } break; case JsonTokenType.Binary: { ReadOnlyMemory <byte> value = jsonReader.GetBinaryValue(); this.WriteBinaryValue(value.Span); } break; default: throw new ArgumentException($"Unexpected JsonTokenType: {jsonTokenType}"); } } } break; } }