Skip to content

Problem deserializing very large floating-point values #66

@SchumacherBrianJ

Description

@SchumacherBrianJ

I have encountered an issue processing very large floating-point values. All of the data being written to InfluxDB are 32-bit floating point values with large exponents. When the values are greater than 1.0E21 (approximately) everything works fine as InfluxDB will serialize the JSON response using exponential format. However, when the values fall into the 1E17 to 1E20 range, InfluxDB serializes those values using all significant digits followed by trailing zeroes. For example, 2.016152E17 is serialized as 201615200000000000. When InfluxDB.Client (Newtonsoft) attempts to deserialize this value it does so as a System.Numerics.BitInteger for which there is no conversion to float (or most any number) and an exception is thrown. I have worked around the issue temporarily by checking for BigInteger, converting it to a string and then converting the string. This is all done in ResultFactory.cs as shown below (excuse the obnoxious comment slashes - put them to highlight change). Another possibility might be to capture the exception during the final SetValue and then create the string

              if( property != null )
              {
                 if( value != null )
                 {
                    if( property.Key == InfluxConstants.TimeColumn )
                    {
                       property.SetValue( dataPoint, timestampParser.ToTimestamp( options.Precision, value ) );
                    }
                    else if( property.IsDateTime )
                    {
                       property.SetValue( dataPoint, DateTime.Parse( (string)value, CultureInfo.InvariantCulture, OnlyUtcStyles ) );
                    }
                    else if( property.IsDateTimeOffset )
                    {
                       property.SetValue( dataPoint, DateTimeOffset.Parse( (string)value, CultureInfo.InvariantCulture ) );
                    }
                    else if( property.IsEnum )
                    {
                       property.SetValue( dataPoint, property.GetEnumValue( value ) );
                    }
                    else
                    {
                       if( value.GetType() == property.Type )
                       {
                          property.SetValue( dataPoint, value );
                       }
                      /////////////////////////////////////////////////
                      //Add check for BigInteger deserialization
                      /////////////////////////////////////////////////
                       else if (value.GetType() == typeof(System.Numerics.BigInteger))
                       {
                          string stringValue = value.ToString();
                          property.SetValue(dataPoint, Convert.ChangeType(stringValue, property.Type, CultureInfo.InvariantCulture));
                       }
                       else if ( value is string stringValue )
                       {
                          if( !string.IsNullOrEmpty( stringValue ) )
                          {
                             property.SetValue( dataPoint, Convert.ChangeType( stringValue, property.Type, CultureInfo.InvariantCulture ) );
                          }
                       }
                       else
                       {
                         ////////////////////////////////////////////////////////////////////////////////////////
                         //Or catch exception for invalid conversion and attemp conversion from string
                         ////////////////////////////////////////////////////////////////////////////////////////
                          try
                          {
                             property.SetValue( dataPoint, Convert.ChangeType( value, property.Type, CultureInfo.InvariantCulture ) );
                          }
                          catch (InvalidCastException)
                          {
                             string stringValue = value.ToString();
                             property.SetValue(dataPoint, Convert.ChangeType(stringValue, property.Type, CultureInfo.InvariantCulture));
                          }
                       }
                    }
                 }
              }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions