{"record":{"id":"36820488c752ce2b","repo":"XINCGer/Unity3DTraining","slug":"unable-to-format-value-of-type","errorCode":null,"errorMessage":"Unable to format value of type ","messagePattern":"Unable to format value of type ","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs","lineNumber":414,"sourceCode":"                string text = ((IFormattable)value).ToString(\"r\", CultureInfo.InvariantCulture);\n                if (text == \"NaN\" || text == \"Infinity\" || text == \"-Infinity\")\n                {\n                    writer.Write('\"');\n                    writer.Write(text);\n                    writer.Write('\"');\n                }\n                else\n                {\n                    writer.Write(text);\n                }\n            }\n            else if (value is IMessage)\n            {\n                Format((IMessage)value, writer);\n            }\n            else\n            {\n                throw new ArgumentException(\"Unable to format value of type \" + value.GetType());\n            }\n        }\n\n        /// <summary>\n        /// Central interception point for well-known type formatting. Any well-known types which\n        /// don't need special handling can fall back to WriteMessage. We avoid assuming that the\n        /// values are using the embedded well-known types, in order to allow for dynamic messages\n        /// in the future.\n        /// </summary>\n        private void WriteWellKnownTypeValue(TextWriter writer, MessageDescriptor descriptor, object value)\n        {\n            // Currently, we can never actually get here, because null values are always handled by the caller. But if we *could*,\n            // this would do the right thing.\n            if (value == null)\n            {\n                WriteNull(writer);\n                return;\n            }","sourceCodeStart":396,"sourceCodeEnd":432,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonFormatter.cs#L396-L432","documentation":"JsonFormatter.WriteValue throws ArgumentException when the runtime value is not one of the types the formatter can serialize (bool, numeric types, string, ByteString, IMessage, enumerables, IFormattable enums, etc.). The formatter is strictly typed and refuses unknown object shapes.","triggerScenarios":"Formatting a message field whose runtime value is an unsupported CLR type, e.g. a custom class, DateTime, or a null-adjacent value that fell through all type checks in WriteValue's if/else chain.","commonSituations":"Custom message implementations not derived from generated IMessage<T> code; inserting foreign objects into maps/lists of a message via reflection; writing custom well-known-type handlers that pass raw values to WriteValue.","solutions":["Ensure messages are generated Google.Protobuf types (implement IMessage properly)","Convert unsupported values (e.g. DateTime) to protobuf-compatible types or well-known wrappers (Timestamp/Duration) before formatting","Check any custom WriteValue/WriteWellKnownTypeValue extension code for passing raw objects","Use a custom JsonFormatter.Settings formatter hook for exotic values"],"exampleFix":"// before\nwriter.WriteValue(myDateTime);\n// after\nvar ts = Timestamp.FromDateTime(myDateTime); formatter.Format(ts, writer);","handlingStrategy":"try-catch","validationCode":"// ensure field values are protobuf-native types before formatting\nif (value is IMessage || value is string || value is int || value is long || value is float || value is double || value is bool || value is ByteString) { formatter.Format(msg); }","typeGuard":null,"tryCatchPattern":"try { writer = formatter.Format(msg); } catch (ArgumentException ex) { /* handle unsupported value type: ex.Message contains the type */ }","preventionTips":["Use only generated message types and scalar wrappers","Convert DateTime/Decimal to Timestamp/Duration well-known types","Avoid injecting foreign objects via reflection"],"tags":["csharp","protobuf","json"],"backgroundTag":"unexpected-response-shape","analyzedSha":"016f98412ea5e11756b286736f479b66ab6216d5","analyzedAt":"2026-09-12T01:08:58.711Z","contentChangedAt":"2026-09-12T01:08:58.711Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}