-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add ListWithStringFallback converters and JSON roundtrip test
- Loading branch information
Showing
9 changed files
with
166 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/Docfx.App/Config/ListWithStringFallbackConverter.NewtonsoftJson.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Docfx; | ||
|
||
|
||
internal partial class ListWithStringFallbackConverter | ||
{ | ||
/// <summary> | ||
/// JsonConverter for <see cref="ListWithStringFallback"/>. | ||
/// </summary> | ||
internal class NewtonsoftJsonConverter : JsonConverter | ||
{ | ||
/// <inheritdoc/> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(FileMapping); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var model = new ListWithStringFallback(); | ||
var value = reader.Value; | ||
IEnumerable<JToken> jItems; | ||
if (reader.TokenType == JsonToken.StartArray) | ||
{ | ||
jItems = JArray.Load(reader); | ||
} | ||
else if (reader.TokenType == JsonToken.StartObject) | ||
{ | ||
jItems = JContainer.Load(reader); | ||
} | ||
else if (reader.TokenType == JsonToken.String) | ||
{ | ||
jItems = JRaw.Load(reader); | ||
} | ||
else | ||
{ | ||
jItems = JObject.Load(reader); | ||
} | ||
|
||
if (jItems is JValue) | ||
{ | ||
model.Add(jItems.ToString()); | ||
} | ||
else | ||
{ | ||
foreach (var item in jItems) | ||
{ | ||
model.Add(item.ToString()); | ||
} | ||
} | ||
|
||
return model; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
writer.WriteStartArray(); | ||
foreach (var item in (ListWithStringFallback)value) | ||
{ | ||
serializer.Serialize(writer, item); | ||
} | ||
writer.WriteEndArray(); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/Docfx.App/Config/ListWithStringFallbackConverter.SystemTextJson.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using YamlDotNet.Serialization; | ||
|
||
namespace Docfx; | ||
|
||
|
||
internal partial class ListWithStringFallbackConverter | ||
{ | ||
/// <summary> | ||
/// JsonConverter for <see cref="ListWithStringFallback"/>. | ||
/// </summary> | ||
internal class SystemTextJsonConverter : JsonConverter<ListWithStringFallback> | ||
{ | ||
public override ListWithStringFallback Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
var tokenType = reader.TokenType; | ||
switch (tokenType) | ||
{ | ||
case JsonTokenType.String: | ||
{ | ||
var value = reader.GetString(); | ||
return new ListWithStringFallback([value]); | ||
} | ||
case JsonTokenType.StartArray: | ||
{ | ||
var items = JsonSerializer.Deserialize<string[]>(ref reader, options); | ||
return new ListWithStringFallback(items); | ||
} | ||
case JsonTokenType.StartObject: | ||
{ | ||
using var document = JsonDocument.ParseValue(ref reader); | ||
JsonElement root = document.RootElement; | ||
var values = root.EnumerateObject().Select(x=>x.ToString()); | ||
return new ListWithStringFallback(values); | ||
} | ||
default: | ||
throw new NotSupportedException($"TokenType({reader.TokenType}) is not supported."); | ||
} | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, ListWithStringFallback value, JsonSerializerOptions options) | ||
{ | ||
writer.WriteStartArray(); | ||
foreach (var item in value) | ||
{ | ||
writer.WriteStringValue(item); | ||
} | ||
writer.WriteEndArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
test/docfx.Tests/SerializationTests/JsonSerializationTest.ListWithStringFallback.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Docfx; | ||
using Docfx.Common; | ||
using FluentAssertions; | ||
|
||
namespace docfx.Tests; | ||
|
||
public partial class JsonSerializationTest | ||
{ | ||
[Theory] | ||
[TestData<ListWithStringFallback>] | ||
public void JsonSerializationTest_ListWithStringFallback(string path) | ||
{ | ||
// Arrange | ||
var model = TestData.Load<ListWithStringFallback>(path); | ||
|
||
// Act/Assert | ||
ValidateJsonRoundTrip(model); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...x.Tests/SerializationTests/TestData/ListWithStringFallback/ListWithStringFallback_01.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"string_item1" |
5 changes: 5 additions & 0 deletions
5
...x.Tests/SerializationTests/TestData/ListWithStringFallback/ListWithStringFallback_02.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
"array_item_01", | ||
"array_item_02", | ||
"array_item_03" | ||
] |
5 changes: 5 additions & 0 deletions
5
...x.Tests/SerializationTests/TestData/ListWithStringFallback/ListWithStringFallback_03.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"object_item_01": "1", | ||
"object_item_02": "2", | ||
"object_item_03": "3" | ||
} |