0

Process:

  1. Create the Player class.

  2. Create the player List<Player>().

  3. Add players to the list.

  4. Write a json serial file.

    When I click on writeJson button I receive the error:

    "Type `'EditPlayers.Player'` cannot be serialized. Consider marking it with the 
    `DataContractAttribute` attribute, and marking all of its members you want serialized 
    with the `DataMemberAttribute` attribute.  If the type is a collection, consider marking 
    it with the `CollectionDataContractAttribute`.See the Microsoft .NET Framework 
    documentation for other supported types."
    

Question:

How to mark List<Player>() with the CollectionDataContractAttribute.

class Player
{
    public int MemberId { get; set; }
    public int Gender { get; set; }
    public string LName { get; set; }
    public string FName { get; set; }
    public int Tee { get; set; }
    public int Team { get; set; }
    public int Flight { get; set; }
    public int Ohc { get; set; }
    public decimal HcIx { get; set; }
}

private async void writeJason_Click(object sender, RoutedEventArgs e)
{
    try
    {
        await writeJsonAsync();
    }
    catch
    {

    }
}

private async void readJason_Click(object sender, RoutedEventArgs e)
{
    await deserializerJsonAsync();
}

private List<Player> buildObjectGraph()
{
    var players = new List<Player>();
    players.Add(new Player() {MemberId=1, Gender=0, LName="Player", FName="New", Tee=1, Team=0, Flight=0, Ohc=0, HcIx=0M});
    players.Add(new Player() {MemberId=2, Gender=0, LName="Draw", FName="Blind", Tee=1, Team=0, Flight=0, Ohc=0, HcIx=0M});
    players.Add(new Player() {MemberId=3, Gender=0, LName="Moles", FName="Mike", Tee=1, Team=4, Flight=1, Ohc=2, HcIx=2.0M});
    players.Add(new Player() {MemberId=4, Gender=0, LName="Broemeling", FName="Donald", Tee=1, Team=4, Flight=2, Ohc=10, HcIx=10.0M});
    players.Add(new Player() {MemberId=5, Gender=0, LName="Nelson", FName="David", Tee=1, Team=4, Flight=3, Ohc=11, HcIx=11.7M});
    players.Add(new Player() {MemberId=6, Gender=0, LName="Moles", FName="David", Tee=1, Team=4, Flight=4, Ohc=20, HcIx=20.0M});
    players.Add(new Player() {MemberId=7, Gender=0, LName="Player", FName="A", Tee=1, Team=1, Flight=1, Ohc=0, HcIx=0M});
    players.Add(new Player() {MemberId=8, Gender=0, LName="Player", FName="B", Tee=1, Team=1, Flight=2, Ohc=0, HcIx=0M});
    players.Add(new Player() {MemberId=9, Gender=0, LName="Player", FName="C", Tee=1, Team=1, Flight=3, Ohc=0, HcIx=0M});
    players.Add(new Player() {MemberId=10,Gender=0, LName="Player", FName="D", Tee=1, Team=1, Flight=4, Ohc=0, HcIx=0M});
    return players;
}
private async Task writeJsonAsync()
{
    var allPlayers = buildObjectGraph();
    var serializer = new DataContractJsonSerializer(typeof(List<Player>));
    //using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
    //    JSONFILENAME,
    //    CreationCollisionOption.ReplaceExisting))
    using (var stream = await ApplicationData.Current.LocalFolder.OpenStreamForWriteAsync(
    JSONFILENAME, CreationCollisionOption.ReplaceExisting))
    {
        serializer.WriteObject(stream, allPlayers);
    }
    resultTextBlock.Text = "Write succeeded";
}
dbc
  • 104,963
  • 20
  • 228
  • 340

1 Answers1

0

You don't need to mark List<Player>() with CollectionDataContractAttribute. Instead, you need to make the Player class be serializable by DataContractJsonSerializer, as described by the documentation: Types Supported by the Data Contract Serializer.

The easiest solution is to mark Player with the Serializable attribute:

[Serializable]
class Player
{
    // Remainder unchanged
}

Alternatively you could annotate it with [DataContract] and [DataMember] attributes:

[DataContract]
class Player
{
    [DataMember]
    public int MemberId { get; set; }
    [DataMember]
    public int Gender { get; set; }
    [DataMember]
    public string LName { get; set; }
    [DataMember]
    public string FName { get; set; }
    [DataMember]
    public int Tee { get; set; }
    [DataMember]
    public int Team { get; set; }
    [DataMember]
    public int Flight { get; set; }
    [DataMember]
    public int Ohc { get; set; }
    [DataMember]
    public decimal HcIx { get; set; }
}

Either way, your List<Player> will be serializable by DataContractJsonSerializer.

Note that data contract serialization is opt-in so you must mark every property or field to be serialized with [DataMember].

dbc
  • 104,963
  • 20
  • 228
  • 340
  • @DaveNelson - You're welcome. If the question is answered, please do [mark it as such](https://meta.stackexchange.com/questions/147531/how-mark-my-question-as-answered-on-stackoverflow). – dbc Apr 30 '15 at 16:01