Newtonsoft to Text.Json, i miss some attributes #115287
Answered
by
elgonzo
zms9110750
asked this question in
Q&A
-
According to the AI's instructions, I added Newtonsoft.Json attributes to my entity. I tried to find the corresponding System.Text.Json attributes but couldn't find them. I encountered two issues:
[JsonObject]
[method: System.Text.Json.Serialization.JsonConstructor]
[method: Newtonsoft.Json.JsonConstructor]
public record ItemI18n(
[property: JsonPropertyName("en"), JsonProperty("en")] Language En,
[property: JsonPropertyName("ko"), JsonProperty("ko")] Language? Ko = null,
[property: JsonPropertyName("ru"), JsonProperty("ru")] Language? Ru = null,
[property: JsonPropertyName("de"), JsonProperty("de")] Language? De = null,
[property: JsonPropertyName("fr"), JsonProperty("fr")] Language? Fr = null,
[property: JsonPropertyName("pt"), JsonProperty("pt")] Language? Pt = null,
[property: JsonPropertyName("es"), JsonProperty("es")] Language? Es = null,
[property: JsonPropertyName("it"), JsonProperty("it")] Language? It = null,
[property: JsonPropertyName("pl"), JsonProperty("pl")] Language? Pl = null,
[property: JsonPropertyName("uk"), JsonProperty("uk")] Language? Uk = null,
[property: JsonPropertyName("zh-hans"), JsonProperty("zh-hans")] Language? ZhHans = null,
[property: JsonPropertyName("zh-hant"), JsonProperty("zh-hant")] Language? ZhHant = null
) : IEnumerable<KeyValuePair<string, Language>>, ITuple |
Beta Was this translation helpful? Give feedback.
Answered by
elgonzo
May 4, 2025
Replies: 2 comments
-
var p2 = """
{
"id": "54aae292e7798909064f1575",
"slug": "secura_dual_cestra",
"gameRef": "/Lotus/Weapons/Syndicates/PerrinSequence/Pistols/PSDualCestra",
"tags": [ "syndicate", "weapon", "secondary" ],
"i18n": {
"en": {
"name": "Secura Dual Cestra",
"icon": "items/images/en/secura_dual_cestra.3d47a4ec6675ff774bb0da9b16c53e0e.png",
"thumb": "items/images/en/thumbs/secura_dual_cestra.3d47a4ec6675ff774bb0da9b16c53e0e.128x128.png"
}
}
}
""";
var a2 = JObject.Parse(p2).ToObject<ItemShort>();
try
{
var b2 = JsonSerializer.Deserialize<ItemShort>(p2);
}
catch (System.Text.Json.JsonException eion)
{
Console.WriteLine(eion.Path);
Console.WriteLine(eion.Message);
} ItemShortpublic partial record ItemShort(
[property: JsonPropertyName("id"), JsonProperty("id")] string Id,
[property: JsonPropertyName("slug"), JsonProperty("slug")] string Slug,
[property: JsonPropertyName("gameRef"), JsonProperty("gameRef")] string GameRef,
[property: JsonPropertyName("tags"), JsonProperty("tags")] HashSet<string> Tags,
[property: JsonPropertyName("maxRank"), JsonProperty("maxRank")] int? MaxRank,
[property: JsonPropertyName("vaulted"), JsonProperty("vaulted")] bool? Vaulted,
[property: JsonPropertyName("ducats"), JsonProperty("ducats")] int? Ducats,
[property: JsonPropertyName("maxAmberStars"), JsonProperty("maxAmberStars")] int? MaxAmberStars,
[property: JsonPropertyName("maxCyanStars"), JsonProperty("maxCyanStars")] int? MaxCyanStars,
[property: JsonPropertyName("baseEndo"), JsonProperty("baseEndo")] int? BaseEndo,
[property: JsonPropertyName("endoMultiplier"), JsonProperty("endoMultiplier")] float? EndoMultiplier,
[property: JsonPropertyName("subtypes"), JsonProperty("subtypes")] HashSet<Subtypes>? Subtypes) Languagepublic record Language(
[property: JsonPropertyName("name"), JsonProperty("name")] string Name,
[property: JsonPropertyName("icon"), JsonProperty("icon")] string Icon,
[property: JsonPropertyName("thumb"), JsonProperty("thumb")] string Thumb,
[property: JsonPropertyName("description"), JsonProperty("description")] string? Description = null,
[property: JsonPropertyName("wikiLink"), JsonProperty("wikiLink")] string? WikiLink = null,
[property: JsonPropertyName("subIcon"), JsonProperty("subIcon")] string? SubIcon = null); ItemI18n[JsonObject]
[method: System.Text.Json.Serialization.JsonConstructor]
[method: Newtonsoft.Json.JsonConstructor]
public record ItemI18n(
[property: JsonPropertyName("en"), JsonProperty("en")] Language En,
[property: JsonPropertyName("ko"), JsonProperty("ko")] Language? Ko = null,
[property: JsonPropertyName("ru"), JsonProperty("ru")] Language? Ru = null,
[property: JsonPropertyName("de"), JsonProperty("de")] Language? De = null,
[property: JsonPropertyName("fr"), JsonProperty("fr")] Language? Fr = null,
[property: JsonPropertyName("pt"), JsonProperty("pt")] Language? Pt = null,
[property: JsonPropertyName("es"), JsonProperty("es")] Language? Es = null,
[property: JsonPropertyName("it"), JsonProperty("it")] Language? It = null,
[property: JsonPropertyName("pl"), JsonProperty("pl")] Language? Pl = null,
[property: JsonPropertyName("uk"), JsonProperty("uk")] Language? Uk = null,
[property: JsonPropertyName("zh-hans"), JsonProperty("zh-hans")] Language? ZhHans = null,
[property: JsonPropertyName("zh-hant"), JsonProperty("zh-hant")] Language? ZhHant = null
) : IEnumerable<KeyValuePair<string, Language>>, ITuple
{
public ItemI18n(IReadOnlyDictionary<string, Language> pairs)
: this(pairs[LanguageCode[0]],
pairs.GetValueOrDefault(LanguageCode[1]),
pairs.GetValueOrDefault(LanguageCode[2]),
pairs.GetValueOrDefault(LanguageCode[3]),
pairs.GetValueOrDefault(LanguageCode[4]),
pairs.GetValueOrDefault(LanguageCode[5]),
pairs.GetValueOrDefault(LanguageCode[6]),
pairs.GetValueOrDefault(LanguageCode[7]),
pairs.GetValueOrDefault(LanguageCode[8]),
pairs.GetValueOrDefault(LanguageCode[9]),
pairs.GetValueOrDefault(LanguageCode[10]),
pairs.GetValueOrDefault(LanguageCode[11]))
{ }
int ITuple.Length => 12;
object? ITuple.this[int index] => index switch
{
0 => En,
1 => Ko,
2 => Ru,
3 => De,
4 => Fr,
5 => Pt,
6 => Es,
7 => It,
8 => Pl,
9 => Uk,
10 => ZhHans,
11 => ZhHant,
_ => new ArgumentOutOfRangeException(nameof(index), "must in [0 .. 11]")
};
static string[] LanguageCode { get; } = ["En", "Ko", "Ru", "De", "Fr", "Pt", "Es", "It", "Pl", "Uk", "ZhHans", "ZhHant",];
public IEnumerator<KeyValuePair<string, Language>> GetEnumerator()
{
ITuple tuple = this;
for (int i = 0; i < tuple.Length; i++)
{
if (tuple[i] is Language language)
{
yield return new KeyValuePair<string, Language>(LanguageCode[i], language);
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is an open issue that is being tracked here: #63791 Upvote it so the team can gauge the interest in / relevance of this feature. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zms9110750
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an open issue that is being tracked here: #63791 Upvote it so the team can gauge the interest in / relevance of this feature.