{"record":{"id":"cbe340767b5de58f","repo":"XINCGer/Unity3DTraining","slug":"unsupported-conversion-from-json-string-for-field","errorCode":null,"errorMessage":"Unsupported conversion from JSON string for field type ","messagePattern":"Unsupported conversion from JSON string for field type ","errorType":"validation","errorClass":"InvalidProtocolBufferException","httpStatus":null,"severity":"error","filePath":"NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs","lineNumber":721,"sourceCode":"                    return ParseNumericString<ulong>(text, ulong.Parse);\n                case FieldType.Double:\n                    double d = ParseNumericString<double>(text, double.Parse);\n                    ValidateInfinityAndNan(text, double.IsPositiveInfinity(d), double.IsNegativeInfinity(d), double.IsNaN(d));\n                    return d;\n                case FieldType.Float:\n                    float f = ParseNumericString<float>(text, float.Parse);\n                    ValidateInfinityAndNan(text, float.IsPositiveInfinity(f), float.IsNegativeInfinity(f), float.IsNaN(f));\n                    return f;\n                case FieldType.Enum:\n                    var enumValue = field.EnumType.FindValueByName(text);\n                    if (enumValue == null)\n                    {\n                        throw new InvalidProtocolBufferException(\"Invalid enum value: \" + text + \" for enum type: \" + field.EnumType.FullName);\n                    }\n                    // Just return it as an int, and let the CLR convert it.\n                    return enumValue.Number;\n                default:\n                    throw new InvalidProtocolBufferException(\"Unsupported conversion from JSON string for field type \" + field.FieldType);\n            }\n        }\n\n        /// <summary>\n        /// Creates a new instance of the message type for the given field.\n        /// </summary>\n        private static IMessage NewMessageForField(FieldDescriptor field)\n        {\n            return field.MessageType.Parser.CreateTemplate();\n        }\n\n        private static T ParseNumericString<T>(string text, Func<string, NumberStyles, IFormatProvider, T> parser)\n        {\n            // Can't prohibit this with NumberStyles.\n            if (text.StartsWith(\"+\"))\n            {\n                throw new InvalidProtocolBufferException(\"Invalid numeric value: \" + text);\n            }","sourceCodeStart":703,"sourceCodeEnd":739,"githubUrl":"https://github.com/XINCGer/Unity3DTraining/blob/016f98412ea5e11756b286736f479b66ab6216d5/NetWorkAndResources/Socket_Protobuff/Assets/Plugins/Google.Protobuf/JsonParser.cs#L703-L739","documentation":"ParseSingleStringValue is only used when a JSON string token must be decoded into a single field value. Its switch over field types handles strings, bytes, enums and a few others; any field type that cannot legally be represented as a JSON string reaches the default branch and throws. The library is telling you a string token was supplied for a field whose type has no string representation in the protobuf JSON mapping.","triggerScenarios":"Calling JsonParser.Parse<T> with a quoted JSON string for a field whose FieldType is numeric, bool, message, or another type not handled by the string branch, e.g. {\"count\": \"5\"} for an int32 field where the JSON mapping expects the number 5.","commonSituations":"Hand-authoring JSON where every value is quoted; middleware/templating that stringifies all values; copying output from a lenient formatter; storing numbers as strings in config and feeding that JSON to the parser.","solutions":["Remove the quotes so the field is a native JSON number/bool/object matching its proto type","Check the field's FieldType in the generated C# class to confirm the expected JSON representation","Fix upstream JSON producers to emit correctly typed JSON per the protobuf JSON mapping","Pre-validate the JSON structure against the message descriptor before parsing"],"exampleFix":"// before\nparser.Parse<MyMessage>(\"{\\\"count\\\": \\\"5\\\"}\"); // string for int32 field\n// after\nparser.Parse<MyMessage>(\"{\\\"count\\\": 5}\");","handlingStrategy":"validation","validationCode":"bool IsJsonStringForNonStringField(string json, MessageDescriptor d) {\n    // Reject quoted values for numeric/bool/message fields per the proto JSON mapping.\n    foreach (var f in d.Fields.InFieldNumberOrder()) {\n        if (f.FieldType == FieldType.String || f.FieldType == FieldType.Enum || f.FieldType == FieldType.Bytes) continue;\n        if (System.Text.RegularExpressions.Regex.IsMatch(json, \"\\\"\" + f.JsonName + \"\\\"\\\\s*:\\\\s*\\\"\")) return false;\n    }\n    return true;\n}","typeGuard":null,"tryCatchPattern":"try { return JsonParser.Default.Parse<T>(json); }\ncatch (InvalidProtocolBufferException ex) when (ex.Message.StartsWith(\"Unsupported conversion from JSON string\")) {\n    logger.LogWarning(ex, \"String token used for non-string field\");\n    return null;\n}","preventionTips":["Never quote numbers/booleans in protobuf JSON","Let serializers (not string templates) build JSON payloads","Check the generated field type before hand-writing JSON","Use JsonFormatter output as the reference shape"],"tags":["protobuf","json","type-mismatch","deserialization"],"backgroundTag":"unsupported-operation","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"}