{"record":{"id":"007c52be09c6f710","repo":"JamesNK/Newtonsoft.Json","slug":"null-collection-of-serializable-members-returned","errorCode":null,"errorMessage":"Null collection of serializable members returned.","messagePattern":"Null collection of serializable members returned\\.","errorType":"exception","errorClass":"JsonSerializationException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs","lineNumber":1376,"sourceCode":"            {\n                return type.FullName!;\n            }\n\n            return \"{0}.{1}\".FormatWith(CultureInfo.InvariantCulture, type.Namespace, type.Name);\n        }\n\n        /// <summary>\n        /// Creates properties for the given <see cref=\"JsonContract\"/>.\n        /// </summary>\n        /// <param name=\"type\">The type to create properties for.</param>\n        /// /// <param name=\"memberSerialization\">The member serialization mode for the type.</param>\n        /// <returns>Properties for the given <see cref=\"JsonContract\"/>.</returns>\n        protected virtual IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)\n        {\n            List<MemberInfo> members = GetSerializableMembers(type);\n            if (members == null)\n            {\n                throw new JsonSerializationException(\"Null collection of serializable members returned.\");\n            }\n\n            DefaultJsonNameTable nameTable = GetNameTable();\n\n            JsonPropertyCollection properties = new JsonPropertyCollection(type);\n\n            foreach (MemberInfo member in members)\n            {\n                JsonProperty property = CreateProperty(member, memberSerialization);\n\n                if (property != null)\n                {\n                    // nametable is not thread-safe for multiple writers\n                    lock (nameTable)\n                    {\n                        property.PropertyName = nameTable.Add(property.PropertyName!);\n                    }\n","sourceCodeStart":1358,"sourceCodeEnd":1394,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs#L1358-L1394","documentation":"CreateProperties calls the virtual GetSerializableMembers and throws a JsonSerializationException (line 1376) if it returns null. An empty list is acceptable; null is not, because the property-building loop would fail. This is a contract for anyone overriding GetSerializableMembers in a custom contract resolver.","triggerScenarios":"A custom DefaultContractResolver (or IContractResolver) override of GetSerializableMembers that returns null instead of a (possibly empty) IList<MemberInfo>.","commonSituations":"Writing a custom resolver and short-circuiting with 'return null;' when no members match a filter.","solutions":["Return an empty list (e.g. 'return new List<MemberInfo>();') instead of null from the override.","If you intended to skip property creation, return an empty collection rather than null."],"exampleFix":"// before\nprotected override IList<MemberInfo> GetSerializableMembers(Type objectType) {\n    var members = base.GetSerializableMembers(objectType).Where(Keep);\n    return members.Any() ? members.ToList() : null; // throws\n}\n\n// after\nprotected override IList<MemberInfo> GetSerializableMembers(Type objectType) {\n    return base.GetSerializableMembers(objectType).Where(Keep).ToList();\n}","handlingStrategy":"validation","validationCode":"// If you ship a custom resolver, assert in tests that GetSerializableMembers never returns null.\nvar resolver = new MyCustomResolver();\nforeach (var t in typesUnderTest)\n{\n    var members = resolver.GetSerializableMembers(t);\n    if (members == null)\n        throw new InvalidOperationException($\"GetSerializableMembers returned null for {t}.\");\n}","typeGuard":"// n/a: this is a contract for an override, not a runtime type test","tryCatchPattern":"try { JsonConvert.SerializeObject(value, settings); }\ncatch (JsonSerializationException ex) when (ex.Message == \"Null collection of serializable members returned.\")\n{\n    // fix the custom resolver override to return an empty list instead of null\n}","preventionTips":["Never return null from a GetSerializableMembers override; return an empty list.","Use '.ToList()' on filtered member queries to guarantee a non-null collection."],"tags":["contract-resolution","custom-resolver","null-reference","members"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}