XINCGer/Unity3DTraining · error · InvalidProtocolBufferException
Invalid numeric value:
Error message
Invalid numeric value:
What it means
ParseNumericString validates textual numeric tokens before delegating to int/long/double parsers. The protobuf JSON spec forbids a leading '+' on numbers, so any text starting with '+' is rejected with InvalidProtocolBufferException. JSON numbers must follow the strict JSON grammar; '+5' is invalid JSON.
Solutions
- Remove the leading '+' from the numeric string (an unsigned positive number needs no sign)
- Fix the formatter producing the string (e.g. drop the '+' section in a .NET format string)
- Parse with double/int.Parse on the cleaned value before embedding it into JSON
- Pre-strip or reject '+'-prefixed numerics in your JSON preprocessing step
Example fix
// before
string json = "{\"score\": \"+10\"}"; // leading '+' rejected
// after
string json = "{\"score\": 10}"; Defensive patterns
Strategy: validation
Validate before calling
static readonly System.Text.RegularExpressions.Regex JsonNumber =
new System.Text.RegularExpressions.Regex(@"^-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?$");
bool IsValidJsonNumber(string text) => JsonNumber.IsMatch(text); Try / catch
try { return JsonParser.Default.Parse<T>(json); }
catch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith("Invalid numeric value")) {
logger.LogWarning(ex, "Malformed JSON number");
return null;
} Prevention
- Use invariant-culture ToString for numbers, never format strings with '+' sections
- Validate payloads with a strict JSON parser before protobuf parsing
- Ban sign-prefixed numerics in upstream producers
When it happens
Trigger: Calling JsonParser.Parse<T> with numeric fields whose values are written with an explicit plus sign, e.g. {"score": "+10"} where a string-form numeric is parsed, or code that formats numbers with a custom format string including a sign specifier ('+0;-0;0').
Common situations: Config files hand-edited with '+value' notation; log/telemetry strings that carry explicit signs; sprintf-style formatting with '+' flag reused to build JSON; pasting values from spreadsheets.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- Invalid numeric value for type:
- Value out of range
- Invalid field type
- Unable to format value of type
- Type registry has no descriptor for type name '
AI-assisted analysis of XINCGer/Unity3DTraining@016f98412e (2026-09-12).
Data as JSON: /api/errors/4ed8a67426dc7a9c.
Report an issue: GitHub.
Appendix: source
Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs:738
default:
throw new InvalidProtocolBufferException("Unsupported conversion from JSON string for field type " + field.FieldType);
}
}
/// <summary>
/// Creates a new instance of the message type for the given field.
/// </summary>
private static IMessage NewMessageForField(FieldDescriptor field)
{
return field.MessageType.Parser.CreateTemplate();
}
private static T ParseNumericString<T>(string text, Func<string, NumberStyles, IFormatProvider, T> parser)
{
// Can't prohibit this with NumberStyles.
if (text.StartsWith("+"))
{
throw new InvalidProtocolBufferException("Invalid numeric value: " + text);
}
if (text.StartsWith("0") && text.Length > 1)
{
if (text[1] >= '0' && text[1] <= '9')
{
throw new InvalidProtocolBufferException("Invalid numeric value: " + text);
}
}
else if (text.StartsWith("-0") && text.Length > 2)
{
if (text[2] >= '0' && text[2] <= '9')
{
throw new InvalidProtocolBufferException("Invalid numeric value: " + text);
}
}
try
{
return parser(text, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture);View on GitHub (pinned to 016f98412e)