XINCGer/Unity3DTraining · error · ArgumentException

Invalid field type

Error message

Invalid field type

What it means

JsonFormatter.IsDefaultValue throws ArgumentException when a field's FieldType is not one of the supported scalar/message types. This is an internal exhaustiveness guard: the formatter only knows how to compute default values for known FieldTypes.

Solutions

  1. Regenerate C# code with a protoc version matching the Google.Protobuf runtime package
  2. Verify all field types in the message descriptor are standard protobuf types
  3. Avoid hand-modified or synthesized descriptors
  4. Upgrade the Google.Protobuf NuGet package so the FieldType enum matches
Defensive patterns

Strategy: try-catch

Try / catch

try { json = formatter.Format(msg); } catch (ArgumentException ex) { /* log descriptor/type mismatch; regenerate code */ }

Prevention

When it happens

Trigger: Calling JsonFormatter.Format on a message whose descriptor contains a field with an unrecognized FieldType, typically from a descriptor loaded dynamically or a runtime/generated-code version mismatch.

Common situations: Mixing generated code with an older/newer Google.Protobuf runtime; hand-built descriptors with unusual field types; formatting groups/messages loaded via reflection from foreign descriptor sets.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs:329

                case FieldType.Enum:
                    return (int)value == 0;
                case FieldType.Fixed32:
                case FieldType.UInt32:
                    return (uint)value == 0;
                case FieldType.Fixed64:
                case FieldType.UInt64:
                    return (ulong)value == 0;
                case FieldType.SFixed64:
                case FieldType.Int64:
                case FieldType.SInt64:
                    return (long)value == 0;
                case FieldType.Float:
                    return (float)value == 0f;
                case FieldType.Message:
                case FieldType.Group: // Never expect to get this, but...
                    return value == null;
                default:
                    throw new ArgumentException("Invalid field type");
            }
        }

        /// <summary>
        /// Writes a single value to the given writer as JSON. Only types understood by
        /// Protocol Buffers can be written in this way. This method is only exposed for
        /// advanced use cases; most users should be using <see cref="Format(IMessage)"/>
        /// or <see cref="Format(IMessage, TextWriter)"/>.
        /// </summary>
        /// <param name="writer">The writer to write the value to. Must not be null.</param>
        /// <param name="value">The value to write. May be null.</param>
        public void WriteValue(TextWriter writer, object value)
        {
            if (value == null)
            {
                WriteNull(writer);
            }
            else if (value is bool)

View on GitHub (pinned to 016f98412e)