XINCGer/Unity3DTraining · error · InvalidProtocolBufferException

Unsupported JSON token type

Error message

Unsupported JSON token type 

What it means

ParseSingleValue's switch exhausted all expected JSON token types (name/value kinds it supports plus Null); the default case rejects any other JsonToken.TokenType with this InvalidProtocolBufferException, appending the token type and target field type. It fires when a scalar/single-value parse position receives a token the parser does not associate with a valid single value — most commonly a structural token.

Solutions

  1. Validate the JSON payload shape against the proto schema: scalar fields must receive string/number/boolean, repeated fields must receive arrays, message fields must receive objects.
  2. Fix mismatched nesting in the JSON (unbalanced braces/brackets) before parsing.
  3. If using a custom JsonTokenizer, ensure it only emits standard JsonToken.TokenType values for valid positions.
  4. Catch InvalidProtocolBufferException and surface the token type and field type mentioned in the message to pinpoint the payload defect.

Example fix

// before: array where a scalar is expected
{"retries": [3]}

// after
{"retries": 3}
Defensive patterns

Strategy: type-guard

Validate before calling

// Scalar fields must receive string/number/boolean tokens, not objects or arrays
function isScalar(v) { return ['string', 'number', 'boolean'].includes(typeof v) || v === null; }

Type guard

function isScalar(v) { return v === null || typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean'; }

Try / catch

try { var msg = JsonParser.Default.Parse<T>(json); }
catch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith("Unsupported JSON token type")) { /* surface token type + field type from message to caller */ }

Prevention

When it happens

Trigger: A field expecting a scalar receives a structural JSON token not routed elsewhere — e.g. an unmatched StartObject/StartArray token reaching a scalar field during malformed nesting, or a parser-internal token kind appearing where a value is expected.

Common situations: Malformed JSON where an array value lands on a scalar field path through MergeRepeatedField edge cases; custom JsonTokenizer implementations emitting unexpected token types; truncated or corrupted JSON streams producing dangling structural tokens.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

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

            {
                case JsonToken.TokenType.True:
                case JsonToken.TokenType.False:
                    if (fieldType == FieldType.Bool)
                    {
                        return token.Type == JsonToken.TokenType.True;
                    }
                    // Fall through to "we don't support this type for this case"; could duplicate the behaviour of the default
                    // case instead, but this way we'd only need to change one place.
                    goto default;
                case JsonToken.TokenType.StringValue:
                    return ParseSingleStringValue(field, token.StringValue);
                // Note: not passing the number value itself here, as we may end up storing the string value in the token too.
                case JsonToken.TokenType.Number:
                    return ParseSingleNumberValue(field, token);
                case JsonToken.TokenType.Null:
                    throw new NotImplementedException("Haven't worked out what to do for null yet");
                default:
                    throw new InvalidProtocolBufferException("Unsupported JSON token type " + token.Type + " for field type " + fieldType);
            }
        }

        /// <summary>
        /// Parses <paramref name="json"/> into a new message.
        /// </summary>
        /// <typeparam name="T">The type of message to create.</typeparam>
        /// <param name="json">The JSON to parse.</param>
        /// <exception cref="InvalidJsonException">The JSON does not comply with RFC 7159</exception>
        /// <exception cref="InvalidProtocolBufferException">The JSON does not represent a Protocol Buffers message correctly</exception>
        public T Parse<T>(string json) where T : IMessage, new()
        {
            ProtoPreconditions.CheckNotNull(json, "json");
            return Parse<T>(new StringReader(json));
        }

        /// <summary>
        /// Parses JSON read from <paramref name="jsonReader"/> into a new message.

View on GitHub (pinned to 016f98412e)