Reads the JSON representation of the object. The to read from. Type of the object. The serializer options. The object value.
(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
| 30 | /// <param name="options">The serializer options.</param> |
| 31 | /// <returns>The object value.</returns> |
| 32 | public override Version? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 33 | { |
| 34 | if (reader.TokenType == JsonTokenType.Null) |
| 35 | return null; |
| 36 | |
| 37 | if (reader.TokenType == JsonTokenType.StartObject) |
| 38 | { |
| 39 | try |
| 40 | { |
| 41 | reader.Read(); |
| 42 | var values = new Dictionary<string, int>(); |
| 43 | while (reader.TokenType == JsonTokenType.PropertyName) |
| 44 | { |
| 45 | var key = reader.GetString(); |
| 46 | reader.Read(); |
| 47 | var val = reader.GetInt32(); |
| 48 | reader.Read(); |
| 49 | values.Add(key, val); |
| 50 | } |
| 51 | |
| 52 | values.TryGetValue("Major", out var major); |
| 53 | values.TryGetValue("Minor", out var minor); |
| 54 | if (!values.TryGetValue("Build", out var build)) |
| 55 | build = -1; |
| 56 | if (!values.TryGetValue("Revision", out var revision)) |
| 57 | revision = -1; |
| 58 | |
| 59 | if (build <= 0) |
| 60 | return new Version(major, minor); |
| 61 | if (revision <= 0) |
| 62 | return new Version(major, minor, build); |
| 63 | return new Version(major, minor, build, revision); |
| 64 | } |
| 65 | catch (Exception ex) |
| 66 | { |
| 67 | throw new Exception(string.Format("Error parsing version string: {0}", reader.GetString()), ex); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if (reader.TokenType == JsonTokenType.String) |
| 72 | { |
| 73 | try |
| 74 | { |
| 75 | return new Version((string)reader.GetString()!); |
| 76 | } |
| 77 | catch (Exception ex) |
| 78 | { |
| 79 | throw new Exception(string.Format("Error parsing version string: {0}", reader.GetString()), ex); |
| 80 | } |
| 81 | } |
| 82 | throw new Exception(string.Format("Unexpected token or value when parsing version. Token: {0}, Value: {1}", reader.TokenType, reader.GetString())); |
| 83 | } |
| 84 | |
| 85 | /// <summary> |
| 86 | /// Determines whether this instance can convert the specified object type. |
no test coverage detected