-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dotnet] Move devtools generator to
System.Text.Json
, update to .NE…
…T 8 (#15061)
- Loading branch information
1 parent
040b9ba
commit 6da3095
Showing
16 changed files
with
122 additions
and
126 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
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
6 changes: 3 additions & 3 deletions
6
third_party/dotnet/devtools/src/generator/CodeGen/CodeGenerationTemplateSettings.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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
namespace OpenQA.Selenium.DevToolsGenerator.CodeGen | ||
{ | ||
using Newtonsoft.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
/// <summary> | ||
/// Defines settings around templates | ||
/// </summary> | ||
public class CodeGenerationTemplateSettings | ||
{ | ||
[JsonProperty("templatePath")] | ||
[JsonPropertyName("templatePath")] | ||
public string TemplatePath { get; set; } | ||
|
||
[JsonProperty("outputPath")] | ||
[JsonPropertyName("outputPath")] | ||
public string OutputPath { get; set; } | ||
} | ||
} |
87 changes: 39 additions & 48 deletions
87
third_party/dotnet/devtools/src/generator/Converters/BooleanJsonConverter.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 |
---|---|---|
@@ -1,68 +1,59 @@ | ||
namespace OpenQA.Selenium.DevToolsGenerator.Converters | ||
{ | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
/// <summary> | ||
/// Handles converting JSON string values into a C# boolean data type. | ||
/// </summary> | ||
public class BooleanJsonConverter : JsonConverter | ||
public class BooleanJsonConverter : JsonConverter<bool> | ||
{ | ||
/// <summary> | ||
/// Determines whether this instance can convert the specified object type. | ||
/// </summary> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <returns> | ||
/// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>. | ||
/// </returns> | ||
public override bool CanConvert(Type objectType) | ||
{ | ||
// Handle only boolean types. | ||
return objectType == typeof(bool); | ||
} | ||
public override bool HandleNull => false; | ||
|
||
/// <summary> | ||
/// Reads the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param> | ||
/// <param name="objectType">Type of the object.</param> | ||
/// <param name="existingValue">The existing value of object being read.</param> | ||
/// <param name="serializer">The calling serializer.</param> | ||
/// <returns> | ||
/// The object value. | ||
/// </returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
public override bool Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
switch (reader.Value.ToString().ToLower().Trim()) | ||
switch (reader.TokenType) | ||
{ | ||
case "true": | ||
case "yes": | ||
case "y": | ||
case "1": | ||
case JsonTokenType.True: | ||
return true; | ||
case "false": | ||
case "no": | ||
case "n": | ||
case "0": | ||
|
||
case JsonTokenType.False: | ||
return false; | ||
} | ||
|
||
// If we reach here, we're pretty much going to throw an error so let's let Json.NET throw it's pretty-fied error message. | ||
return new JsonSerializer().Deserialize(reader, objectType); | ||
} | ||
case JsonTokenType.String: | ||
string boolString = reader.GetString()!; | ||
if (bool.TryParse(boolString, out bool b)) | ||
{ | ||
return b; | ||
} | ||
|
||
boolString = boolString.ToLowerInvariant(); | ||
|
||
/// <summary> | ||
/// Specifies that this converter will not participate in writing results. | ||
/// </summary> | ||
public override bool CanWrite => false; | ||
if (boolString.AsSpan().Trim().SequenceEqual("yes".AsSpan()) || | ||
boolString.AsSpan().Trim().SequenceEqual("y".AsSpan()) || | ||
boolString.AsSpan().Trim().SequenceEqual("1".AsSpan())) | ||
{ | ||
return true; | ||
} | ||
|
||
if (boolString.AsSpan().Trim().SequenceEqual("no".AsSpan()) || | ||
boolString.AsSpan().Trim().SequenceEqual("n".AsSpan()) || | ||
boolString.AsSpan().Trim().SequenceEqual("0".AsSpan())) | ||
{ | ||
return false; | ||
} | ||
|
||
throw new JsonException(); | ||
|
||
default: | ||
throw new JsonException(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Writes the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="writer">The <see cref="T:Newtonsoft.Json.JsonWriter"/> to write to.</param><param name="value">The value.</param><param name="serializer">The calling serializer.</param> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
public override void Write(Utf8JsonWriter writer, bool value, JsonSerializerOptions options) | ||
{ | ||
throw new NotSupportedException(); | ||
writer.WriteBooleanValue(value); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.