|
| 1 | +namespace CloudinaryDotNet.Actions |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Globalization; |
| 5 | + using Newtonsoft.Json; |
| 6 | + |
| 7 | + /// <summary> |
| 8 | + /// Custom JSON converter to safely parse boolean values that might come as strings. |
| 9 | + /// </summary> |
| 10 | + public class SafeBooleanConverter : JsonConverter |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// Gets a value indicating whether this <see cref="SafeBooleanConverter"/> can write JSON. |
| 14 | + /// </summary> |
| 15 | + public override bool CanWrite => false; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Determines whether this instance can convert the specified object type. |
| 19 | + /// </summary> |
| 20 | + /// <param name="objectType">Type of the object.</param> |
| 21 | + /// <returns>True if this instance can convert the specified object type; otherwise, false.</returns> |
| 22 | + public override bool CanConvert(Type objectType) |
| 23 | + { |
| 24 | + // Decide which types you want this converter to handle |
| 25 | + return objectType == typeof(bool) || objectType == typeof(bool?); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Reads the JSON representation of the object. |
| 30 | + /// </summary> |
| 31 | + /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> |
| 32 | + /// <param name="objectType">Type of the object.</param> |
| 33 | + /// <param name="existingValue">The existing value of the object being read.</param> |
| 34 | + /// <param name="serializer">The calling serializer.</param> |
| 35 | + /// <returns>The object value.</returns> |
| 36 | + public override object ReadJson( |
| 37 | + JsonReader reader, |
| 38 | + Type objectType, |
| 39 | + object existingValue, |
| 40 | + JsonSerializer serializer) |
| 41 | + { |
| 42 | + // We'll parse everything into a 'bool?' and decide how to return |
| 43 | + // based on whether the target is bool or bool?. |
| 44 | + bool? parsedValue = null; |
| 45 | + |
| 46 | + if (reader.TokenType == JsonToken.Null || |
| 47 | + (reader.Value is string strVal && string.IsNullOrWhiteSpace(strVal))) |
| 48 | + { |
| 49 | + parsedValue = null; |
| 50 | + } |
| 51 | + |
| 52 | + // Handle Boolean token directly: e.g., true or false |
| 53 | + else if (reader is { TokenType: JsonToken.Boolean, Value: bool boolValue }) |
| 54 | + { |
| 55 | + parsedValue = boolValue; |
| 56 | + } |
| 57 | + |
| 58 | + // Handle integer "0" or "1" |
| 59 | + else if (reader is { TokenType: JsonToken.Integer, Value: not null }) |
| 60 | + { |
| 61 | + var intValue = Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture); |
| 62 | + parsedValue = intValue == 1; |
| 63 | + } |
| 64 | + |
| 65 | + // Handle string values: "0", "1", "true", "false", etc. |
| 66 | + else if (reader is { TokenType: JsonToken.String, Value: string stringValue }) |
| 67 | + { |
| 68 | + switch (stringValue) |
| 69 | + { |
| 70 | + case "0": |
| 71 | + parsedValue = false; |
| 72 | + break; |
| 73 | + case "1": |
| 74 | + parsedValue = true; |
| 75 | + break; |
| 76 | + default: |
| 77 | + if (bool.TryParse(stringValue, out var boolFromString)) |
| 78 | + { |
| 79 | + parsedValue = boolFromString; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + throw new JsonSerializationException( |
| 84 | + $"Unable to parse \"{stringValue}\" as a boolean value."); |
| 85 | + } |
| 86 | + |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + // Unexpected token => throw |
| 93 | + throw new JsonSerializationException( |
| 94 | + $"Unexpected token {reader.TokenType} when parsing a boolean."); |
| 95 | + } |
| 96 | + |
| 97 | + // If we're targeting bool? => return the parsedValue (can be null or bool). |
| 98 | + // If we're targeting bool => return false if parsedValue is null, else the bool value. |
| 99 | + if (objectType == typeof(bool?)) |
| 100 | + { |
| 101 | + return parsedValue; |
| 102 | + } |
| 103 | + |
| 104 | + // objectType == typeof(bool) |
| 105 | + return parsedValue ?? false; |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// Writes the JSON representation of the object. |
| 110 | + /// </summary> |
| 111 | + /// <param name="writer">The <see cref="JsonWriter"/> to write to.</param> |
| 112 | + /// <param name="value">The value.</param> |
| 113 | + /// <param name="serializer">The calling serializer.</param> |
| 114 | + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) |
| 115 | + { |
| 116 | + throw new NotImplementedException("Unnecessary because this converter is used primarily for deserialization."); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments