Omitting a field from POCO class serialization for OpenBadge

49 Views Asked by At

I have the following POCO class:

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; }
}

I am trying to serialize the object into JSON and write to local file using the following code:

var json = JsonConvert.SerializeObject(newBadge);
System.IO.File.WriteAllText(@"c:\test.json", json);

This is working so much in that the JSON file is being created but I want to omit the BadgeClassID field from the serialization. I thought that the ScriptIgnore marker would have taken care of this. Is there a way to do this?

1

There are 1 best solutions below

0
On

When using Newtonsoft.Json JsonCovert.SerializeObject to do your serialization, you can ignore properties by using the [JsonIgnore] attribute.

Revise your key property to read:

[Key]
[JsonIgnore]
//Internal ID not used for sending information to the OpenBadges API
public int BadgeID { get; set; }