Пропуск поля из сериализации класса POCO для OpenBadge
У меня есть следующий класс POCO:
public class BadgeClass
{
[Key]
[ScriptIgnore()]
//Internal ID not used for sending information to the OpenBadges API
public int BadgeID { get; set; }
//The name of the achievement.
[StringLength(128)]
public string Name { get; set; }
//A short description of the achievement.
[StringLength(128)]
public string Description { get; set; }
//URL of an image representing the achievement. Should be a square and in PNG format. Maximum size is 256kb.
public string Image { get; set; }
//URL of the criteria for earning the achievement. If the badge represents an educational achievement, consider marking
//up this up with LRMI
public string Criteria { get; set; }
//URL of the organization that issued the badge. Endpoint should be an IssuerOrganization
public string Issuer { get; set; }
//List of objects describing which educational standards this badge aligns to, if any.
//public List<AlignmentObject> Alignments { get; set; }
//List of tags that describe the type of achievement.
//public List<string> Tags { get; set; }
}
Я пытаюсь сериализовать объект в JSON и записать в локальный файл, используя следующий код:
var json = JsonConvert.SerializeObject(newBadge);
System.IO.File.WriteAllText(@"c:\test.json", json);
Это работает настолько, что создается файл JSON, но я хочу опустить поле BadgeClassID в сериализации. Я думал, что маркер ScriptIgnore позаботится об этом. Есть ли способ сделать это?
1 ответ
При использовании Newtonsoft.Json JsonCovert.SerializeObject
чтобы выполнить сериализацию, вы можете игнорировать свойства, используя [JsonIgnore]
приписывать.
Пересмотрите ключевое свойство, чтобы оно читалось:
[Key]
[JsonIgnore]
//Internal ID not used for sending information to the OpenBadges API
public int BadgeID { get; set; }