case JsonConstants.Plus:
case JsonConstants.Hyphen:
parseData.OffsetToken = curByte;
- return ParseOffset(ref parseData, source.Slice(sourceIndex))
- && true;
+ return ParseOffset(ref parseData, source.Slice(sourceIndex));
default:
return false;
}
private const string KeyName = "Key";
private const string ValueName = "Value";
- // todo: move these to JsonSerializerOptions and use the proper encoding.
+ // todo: https://github.com/dotnet/runtime/issues/1197
+ // move these to JsonSerializerOptions and use the proper encoding.
private static readonly JsonEncodedText _keyName = JsonEncodedText.Encode(KeyName, encoder: null);
private static readonly JsonEncodedText _valueName = JsonEncodedText.Encode(ValueName, encoder: null);
- // todo: it is possible to cache the underlying converters since this is an internal converter and
+ // todo: https://github.com/dotnet/runtime/issues/32352
+ // it is possible to cache the underlying converters since this is an internal converter and
// an instance is created only once for each JsonSerializerOptions instance.
internal override bool OnTryRead(
string key = reader.GetString()!;
- // todo: verify value is converter.TypeToConvert and throw JsonException? (currently no test)
+ // todo: https://github.com/dotnet/runtime/issues/32354
state.Current.ReturnValue = state.ReferenceResolver.ResolveReferenceOnDeserialize(key);
state.Current.ObjectState = StackFrameObjectState.MetadataRefPropertyEndObject;
}
var readerState = new JsonReaderState(options.GetReaderOptions());
- // todo: switch to ArrayBuffer implementation to handle and simplify the allocs?
+ // todo: https://github.com/dotnet/runtime/issues/32355
int utf8BomLength = JsonConstants.Utf8Bom.Length;
byte[] buffer = ArrayPool<byte>.Shared.Rent(Math.Max(options.DefaultBufferSize, utf8BomLength));
int bytesInBuffer = 0;
do
{
- state.FlushThreshold = (int)(bufferWriter.Capacity * .9); //todo: determine best value here
+ // todo: determine best value here
+ // https://github.com/dotnet/runtime/issues/32356
+ state.FlushThreshold = (int)(bufferWriter.Capacity * .9);
isFinalBlock = WriteCore(
writer,
value,
_haveTypesBeenCreated = true;
// todo: for performance and reduced instances, consider using the converters and JsonClassInfo from s_defaultOptions by cloning (or reference directly if no changes).
+ // https://github.com/dotnet/runtime/issues/32357
if (!_classes.TryGetValue(classType, out JsonClassInfo? result))
{
result = _classes.GetOrAdd(classType, new JsonClassInfo(classType, this));
JsonClassInfo newClassInfo = options.GetOrAddClass(type);
// todo: check if type==newtype and skip below?
+ // https://github.com/dotnet/runtime/issues/32358
// Set for exception handling calculation of JsonPath.
JsonPropertyNameAsString = propertyName;
);
// Temporary hack until we can use the same escape algorithm on both sides and make sure we want uppercase hex.
- // Todo: create new AssertContentsAgainJsonNet to avoid calling NormalizeToJsonNetFormat when not necessary.
+ // Todo: https://github.com/dotnet/runtime/issues/32351
Assert.Equal(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
}
);
// Temporary hack until we can use the same escape algorithm on both sides and make sure we want uppercase hex.
- // Todo: create new AssertContentsNotEqualAgainJsonNet to avoid calling NormalizeToJsonNetFormat when not necessary.
+ // Todo: https://github.com/dotnet/runtime/issues/32351
Assert.NotEqual(expectedValue.NormalizeToJsonNetFormat(), value.NormalizeToJsonNetFormat());
}
Assert.Equal(0, obj.MyImmutableList.Count);
TestRoundTrip(obj);
- // Skip ImmutableArray due to https://github.com/dotnet/corefx/issues/42399.
+ // TODO: Skip ImmutableArray due to https://github.com/dotnet/runtime/issues/1037.
const string inputJsonWithNullCollections =
@"{
""Array"":null,
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
+using System.Diagnostics;
using Xunit;
namespace System.Text.Json.Serialization.Tests
// The options are not passed here as that would cause an infinite loop.
Customer value = JsonSerializer.Deserialize<Customer>(ref reader);
- value.Name = value.Name + "Hello!";
+ value.Name += "Hello!";
return value;
}
public override void Write(Utf8JsonWriter writer, Customer value, JsonSerializerOptions options)
{
- // todo: there is no WriteValue yet.
- throw new NotSupportedException();
+ writer.WriteStartArray();
+
+ long bytesWrittenSoFar = writer.BytesCommitted + writer.BytesPending;
+
+ JsonSerializer.Serialize(writer, value);
+
+ Debug.Assert(writer.BytesPending == 0);
+ long payloadLength = writer.BytesCommitted - bytesWrittenSoFar;
+ writer.WriteNumberValue(payloadLength);
+ writer.WriteEndArray();
}
}
Customer customer = JsonSerializer.Deserialize<Customer>(json, options);
Assert.Equal("MyNameHello!", customer.Name);
+
+ string result = JsonSerializer.Serialize(customer, options);
+ int expectedLength = JsonSerializer.Serialize(customer).Length;
+ Assert.Equal(@"[{""CreditLimit"":0,""Name"":""MyNameHello!"",""Address"":{""City"":null}}," + $"{expectedLength}]", result);
}
}
}
Assert.Throws<JsonException>(() => JsonSerializer.Deserialize<Dictionary<string, string>>(json));
}
- [Fact, ActiveIssue("JsonElement fails since it is a struct.")]
+ [Fact]
public static void ObjectToJsonElement()
{
string json = @"{""MyDictionary"":{""Key"":""Value""}}";
- JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json);
+ Dictionary<string, JsonElement> result = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(json);
+ JsonElement element = result["MyDictionary"];
+ Assert.Equal(JsonValueKind.Object, element.ValueKind);
+ Assert.Equal("Value", element.GetProperty("Key").GetString());
}
[Fact]
}
[Fact]
- [ActiveIssue("JsonElement needs to support Path")]
+ [ActiveIssue("https://github.com/dotnet/runtime/issues/32359")]
public static void ExtensionPropertyRoundTripFails()
{
try
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
+using System.Text.Encodings.Web;
using Xunit;
namespace System.Text.Json.Serialization.Tests
[JsonPropertyName("p_3")]
public object P3 => "";
}
+
+ // https://github.com/dotnet/corefx/issues/40979
+ [Fact]
+ public static void EscapingShouldntStackOverflow_40979()
+ {
+ var test = new { Name = "\u6D4B\u8A6611" };
+
+ var options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
+ string result = JsonSerializer.Serialize(test, options);
+
+ Assert.Equal("{\"name\":\"\u6D4B\u8A6611\"}", result);
+ }
}
}
public ClassWithIgnoredUnsupportedBigInteger MyClass { get; set; }
}
- // Todo: add tests with missing object property and missing collection property.
+ // Todo: https://github.com/dotnet/runtime/issues/32348
public class ClassWithPrivateSetterAndGetter
{
Assert.NotEqual(expected, JsonSerializer.Serialize(inputString));
}
- // todo: move this to object tests; it is not a value test.
- // https://github.com/dotnet/corefx/issues/40979
- [Fact]
- public static void EscapingShouldntStackOverflow_40979()
- {
- var test = new { Name = "\u6D4B\u8A6611" };
-
- var options = new JsonSerializerOptions { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, PropertyNamingPolicy = JsonNamingPolicy.CamelCase };
- string result = JsonSerializer.Serialize(test, options);
-
- Assert.Equal("{\"name\":\"\u6D4B\u8A6611\"}", result);
- }
-
[Fact]
public static void WritePrimitives()
{
jsonUtf8.WriteEndObject();
jsonUtf8.Flush();
- // TODO: The output doesn't match what JSON.NET does (different rounding/e-notation).
+ // TODO: https://github.com/dotnet/runtime/issues/32350
// JsonTestHelper.AssertContents(expectedStr, output);
}
}