XINCGer/Unity3DTraining · error · InvalidProtocolBufferException

Invalid string for bool map key:

Error message

Invalid string for bool map key: 

What it means

ParseMapKey converts JSON map keys (which are always strings) to the map's key field type. For bool keys, only the exact strings 'true' and 'false' are accepted per the protobuf JSON mapping; anything else throws this InvalidProtocolBufferException.

Solutions

  1. Change bool map keys to lowercase 'true'/'false' in the JSON.
  2. If the producer cannot be fixed, change the map key type to string or int32 in the .proto and convert on both sides.
  3. Pre-normalize keys (map 'True'->'true', '1'->'true') before passing JSON to the parser.

Example fix

// before
{"myBoolMap": {"True": 1, "False": 2}}
// after
{"myBoolMap": {"true": 1, "false": 2}}
Defensive patterns

Strategy: validation

Validate before calling

// Normalize/validate bool map keys before parsing
string normalized = Regex.Replace(json,
    '"(True|False|TRUE|FALSE|1|0)"\\s*:',
    m => '"' + (m.Groups[1].Value.Equals("True", StringComparison.OrdinalIgnoreCase) || m.Groups[1].Value == "1" ? "true" : "false") + '":');

Try / catch

try
{
    var msg = parser.Parse<T>(json);
}
catch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith("Invalid string for bool map key:"))
{
    // re-run with normalized keys or reject payload
}

Prevention

When it happens

Trigger: Parsing JSON for a map<bool, ...> with keys like 'True', 'FALSE', '1', or '0' — protobuf JSON requires lowercase 'true'/'false' string keys.

Common situations: JSON generated by a non-conforming serializer using 'True'/'False' (PascalCase) or numeric '1'/'0' for boolean map keys; hand-written JSON.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

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

                throw new InvalidProtocolBufferException("Expected end-object token after @type/value for well-known type");
            }
        }

        #region Utility methods which don't depend on the state (or settings) of the parser.
        private static object ParseMapKey(FieldDescriptor field, string keyText)
        {
            switch (field.FieldType)
            {
                case FieldType.Bool:
                    if (keyText == "true")
                    {
                        return true;
                    }
                    if (keyText == "false")
                    {
                        return false;
                    }
                    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);

View on GitHub (pinned to 016f98412e)