jukiduki
jukiduki

Reputation: 39

Deserialize a JSON stream

here's my class

[DataContract]
public class WytypowaneMecze
{
    public WytypowaneMecze() { }
    public WytypowaneMecze(String data, String d_gospodarzy, String d_gosci, String wynik)
    {
        this.Data = data;
        this.D_gospodarzy = d_gospodarzy;
        this.D_gosci = d_gosci;
        this.Wynik = wynik;
    }
    public string Data { get; set; }
    public string D_gospodarzy { get; set; }
    public string D_gosci { get; set; }
    public string Wynik { get; set; }
}

}

that's how i write to file my list wytypowane

    private async void zapiszPlik() 
    {
        string json = "wytypowane.json";
        var serializer = new DataContractJsonSerializer(typeof(List<WytypowaneMecze>));

        var stream = await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForWriteAsync(json, CreationCollisionOption.OpenIfExists);
        using (stream)
        {
            serializer.WriteObject(stream, wytypowane);
        }

    }

but i can't read this...

Additional information: '{}][{},{}][{}][{}][{},{}][{}][{}][{}][{}][{}][{}][{}]' is not a valid JSON primitive. This error can also occur when extraneous data is present after the JSON data.

    private async void odczyt()
    {
        string json = "wytypowane.json";
        List<WytypowaneMecze> lista = new List<WytypowaneMecze>();
        var deserializer = new DataContractJsonSerializer(typeof(List<WytypowaneMecze>));
        var stream = await Windows.ApplicationModel.Package.Current.InstalledLocation.OpenStreamForReadAsync(json);
        using (stream)
        {
            lista = (List<WytypowaneMecze>)deserializer.ReadObject(stream);
        }
    }

Upvotes: 0

Views: 344

Answers (1)

dbc
dbc

Reputation: 117026

You need to mark the properties you want to serialize with the DataMember attribute. That's because you are using the DataContractJsonSerializer and data contracts are opt-in:

Apply the DataMemberAttribute attribute in conjunction with the DataContractAttribute to identify members of a type that are part of a data contract. One of the serializers that can serialize data contracts is the DataContractSerializer.

The data contract model is an "opt-in" model. Applying the DataMemberAttribute to a field or property explicitly specifies that the member value will be serialized. In contrast, the BinaryFormatter serializes public and private fields of a type, and the XmlSerializer serializes only public fields and properties of a type.

Thus:

[DataContract]
public class WytypowaneMecze
{
    public WytypowaneMecze() { }
    public WytypowaneMecze(String data, String d_gospodarzy, String d_gosci, String wynik)
    {
        this.Data = data;
        this.D_gospodarzy = d_gospodarzy;
        this.D_gosci = d_gosci;
        this.Wynik = wynik;
    }
    [DataMember]
    public string Data { get; set; }
    [DataMember]
    public string D_gospodarzy { get; set; }
    [DataMember]
    public string D_gosci { get; set; }
    [DataMember]
    public string Wynik { get; set; }
}

Upvotes: 1

Related Questions