Skip to content

Commit 82993d2

Browse files
Remove exception messages from resource strings (#59104)
* remove exception messages from resource strings * fix alphabetical ordering
1 parent cac028c commit 82993d2

17 files changed

+46
-350
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Text;
7+
8+
namespace System.Text.Json.SourceGeneration
9+
{
10+
public sealed partial class JsonSourceGenerator
11+
{
12+
private sealed partial class Emitter
13+
{
14+
/// <summary>
15+
/// Unlike sourcegen warnings, exception messages should not be localized so we keep them in source.
16+
/// </summary>
17+
private static class ExceptionMessages
18+
{
19+
public const string InaccessibleJsonIncludePropertiesNotSupported =
20+
"The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.";
21+
22+
public const string IncompatibleConverterType =
23+
"The converter '{0}' is not compatible with the type '{1}'.";
24+
25+
public const string InitOnlyPropertyDeserializationNotSupported =
26+
"Deserialization of init-only properties is currently not supported in source generation mode.";
27+
28+
public const string InvalidJsonConverterFactoryOutput =
29+
"The converter '{0}' cannot return null or a JsonConverterFactory instance.";
30+
31+
public const string InvalidSerializablePropertyConfiguration =
32+
"Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.";
33+
};
34+
}
35+
}
36+
}

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ private string GenerateForTypeWithUnknownConverter(TypeGenerationSpec typeMetada
324324
}}
325325
else
326326
{{
327-
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{SR.Exception_IncompatibleConverterType}"", converter.GetType(), typeToConvert));
327+
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{ExceptionMessages.IncompatibleConverterType}"", converter.GetType(), typeToConvert));
328328
}}
329329
}}");
330330
}
@@ -333,7 +333,7 @@ private string GenerateForTypeWithUnknownConverter(TypeGenerationSpec typeMetada
333333
metadataInitSource.Append($@"
334334
if (!converter.CanConvert(typeToConvert))
335335
{{
336-
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{SR.Exception_IncompatibleConverterType}"", converter.GetType(), typeToConvert));
336+
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{ExceptionMessages.IncompatibleConverterType}"", converter.GetType(), typeToConvert));
337337
}}");
338338
}
339339

@@ -716,21 +716,21 @@ private string GeneratePropMetadataInitFunc(TypeGenerationSpec typeGenerationSpe
716716
{ DefaultIgnoreCondition: JsonIgnoreCondition.Always } => "getter: null",
717717
{ CanUseGetter: true } => $"getter: static (obj) => (({declaringTypeCompilableName})obj).{clrPropertyName}",
718718
{ CanUseGetter: false, HasJsonInclude: true }
719-
=> @$"getter: static (obj) => throw new {InvalidOperationExceptionTypeRef}(""{SR.Format(SR.Exception_InaccessibleJsonIncludePropertiesNotSupported, typeGenerationSpec.Type.Name, memberMetadata.ClrName)}"")",
719+
=> @$"getter: static (obj) => throw new {InvalidOperationExceptionTypeRef}(""{string.Format(ExceptionMessages.InaccessibleJsonIncludePropertiesNotSupported, typeGenerationSpec.Type.Name, memberMetadata.ClrName)}"")",
720720
_ => "getter: null"
721721
};
722722

723723
string setterNamedArg = memberMetadata switch
724724
{
725725
{ DefaultIgnoreCondition: JsonIgnoreCondition.Always } => "setter: null",
726726
{ CanUseSetter: true, IsInitOnlySetter: true }
727-
=> @$"setter: static (obj, value) => throw new {InvalidOperationExceptionTypeRef}(""{SR.Exception_InitOnlyPropertyDeserializationNotSupported}"")",
727+
=> @$"setter: static (obj, value) => throw new {InvalidOperationExceptionTypeRef}(""{ExceptionMessages.InitOnlyPropertyDeserializationNotSupported}"")",
728728
{ CanUseSetter: true } when typeGenerationSpec.IsValueType
729729
=> $@"setter: static (obj, value) => {UnsafeTypeRef}.Unbox<{declaringTypeCompilableName}>(obj).{clrPropertyName} = value!",
730730
{ CanUseSetter: true }
731731
=> @$"setter: static (obj, value) => (({declaringTypeCompilableName})obj).{clrPropertyName} = value!",
732732
{ CanUseSetter: false, HasJsonInclude: true }
733-
=> @$"setter: static (obj, value) => throw new {InvalidOperationExceptionTypeRef}(""{SR.Format(SR.Exception_InaccessibleJsonIncludePropertiesNotSupported, typeGenerationSpec.Type.Name, memberMetadata.ClrName)}"")",
733+
=> @$"setter: static (obj, value) => throw new {InvalidOperationExceptionTypeRef}(""{string.Format(ExceptionMessages.InaccessibleJsonIncludePropertiesNotSupported, typeGenerationSpec.Type.Name, memberMetadata.ClrName)}"")",
734734
_ => "setter: null",
735735
};
736736

@@ -824,7 +824,7 @@ private string GenerateFastPathFuncForObject(TypeGenerationSpec typeGenSpec)
824824
out Dictionary<string, PropertyGenerationSpec>? serializableProperties,
825825
out bool castingRequiredForProps))
826826
{
827-
string exceptionMessage = SR.Format(SR.Exception_InvalidSerializablePropertyConfiguration, typeRef);
827+
string exceptionMessage = string.Format(ExceptionMessages.InvalidSerializablePropertyConfiguration, typeRef);
828828

829829
return GenerateFastPathFuncForType(
830830
serializeMethodName,
@@ -1205,7 +1205,7 @@ private string GetFetchLogicForRuntimeSpecifiedCustomConverter()
12051205
converter = factory.CreateConverter(type, {OptionsInstanceVariableName});
12061206
if (converter == null || converter is {JsonConverterFactoryTypeRef})
12071207
{{
1208-
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{SR.Exception_InvalidJsonConverterFactoryOutput}"", factory.GetType()));
1208+
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{ExceptionMessages.InvalidJsonConverterFactoryOutput}"", factory.GetType()));
12091209
}}
12101210
}}
12111211
@@ -1236,7 +1236,7 @@ private string GetFetchLogicForGetCustomConverter_TypesWithFactories()
12361236
{JsonConverterTypeRef}? converter = factory.CreateConverter(type, {Emitter.OptionsInstanceVariableName});
12371237
if (converter == null || converter is {JsonConverterFactoryTypeRef})
12381238
{{
1239-
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{SR.Exception_InvalidJsonConverterFactoryOutput}"", factory.GetType()));
1239+
throw new {InvalidOperationExceptionTypeRef}(string.Format(""{ExceptionMessages.InvalidJsonConverterFactoryOutput}"", factory.GetType()));
12401240
}}
12411241
12421242
return converter;

src/libraries/System.Text.Json/gen/Resources/Strings.resx

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,4 @@
165165
<data name="InaccessibleJsonIncludePropertiesNotSupportedFormat" xml:space="preserve">
166166
<value>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</value>
167167
</data>
168-
<!-- runtime exception messages -->
169-
<data name="Exception_IncompatibleConverterType" xml:space="preserve">
170-
<value>The converter '{0}' is not compatible with the type '{1}'.</value>
171-
</data>
172-
<data name="Exception_InitOnlyPropertyDeserializationNotSupported" xml:space="preserve">
173-
<value>Deserialization of init-only properties is currently not supported in source generation mode.</value>
174-
</data>
175-
<data name="Exception_InaccessibleJsonIncludePropertiesNotSupported" xml:space="preserve">
176-
<value>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</value>
177-
</data>
178-
<data name="Exception_InvalidSerializablePropertyConfiguration" xml:space="preserve">
179-
<value>Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</value>
180-
</data>
181-
<data name="Exception_InvalidJsonConverterFactoryOutput" xml:space="preserve">
182-
<value>The converter '{0}' cannot return null or a JsonConverterFactory instance.</value>
183-
</data>
184168
</root>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.cs.xlf

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,6 @@
3232
<target state="translated">Duplicitní název typu</target>
3333
<note />
3434
</trans-unit>
35-
<trans-unit id="Exception_InaccessibleJsonIncludePropertiesNotSupported">
36-
<source>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</source>
37-
<target state="new">The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</target>
38-
<note />
39-
</trans-unit>
40-
<trans-unit id="Exception_IncompatibleConverterType">
41-
<source>The converter '{0}' is not compatible with the type '{1}'.</source>
42-
<target state="new">The converter '{0}' is not compatible with the type '{1}'.</target>
43-
<note />
44-
</trans-unit>
45-
<trans-unit id="Exception_InitOnlyPropertyDeserializationNotSupported">
46-
<source>Deserialization of init-only properties is currently not supported in source generation mode.</source>
47-
<target state="new">Deserialization of init-only properties is currently not supported in source generation mode.</target>
48-
<note />
49-
</trans-unit>
50-
<trans-unit id="Exception_InvalidJsonConverterFactoryOutput">
51-
<source>The converter '{0}' cannot return null or a JsonConverterFactory instance.</source>
52-
<target state="new">The converter '{0}' cannot return null or a JsonConverterFactory instance.</target>
53-
<note />
54-
</trans-unit>
55-
<trans-unit id="Exception_InvalidSerializablePropertyConfiguration">
56-
<source>Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</source>
57-
<target state="new">Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</target>
58-
<note />
59-
</trans-unit>
6035
<trans-unit id="InaccessibleJsonIncludePropertiesNotSupportedFormat">
6136
<source>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</source>
6237
<target state="new">The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</target>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.de.xlf

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,6 @@
3232
<target state="translated">Doppelter Typname</target>
3333
<note />
3434
</trans-unit>
35-
<trans-unit id="Exception_InaccessibleJsonIncludePropertiesNotSupported">
36-
<source>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</source>
37-
<target state="new">The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</target>
38-
<note />
39-
</trans-unit>
40-
<trans-unit id="Exception_IncompatibleConverterType">
41-
<source>The converter '{0}' is not compatible with the type '{1}'.</source>
42-
<target state="new">The converter '{0}' is not compatible with the type '{1}'.</target>
43-
<note />
44-
</trans-unit>
45-
<trans-unit id="Exception_InitOnlyPropertyDeserializationNotSupported">
46-
<source>Deserialization of init-only properties is currently not supported in source generation mode.</source>
47-
<target state="new">Deserialization of init-only properties is currently not supported in source generation mode.</target>
48-
<note />
49-
</trans-unit>
50-
<trans-unit id="Exception_InvalidJsonConverterFactoryOutput">
51-
<source>The converter '{0}' cannot return null or a JsonConverterFactory instance.</source>
52-
<target state="new">The converter '{0}' cannot return null or a JsonConverterFactory instance.</target>
53-
<note />
54-
</trans-unit>
55-
<trans-unit id="Exception_InvalidSerializablePropertyConfiguration">
56-
<source>Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</source>
57-
<target state="new">Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</target>
58-
<note />
59-
</trans-unit>
6035
<trans-unit id="InaccessibleJsonIncludePropertiesNotSupportedFormat">
6136
<source>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</source>
6237
<target state="new">The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</target>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.es.xlf

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,6 @@
3232
<target state="translated">Nombre de tipo duplicado.</target>
3333
<note />
3434
</trans-unit>
35-
<trans-unit id="Exception_InaccessibleJsonIncludePropertiesNotSupported">
36-
<source>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</source>
37-
<target state="new">The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</target>
38-
<note />
39-
</trans-unit>
40-
<trans-unit id="Exception_IncompatibleConverterType">
41-
<source>The converter '{0}' is not compatible with the type '{1}'.</source>
42-
<target state="new">The converter '{0}' is not compatible with the type '{1}'.</target>
43-
<note />
44-
</trans-unit>
45-
<trans-unit id="Exception_InitOnlyPropertyDeserializationNotSupported">
46-
<source>Deserialization of init-only properties is currently not supported in source generation mode.</source>
47-
<target state="new">Deserialization of init-only properties is currently not supported in source generation mode.</target>
48-
<note />
49-
</trans-unit>
50-
<trans-unit id="Exception_InvalidJsonConverterFactoryOutput">
51-
<source>The converter '{0}' cannot return null or a JsonConverterFactory instance.</source>
52-
<target state="new">The converter '{0}' cannot return null or a JsonConverterFactory instance.</target>
53-
<note />
54-
</trans-unit>
55-
<trans-unit id="Exception_InvalidSerializablePropertyConfiguration">
56-
<source>Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</source>
57-
<target state="new">Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</target>
58-
<note />
59-
</trans-unit>
6035
<trans-unit id="InaccessibleJsonIncludePropertiesNotSupportedFormat">
6136
<source>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</source>
6237
<target state="new">The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</target>

src/libraries/System.Text.Json/gen/Resources/xlf/Strings.fr.xlf

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,6 @@
3232
<target state="translated">Nom de type dupliqué.</target>
3333
<note />
3434
</trans-unit>
35-
<trans-unit id="Exception_InaccessibleJsonIncludePropertiesNotSupported">
36-
<source>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</source>
37-
<target state="new">The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</target>
38-
<note />
39-
</trans-unit>
40-
<trans-unit id="Exception_IncompatibleConverterType">
41-
<source>The converter '{0}' is not compatible with the type '{1}'.</source>
42-
<target state="new">The converter '{0}' is not compatible with the type '{1}'.</target>
43-
<note />
44-
</trans-unit>
45-
<trans-unit id="Exception_InitOnlyPropertyDeserializationNotSupported">
46-
<source>Deserialization of init-only properties is currently not supported in source generation mode.</source>
47-
<target state="new">Deserialization of init-only properties is currently not supported in source generation mode.</target>
48-
<note />
49-
</trans-unit>
50-
<trans-unit id="Exception_InvalidJsonConverterFactoryOutput">
51-
<source>The converter '{0}' cannot return null or a JsonConverterFactory instance.</source>
52-
<target state="new">The converter '{0}' cannot return null or a JsonConverterFactory instance.</target>
53-
<note />
54-
</trans-unit>
55-
<trans-unit id="Exception_InvalidSerializablePropertyConfiguration">
56-
<source>Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</source>
57-
<target state="new">Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'.</target>
58-
<note />
59-
</trans-unit>
6035
<trans-unit id="InaccessibleJsonIncludePropertiesNotSupportedFormat">
6136
<source>The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</source>
6237
<target state="new">The member '{0}.{1}' has been annotated with the JsonIncludeAttribute but is not visible to the source generator.</target>

0 commit comments

Comments
 (0)