XINCGer/Unity3DTraining · error · InvalidProtocolBufferException

Invalid field type for map:

Error message

Invalid field type for map: 

What it means

ParseMapKey handles all legal protobuf map key field types (bool, string, and integer types). If the map's key field is any other type (e.g. float, double, bytes — which are disallowed as map keys in proto anyway), the parser falls through to the default case and throws this InvalidProtocolBufferException.

Solutions

  1. Regenerate code from the .proto ensuring map keys are only integral, bool, or string types.
  2. Verify the descriptor source matches the generated code version (rebuild both from the same .proto files).
  3. If using dynamic messages, validate the map field's key FieldDescriptor.FieldType before parsing.
Defensive patterns

Strategy: validation

Validate before calling

// Validate map key field type on the generated descriptor
var keyType = MyMessage.Descriptor.FindFieldByName("myMap").MessageType.FindFieldByName("key").FieldType;
bool ok = keyType == FieldType.Bool || keyType == FieldType.String ||
          (keyType >= FieldType.Int32 && keyType <= FieldType.Fixed64);
if (!ok) throw new Exception($"Unsupported map key type: {keyType}");

Try / catch

try
{
    var msg = parser.Parse<T>(json);
}
catch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith("Invalid field type for map:"))
{
    // indicates generated code/descriptor mismatch; regenerate code
}

Prevention

When it happens

Trigger: Parsing JSON for a generated map type whose key field descriptor has an unsupported FieldType — typically from a corrupted/desynced descriptor set or a descriptor built outside the standard proto map-key constraints.

Common situations: Reflectively constructed descriptors or generated code/descriptor mismatch after regenerating protos with a different compiler version; dynamic messages built with invalid key types.

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/e7217357a08551e8. Report an issue: GitHub.

Appendix: source

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

                    throw new InvalidProtocolBufferException("Invalid string for bool map key: " + keyText);
                case FieldType.String:
                    return keyText;
                case FieldType.Int32:
                case FieldType.SInt32:
                case FieldType.SFixed32:
                    return ParseNumericString<int>(keyText, int.Parse);
                case FieldType.UInt32:
                case FieldType.Fixed32:
                    return ParseNumericString<uint>(keyText, uint.Parse);
                case FieldType.Int64:
                case FieldType.SInt64:
                case FieldType.SFixed64:
                    return ParseNumericString<long>(keyText, long.Parse);
                case FieldType.UInt64:
                case FieldType.Fixed64:
                    return ParseNumericString<ulong>(keyText, ulong.Parse);
                default:
                    throw new InvalidProtocolBufferException("Invalid field type for map: " + field.FieldType);
            }
        }

        private static object ParseSingleNumberValue(FieldDescriptor field, JsonToken token)
        {
            double value = token.NumberValue;
            checked
            {
                try
                {
                    switch (field.FieldType)
                    {
                        case FieldType.Int32:
                        case FieldType.SInt32:
                        case FieldType.SFixed32:
                            CheckInteger(value);
                            return (int)value;
                        case FieldType.UInt32:

View on GitHub (pinned to 016f98412e)