c# - Json write can not be serialized -
process:
create
player
class.create player
list<player>()
.add players list.
write
json
serial file.when click on
writejson
button receive error:"type `'editplayers.player'` cannot serialized. consider marking `datacontractattribute` attribute, , marking of members want serialized `datamemberattribute` attribute. if type collection, consider marking `collectiondatacontractattribute`.see microsoft .net framework documentation other supported types."
question:
how mark list<player>()
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"; }
you don't need mark list<player>()
collectiondatacontractattribute
. instead, need make player
class serializable datacontractjsonserializer
, described documentation: types supported data contract serializer.
the easiest solution mark player
serializable
attribute:
[serializable] class player { // remainder unchanged }
alternatively annotate [datacontract]
, [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, list<player>
serializable datacontractjsonserializer
.
note data contract serialization opt-in must mark every property or field serialized [datamember]
.
Comments
Post a Comment