XINCGer/Unity3DTraining · error · InvalidProtocolBufferException

Value out of range:

Error message

Value out of range: 

What it means

When converting a JSON number to a float field, the parser checks that the double value fits in the float range; values whose magnitude exceeds float.MaxValue (other than infinities) throw this InvalidProtocolBufferException.

Solutions

  1. Clamp or rescale the value at the producer before serializing.
  2. Change the field type from float to double in the .proto if full range is needed.
  3. Catch InvalidProtocolBufferException and substitute float.PositiveInfinity/NegativeInfinity if saturation is acceptable.

Example fix

// before
{"temperature": 1e60}  // float field
// after
{"temperature": 3.4e38} // or change field to double
Defensive patterns

Strategy: validation

Validate before calling

// Check float-field values are within float range before parsing
if (Math.Abs(rawValue) > (double)float.MaxValue && !double.IsInfinity(rawValue))
    throw new ArgumentOutOfRangeException(nameof(rawValue), $"{rawValue} exceeds float range");

Type guard

static bool FitsInFloat(double d) => double.IsInfinity(d) || (d >= -3.40282347e38 && d <= 3.40282347e38);

Try / catch

try
{
    var msg = parser.Parse<T>(json);
}
catch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith("Value out of range:") && ex.Message.Contains("float"))
{
    // clamp and retry, or reject the record
}

Prevention

When it happens

Trigger: Parsing JSON containing a numeric value like 1e60 for a float (google.protobuf.FloatValue or float field) — representable as double but out of float range.

Common situations: Sensor/scientific data serialized to JSON with values beyond ~3.4e38; unit mismatches (e.g. nanoseconds vs seconds producing huge numbers).

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of XINCGer/Unity3DTraining@016f98412e (2026-09-12). Data as JSON: /api/errors/f4d34274f4c4268f. Report an issue: GitHub.

Appendix: source

Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs:644

                            return (ulong)value;
                        case FieldType.Double:
                            return value;
                        case FieldType.Float:
                            if (double.IsNaN(value))
                            {
                                return float.NaN;
                            }
                            if (value > float.MaxValue || value < float.MinValue)
                            {
                                if (double.IsPositiveInfinity(value))
                                {
                                    return float.PositiveInfinity;
                                }
                                if (double.IsNegativeInfinity(value))
                                {
                                    return float.NegativeInfinity;
                                }
                                throw new InvalidProtocolBufferException("Value out of range: " + value);
                            }
                            return (float)value;
                        case FieldType.Enum:
                            CheckInteger(value);
                            // Just return it as an int, and let the CLR convert it.
                            // Note that we deliberately don't check that it's a known value.
                            return (int)value;
                        default:
                            throw new InvalidProtocolBufferException("Unsupported conversion from JSON number for field type " + field.FieldType);
                    }
                }
                catch (OverflowException)
                {
                    throw new InvalidProtocolBufferException("Value out of range: " + value);
                }
            }
        }

View on GitHub (pinned to 016f98412e)