"Fossies" - the Fresh Open Source Software Archive 
Member "selenium-selenium-4.8.1/third_party/dotnet/devtools/src/generator/Converters/BooleanJsonConverter.cs" (17 Feb 2023, 2639 Bytes) of package /linux/www/selenium-selenium-4.8.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C# source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 namespace OpenQA.Selenium.DevToolsGenerator.Converters
2 {
3 using Newtonsoft.Json;
4 using System;
5
6 /// <summary>
7 /// Handles converting JSON string values into a C# boolean data type.
8 /// </summary>
9 public class BooleanJsonConverter : JsonConverter
10 {
11 /// <summary>
12 /// Determines whether this instance can convert the specified object type.
13 /// </summary>
14 /// <param name="objectType">Type of the object.</param>
15 /// <returns>
16 /// <c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
17 /// </returns>
18 public override bool CanConvert(Type objectType)
19 {
20 // Handle only boolean types.
21 return objectType == typeof(bool);
22 }
23
24 /// <summary>
25 /// Reads the JSON representation of the object.
26 /// </summary>
27 /// <param name="reader">The <see cref="T:Newtonsoft.Json.JsonReader"/> to read from.</param>
28 /// <param name="objectType">Type of the object.</param>
29 /// <param name="existingValue">The existing value of object being read.</param>
30 /// <param name="serializer">The calling serializer.</param>
31 /// <returns>
32 /// The object value.
33 /// </returns>
34 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
35 {
36 switch (reader.Value.ToString().ToLower().Trim())
37 {
38 case "true":
39 case "yes":
40 case "y":
41 case "1":
42 return true;
43 case "false":
44 case "no":
45 case "n":
46 case "0":
47 return false;
48 }
49
50 // 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.
51 return new JsonSerializer().Deserialize(reader, objectType);
52 }
53
54 /// <summary>
55 /// Specifies that this converter will not participate in writing results.
56 /// </summary>
57 public override bool CanWrite { get { return false; } }
58
59 /// <summary>
60 /// Writes the JSON representation of the object.
61 /// </summary>
62 /// <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>
63 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
64 {
65 }
66 }
67 }