/// <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>
        /// <exception cref="UnitsNetException">Unable to parse value and unit from JSON.</exception>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
                                        JsonSerializer serializer)
        {
            if (reader.ValueType != null)
            {
                return(reader.Value);
            }

            object obj = TryDeserializeIComparable(reader, serializer);

            if (obj is Array values)
            {
                // Create array with the requested type, such as `Length[]` or `Frequency[]` or multi-dimensional arrays like `Length[,]` or `Frequency[,,]`
                var arrayOfQuantities = Array.CreateInstance(objectType.GetElementType(), MultiDimensionalArrayHelpers.LastIndex(values));

                // Fill array with parsed quantities
                int[] index = MultiDimensionalArrayHelpers.FirstIndex(values);
                while (index != null)
                {
                    arrayOfQuantities.SetValue(values.GetValue(index), index);
                }

                return(arrayOfQuantities);
            }
            else if (obj is ValueUnit valueUnit)
            {
                return(ParseValueUnit(valueUnit));
            }
            else
            {
                return(obj);
            }
        }
        /// <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="obj">The value to write.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <exception cref="UnitsNetException">Can't serialize 'null' value.</exception>
        public override void WriteJson(JsonWriter writer, object obj, JsonSerializer serializer)
        {
            // ValueUnit should be written as usual (but read in a custom way)
            if (obj is ValueUnit valueUnit)
            {
                var localSerializer = new JsonSerializer
                {
                    TypeNameHandling = serializer.TypeNameHandling,
                };

                var t = JToken.FromObject(valueUnit, localSerializer);
                t.WriteTo(writer);
            }
            else if (obj is Array values)
            {
                var results = Array.CreateInstance(typeof(ValueUnit), MultiDimensionalArrayHelpers.LastIndex(values));
                var ind     = MultiDimensionalArrayHelpers.FirstIndex(values);

                while (ind != null)
                {
                    results.SetValue((IQuantity)values.GetValue(ind), ind);
                    ind = MultiDimensionalArrayHelpers.NextIndex(results, ind);
                }

                serializer.Serialize(writer, results);
            }
            else if (obj is IQuantity quantity)
            {
                serializer.Serialize(writer, ToValueUnit(quantity));
            }
            else
            {
                throw new NotSupportedException($"Unsupported type: {obj.GetType()}");
            }
        }