/// <summary> /// Initializes a new instance of the <see cref="Rectangle"/> struct. /// </summary> /// <param name="coords1">The first coordinates.</param> /// <param name="coords2">The second coordinates.</param> /// <param name="directionType">The rectangle direction type.</param> public Rectangle(Coordinates2 coords1, Coordinates2 coords2, RectangleDirectionType directionType) { this.Direction = directionType; if (directionType == RectangleDirectionType.BottomUp) { this.TopLeft = new Coordinates2(coords1.X, coords2.Y); this.TopRight = coords2; this.BottomLeft = coords1; this.BottomRight = new Coordinates2(coords2.X, coords1.Y); } else // TopDown { this.TopLeft = coords1; this.TopRight = new Coordinates2(coords2.X, coords1.Y); this.BottomLeft = new Coordinates2(coords1.X, coords2.Y); this.BottomRight = coords2; } }
/// <summary> /// Reads the JSON representation of the object. /// </summary> /// <param name="reader">The <see cref="JsonReader"/> to read from.</param> /// <param name="objectType">Type of the object.</param> /// <param name="existingValue">The existing value of object being read. If there is no existing value then <c>null</c> will be used.</param> /// <param name="hasExistingValue">The existing value has a value.</param> /// <param name="serializer">The calling serializer.</param> /// <param name="directionType">The rectangle direction type.</param> /// <returns>The object value.</returns> public Rectangle ReadJson(JsonReader reader, Type objectType, Rectangle existingValue, bool hasExistingValue, JsonSerializer serializer, RectangleDirectionType directionType = RectangleDirectionType.TopDown) { if (!(serializer.Deserialize <JToken>(reader) is JArray jArray)) { throw new JsonSerializationException($"Expected {nameof(jArray)} to be an array"); } return(new Rectangle(jArray[0].ToObject <Coordinates2>(serializer), jArray[1].ToObject <Coordinates2>(serializer), directionType)); }
/// <summary> /// Reads and converts the JSON to type <see cref="Rectangle"/>. /// </summary> /// <param name="reader">The reader.</param> /// <param name="options">An object that specifies serialization options to use.</param> /// <param name="directionType">The rectangle direction type.</param> /// <returns>The converted value.</returns> protected static Rectangle Read(ref Utf8JsonReader reader, JsonSerializerOptions options, RectangleDirectionType directionType) { if (reader.TokenType != JsonTokenType.StartArray) { throw new JsonException("Expected start of array"); } var values = new Coordinates2[2]; for (int i = 0; i < 2; i++) { if (!reader.Read()) { throw new JsonException("Unexpected end of array"); } values[i] = JsonSerializer.Deserialize <Coordinates2>(ref reader, options); } if (!reader.Read() || reader.TokenType != JsonTokenType.EndArray) { throw new JsonException("Expected end of array"); } return(new Rectangle(values[0], values[1], directionType)); }