{"record":{"id":"6ec6392714fb223a","repo":"JamesNK/Newtonsoft.Json","slug":"a-name-is-required-when-setting-property-name-stat","errorCode":null,"errorMessage":"A name is required when setting property name state.","messagePattern":"A name is required when setting property name state\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/JsonWriter.Async.cs","lineNumber":1666,"sourceCode":"        protected Task SetWriteStateAsync(JsonToken token, object value, CancellationToken cancellationToken)\n        {\n            if (cancellationToken.IsCancellationRequested)\n            {\n                return cancellationToken.FromCanceled();\n            }\n\n            switch (token)\n            {\n                case JsonToken.StartObject:\n                    return InternalWriteStartAsync(token, JsonContainerType.Object, cancellationToken);\n                case JsonToken.StartArray:\n                    return InternalWriteStartAsync(token, JsonContainerType.Array, cancellationToken);\n                case JsonToken.StartConstructor:\n                    return InternalWriteStartAsync(token, JsonContainerType.Constructor, cancellationToken);\n                case JsonToken.PropertyName:\n                    if (!(value is string s))\n                    {\n                        throw new ArgumentException(\"A name is required when setting property name state.\", nameof(value));\n                    }\n\n                    return InternalWritePropertyNameAsync(s, cancellationToken);\n                case JsonToken.Comment:\n                    return InternalWriteCommentAsync(cancellationToken);\n                case JsonToken.Raw:\n                    return AsyncUtils.CompletedTask;\n                case JsonToken.Integer:\n                case JsonToken.Float:\n                case JsonToken.String:\n                case JsonToken.Boolean:\n                case JsonToken.Date:\n                case JsonToken.Bytes:\n                case JsonToken.Null:\n                case JsonToken.Undefined:\n                    return InternalWriteValueAsync(token, cancellationToken);\n                case JsonToken.EndObject:\n                    return InternalWriteEndAsync(JsonContainerType.Object, cancellationToken);","sourceCodeStart":1648,"sourceCodeEnd":1684,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/JsonWriter.Async.cs#L1648-L1684","documentation":"Thrown on the async path (JsonWriter.Async.cs) inside InternalWriteTokenAsync when a JsonToken.PropertyName is being written but the supplied value is not a string. JSON property names must be strings, so a non-string name is rejected with ArgumentException before the writer corrupts the output. This mirrors the synchronous guard in JsonWriter.cs.","triggerScenarios":"Calling WritePropertyNameAsync with a non-string value, or routing a JsonToken.PropertyName token through an async write path (e.g. WriteTokenAsync / a JTokenWriter async copy) where the token's value object is not a string.","commonSituations":"Custom async writers or token pipelines passing boxed non-string values as property names; programmatically constructing tokens with numeric/object property names; copying JToken trees whose property names are not strings.","solutions":["Always pass a string property name to WritePropertyName/WritePropertyNameAsync.","When writing tokens via WriteTokenAsync, ensure PropertyName tokens carry a string value.","Convert non-string keys (e.g. dictionary numeric keys) to string before emitting a property name."],"exampleFix":"// before\nawait writer.WriteTokenAsync(JsonToken.PropertyName, 42);\n\n// after\nawait writer.WritePropertyNameAsync(\"42\");","handlingStrategy":"validation","validationCode":"if (value is string name)\n{\n    await writer.WritePropertyNameAsync(name, escape: false);\n}","typeGuard":"static bool IsValidPropertyName(object value) => value is string;","tryCatchPattern":null,"preventionTips":["Always pass string names to WritePropertyNameAsync.","Stringify non-string keys before emitting a property name.","Ensure PropertyName tokens in WriteTokenAsync pipelines carry a string value."],"tags":["json-writer","async","type-mismatch"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}