{"record":{"id":"3d992de24b3e64fe","repo":"Unity-Technologies/UnityCsReference","slug":"cannot-serialize-json-value-of-unknown-type","errorCode":null,"errorMessage":"Cannot serialize json value of unknown type","messagePattern":"Cannot serialize json value of unknown type","errorType":"exception","errorClass":"JSONTypeException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/AssetStore/Json.cs","lineNumber":309,"sourceCode":"                string delim = \"\";\n                foreach (KeyValuePair<string, JSONValue> kv in AsDict())\n                {\n                    res += delim + '\"' + EncodeString(kv.Key) + \"\\\" : \" + kv.Value;\n                    delim = \", \";\n                }\n                return res + \"}\";\n            }\n            else if (IsBool())\n            {\n                return AsBool() ? \"true\" : \"false\";\n            }\n            else if (IsNull())\n            {\n                return \"null\";\n            }\n            else\n            {\n                throw new JSONTypeException(\"Cannot serialize json value of unknown type\");\n            }\n        }\n\n        // Encode a string into a json string\n        private static string EncodeString(string str)\n        {\n            str = str.Replace(\"\\\"\", \"\\\\\\\"\");\n            str = str.Replace(\"\\\\\", \"\\\\\\\\\");\n            str = str.Replace(\"\\b\", \"\\\\b\");\n            str = str.Replace(\"\\f\", \"\\\\f\");\n            str = str.Replace(\"\\n\", \"\\\\n\");\n            str = str.Replace(\"\\r\", \"\\\\r\");\n            str = str.Replace(\"\\t\", \"\\\\t\");\n            // We do not use \\uXXXX specifier but direct unicode in the string.\n            return str;\n        }\n\n        object data;","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/AssetStore/Json.cs#L291-L327","documentation":"When serializing a JSONValue back to a string, the serializer handles string, number, bool, null, list, and dict. The final else branch throws JSONTypeException (\"Cannot serialize json value of unknown type\") if the wrapped CLR object is none of those. This is essentially unreachable through the library's own New* factories and indicates a JSONValue was constructed directly with an unsupported type.","triggerScenarios":"Constructing JSONValue with an unsupported CLR type (e.g. new JSONValue(someDateTime), new JSONValue(someObject)); mutating the internal data field to a non-primitive; a subclass introducing a new wrapped type without extending the serializer.","commonSituations":"Building JSON by hand with domain objects instead of primitives; passing an int or double where the library only stored float (depends on constructor overloads); extending the library incorrectly.","solutions":["Construct JSONValue only through the provided factories (NewString/NewFloat/NewBool/NewList/NewDict/NewNull) or with values known to be string/float/bool.","Convert domain values to primitives before wrapping (e.g. DateTime -> ISO string).","If extending the type set, add a matching branch in the serializer."],"exampleFix":"// before\nvar node = JSONValue.NewDict();\nnode[\"created\"] = new JSONValue(System.DateTime.Now); // unsupported type\n\n// after\nnode[\"created\"] = JSONValue.NewString(System.DateTime.Now.ToString(\"o\"));","handlingStrategy":"validation","validationCode":"// Only wrap supported primitives. Convert everything else.\nJSONValue Wrap(object o) {\n    switch (o) {\n        case string s: return JSONValue.NewString(s);\n        case float f:  return JSONValue.NewFloat(f);\n        case bool b:   return JSONValue.NewBool(b);\n        case null:     return JSONValue.NewNull();\n        case IEnumerable<JSONValue> l: return JSONValue.NewList(new List<JSONValue>(l));\n        default: return JSONValue.NewString(o.ToString());\n    }\n}","typeGuard":"static bool IsSerializable(object o) => o is string || o is float || o is bool || o is List<JSONValue> || o is Dictionary<string, JSONValue> || o == null;","tryCatchPattern":null,"preventionTips":["Always build JSONValue via the New* factories, never new JSONValue(arbitraryObject).","Convert domain types (DateTime, enums, structs) to primitives before wrapping.","Add a unit test that round-trips (build -> serialize) every value you construct."],"tags":["json","serialization","asset-store","unity","programming-error"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}