Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add issues to the TODOs in S.T.Json source for better tracking and minor clean up #32360

Merged
merged 4 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -421,8 +421,7 @@ private static bool TryParseDateTimeOffset(ReadOnlySpan<byte> source, out DateTi
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ internal sealed class KeyValuePairConverter<TKey, TValue> : JsonValueConverter<K
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal static bool ResolveMetadata(

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static async ValueTask<TValue> ReadAsync<TValue>(

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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ private static async Task WriteAsyncCore(Stream utf8Json, object? value, Type in

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ internal JsonClassInfo GetOrAddClass(Type classType)
_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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public JsonConverter InitializeReEntry(Type type, JsonSerializerOptions options,
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;
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Text.Json/tests/JsonTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ public static void AssertContents(string expectedValue, ArrayBufferWriter<byte>
);

// 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());
}

Expand All @@ -741,7 +741,7 @@ public static void AssertContentsNotEqual(string expectedValue, ArrayBufferWrite
);

// 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ static void TestRoundTrip(ClassWithNonNullEnumerableGetters obj)
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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
Expand All @@ -23,14 +24,22 @@ public override Customer Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
// 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();
}
}

Expand All @@ -44,6 +53,10 @@ public static void ConverterWithCallback()

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1263,11 +1263,14 @@ public static void ObjectToStringFail()
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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ public static void EscapingFails()
}

[Fact]
[ActiveIssue("JsonElement needs to support Path")]
[ActiveIssue("https://github.com/dotnet/runtime/issues/32359")]
public static void ExtensionPropertyRoundTripFails()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// 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
Expand Down Expand Up @@ -125,5 +126,17 @@ private class Polymorphic
[JsonPropertyName("p_3")]
public object P3 => "";
}

// https://github.com/dotnet/corefx/issues/40979
ahsonkhan marked this conversation as resolved.
Show resolved Hide resolved
[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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public class WrapperForClassWithIgnoredUnsupportedBigInteger
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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,6 @@ public static void WriteStringWithRelaxedEscaper()
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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5554,7 +5554,7 @@ public void WriteNumbers(bool formatted, bool skipValidation, string keyString)
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);
}
}
Expand Down