public override RattlerTransport ReadJson(JsonReader reader, Type objectType, RattlerTransport existingValue, bool hasExistingValue, JsonSerializer serializer ) { if (reader.TokenType.Equals(JsonToken.StartObject)) { JObject obj = JObject.Load(reader); if (obj["type"] != null) { string rawType = obj["type"].Value <string>(); RattlerTransportType type = RattlerTransportType.ofValue(rawType); int id = obj["id"] != null ? obj["id"].Value <int>() : 0; string name = obj["name"] != null ? obj["name"].Value <string>() : "unnamed"; int capacity = obj["capacity"] != null ? obj["capacity"].Value <int>() : 1; double averageSpeed = obj["average-speed"] != null ? obj["average-speed"].Value <double>() : 1; long[] stationsId = obj["stations"] != null?JsonConvert.DeserializeObject <long[]>(obj["stations"].ToString()) : new long[] { }; RattlerTransport transport = null; if (type.Equals(RattlerTransportType.METRO)) { transport = new Metro(name); } else if (type.Equals(RattlerTransportType.TRAM)) { transport = new Tram(name); } else if (type.Equals(RattlerTransportType.TRAIN)) { transport = new Train(name); } else if (type.Equals(RattlerTransportType.EXPRESS_TRAIN)) { transport = new ExpressTrain(name); } if (transport != null) { transport.id = id; transport.capacity = capacity; transport.averageSpeed = averageSpeed; addStations(stationsId, transport); } return(transport); } } throw new JsonException("Неудалось прочесть"); }
public void demoData() { SimpleRattlerStation <Metro> station1 = new SimpleRattlerStation <Metro>("Yaroslavl", RattlerTransportType.METRO); SimpleRattlerStation <Metro> station2 = new SimpleRattlerStation <Metro>("Rybinsk", RattlerTransportType.METRO); core.stationService.addStation(station1); core.stationService.addStation(station2); Metro metro = new Metro("red way"); core.transportService.addTransport(metro); Train train = new Train("11c"); core.transportService.addTransport(train); LinkStation link = new LinkStation(station1, station2, 10); core.stationService.addLink(link); metro.addStation(station1); metro.addStation(station2); }