XINCGer/Unity3DTraining · error · InvalidProtocolBufferException

Unsupported conversion from JSON string for field type

Error message

Unsupported conversion from JSON string for field type 

What it means

ParseSingleStringValue is only used when a JSON string token must be decoded into a single field value. Its switch over field types handles strings, bytes, enums and a few others; any field type that cannot legally be represented as a JSON string reaches the default branch and throws. The library is telling you a string token was supplied for a field whose type has no string representation in the protobuf JSON mapping.

Solutions

  1. Remove the quotes so the field is a native JSON number/bool/object matching its proto type
  2. Check the field's FieldType in the generated C# class to confirm the expected JSON representation
  3. Fix upstream JSON producers to emit correctly typed JSON per the protobuf JSON mapping
  4. Pre-validate the JSON structure against the message descriptor before parsing

Example fix

// before
parser.Parse<MyMessage>("{\"count\": \"5\"}"); // string for int32 field
// after
parser.Parse<MyMessage>("{\"count\": 5}");
Defensive patterns

Strategy: validation

Validate before calling

bool IsJsonStringForNonStringField(string json, MessageDescriptor d) {
    // Reject quoted values for numeric/bool/message fields per the proto JSON mapping.
    foreach (var f in d.Fields.InFieldNumberOrder()) {
        if (f.FieldType == FieldType.String || f.FieldType == FieldType.Enum || f.FieldType == FieldType.Bytes) continue;
        if (System.Text.RegularExpressions.Regex.IsMatch(json, "\"" + f.JsonName + "\"\\s*:\\s*\"")) return false;
    }
    return true;
}

Try / catch

try { return JsonParser.Default.Parse<T>(json); }
catch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith("Unsupported conversion from JSON string")) {
    logger.LogWarning(ex, "String token used for non-string field");
    return null;
}

Prevention

When it happens

Trigger: Calling JsonParser.Parse<T> with a quoted JSON string for a field whose FieldType is numeric, bool, message, or another type not handled by the string branch, e.g. {"count": "5"} for an int32 field where the JSON mapping expects the number 5.

Common situations: Hand-authoring JSON where every value is quoted; middleware/templating that stringifies all values; copying output from a lenient formatter; storing numbers as strings in config and feeding that JSON to the parser.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

                    return ParseNumericString<ulong>(text, ulong.Parse);
                case FieldType.Double:
                    double d = ParseNumericString<double>(text, double.Parse);
                    ValidateInfinityAndNan(text, double.IsPositiveInfinity(d), double.IsNegativeInfinity(d), double.IsNaN(d));
                    return d;
                case FieldType.Float:
                    float f = ParseNumericString<float>(text, float.Parse);
                    ValidateInfinityAndNan(text, float.IsPositiveInfinity(f), float.IsNegativeInfinity(f), float.IsNaN(f));
                    return f;
                case FieldType.Enum:
                    var enumValue = field.EnumType.FindValueByName(text);
                    if (enumValue == null)
                    {
                        throw new InvalidProtocolBufferException("Invalid enum value: " + text + " for enum type: " + field.EnumType.FullName);
                    }
                    // Just return it as an int, and let the CLR convert it.
                    return enumValue.Number;
                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);
            }

View on GitHub (pinned to 016f98412e)