/// <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.</param>
        /// <param name="serializer">The calling serializer.</param>
        /// <returns>The object value.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            var index = new GrowingObjectIndex();

            while (reader.Read() && reader.TokenType == JsonToken.PropertyName)
            {
                var propertyName = reader.Value.ToString();
                reader.Read();

                if (!ItemPropertyName.Equals(propertyName))
                {
                    continue;
                }

                var  value = serializer.Deserialize(reader);
                long longValue;

                // Handle case where avro-json format is not used
                if (value is long || value is double)
                {
                    index.Item = value;
                }
                // Handle number sent as string
                else if (value is string && long.TryParse(value.ToString(), out longValue))
                {
                    index.Item = longValue;
                }
                else if (value is JObject)
                {
                    var jObject        = value as JObject;
                    var doubleProperty = jObject.Property(DoubleDataType);
                    var longProperty   = jObject.Property(LongDataType);

                    if (doubleProperty != null)
                    {
                        index.Item = doubleProperty.Value.Value <double>();
                    }
                    else if (longProperty != null)
                    {
                        index.Item = longProperty.Value.Value <long>();
                    }
                    else
                    {
                        throw new JsonSerializationException($"Unsupported data type: {value}");
                    }
                }
                else if (value != null)
                {
                    throw new JsonSerializationException($"Unsupported data type: {value}");
                }
            }

            return(index);
        }
예제 #2
0
        public void GrowingObjectIndexConverter_WriteJson_serializes_long_value()
        {
            var index = new GrowingObjectIndex
            {
                Item = 10L
            };

            var expected = "{\"item\":{\"long\":10}}";
            var json     = EtpExtensions.Serialize(index);

            Assert.AreEqual(expected, json);
        }
예제 #3
0
        public void GrowingObjectIndexConverter_WriteJson_serializes_null_value()
        {
            var index = new GrowingObjectIndex
            {
                Item = null
            };

            const string expected = "{\"item\":null}";
            var          json     = EtpExtensions.Serialize(index);

            Assert.AreEqual(expected, json);
        }
예제 #4
0
        public void GrowingObjectIndexConverter_WriteJson_raises_error_for_unsupported_data_type()
        {
            var index = new GrowingObjectIndex
            {
                Item = "abc"
            };

            var pass = false;

            try
            {
                EtpExtensions.Serialize(index);
            }
            catch (JsonSerializationException)
            {
                pass = true;
            }

            Assert.IsTrue(pass);
        }