public override LinkStation ReadJson( JsonReader reader, Type objectType, LinkStation existingValue, bool hasExistingValue, JsonSerializer serializer ) { if (reader.TokenType.Equals(JsonToken.StartObject)) { JObject obj = JObject.Load(reader); string rawType = obj["type"].Value <string>(); RattlerTransportType type = RattlerTransportType.ofValue(rawType); int id1 = obj["station1"] != null ? obj["station1"].Value <int>() : 0; int id2 = obj["station2"] != null ? obj["station2"].Value <int>() : 0; double distance = obj["station2"] != null ? obj["distance"].Value <double>() : 0; RattlerStation station1 = core.stationService.getById(id1); RattlerStation station2 = core.stationService.getById(id2); LinkStation linkStation = new LinkStation(station1, station2, distance); return(linkStation); } throw new JsonException("Неудалось прочесть"); }
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 override RattlerStation ReadJson( JsonReader reader, Type objectType, RattlerStation existingValue, bool hasExistingValue, JsonSerializer serializer ) { if (reader.TokenType.Equals(JsonToken.StartObject)) { JObject obj = JObject.Load(reader); 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"; if (type.Equals(RattlerTransportType.COMPLEX)) { ComplexRattlerStation station = new ComplexRattlerStation(name); station.id = id; station.name = name; return(station); } else if (type.Equals(RattlerTransportType.METRO)) { SimpleRattlerStation <Metro> station = new SimpleRattlerStation <Metro>(name, type); station.id = id; station.name = name; return(station); } else if (type.Equals(RattlerTransportType.TRAIN)) { SimpleRattlerStation <Train> station = new SimpleRattlerStation <Train>(name, type); station.id = id; station.name = name; return(station); } else if (type.Equals(RattlerTransportType.TRAM)) { SimpleRattlerStation <Tram> station = new SimpleRattlerStation <Tram>(name, type); station.id = id; station.name = name; return(station); } else if (type.Equals(RattlerTransportType.EXPRESS_TRAIN)) { SimpleRattlerStation <ExpressTrain> station = new SimpleRattlerStation <ExpressTrain>(name, type); station.id = id; station.name = name; return(station); } } throw new JsonException("Неудалось прочесть"); }