1 using System;
2 using System.Collections;
3 using System.Json;
4 using Vehicles;
5
6 namespace Vehicles
7 {
8 [json="true"]
9 public abstract class Vehicle
10 {
11 public nothrow Vehicle(double speed_) : speed(speed_)
12 {
13 }
14 public virtual default ~Vehicle();
15 public inline nothrow double Speed() const
16 {
17 return speed;
18 }
19 private double speed;
20 }
21
22 [json="true"]
23 public class Bicycle : Vehicle
24 {
25 public nothrow Bicycle(const string& brand_, double speed_) : base(speed_), brand(brand_)
26 {
27 }
28 public inline nothrow string& Brand() const
29 {
30 return brand;
31 }
32 private string brand;
33 }
34
35 [json="true"]
36 public class Car : Vehicle
37 {
38 public nothrow Car(const string& model_, const string& registrationNumber_, double speed_) : base(speed_), model(model_), registrationNumber(registrationNumber_)
39 {
40 }
41 public inline nothrow const string& Model() const
42 {
43 return model;
44 }
45 public inline nothrow const string& RegistrationNumber() const
46 {
47 return registrationNumber;
48 }
49 private string model;
50 private string registrationNumber;
51 }
52
53 [json="true"]
54 public class Truck : Car
55 {
56 public nothrow Truck(const string& model_, const string& registrationNumber_, double speed_, int weight_) : base(model_, registrationNumber_, speed_), weight(weight_)
57 {
58 }
59 public inline nothrow int Weight() const
60 {
61 return weight;
62 }
63 private int weight;
64 }
65 }
66
67 public void SerializeOneVehicle()
68 {
69 UniquePtr<Vehicle> porsche(new Car("Porsche", "ABC-123", 200));
70 UniquePtr<JsonValue> porscheJsonValue = ToJson(porsche);
71 string porscheJsonStr = porscheJsonValue->ToString();
72 Console.WriteLine(porscheJsonStr);
73
74 UniquePtr<JsonValue> parsedPorscheJsonValue = ParseJson(porscheJsonStr);
75 UniquePtr<Vehicle> parsedVehicle;
76 FromJson(parsedPorscheJsonValue.Get(), parsedVehicle);
77 Car* parsedPorsche = parsedVehicle.Get() as Car*;
78 if (parsedPorsche != null)
79 {
80 Console.Out() << parsedPorsche->Model() << ": " << parsedPorsche->RegistrationNumber() << " " << parsedPorsche->Speed() << endl();
81 }
82 }
83
84 public void SerializeListOfVehicles()
85 {
86 List<UniquePtr<Vehicle>> vehicles;
87 vehicles.Add(UniquePtr<Vehicle>(new Truck("Volvo", "XYZ-987", 100, 1000)));
88 vehicles.Add(UniquePtr<Vehicle>(new Car("Peugeot", "PRQ-567", 150)));
89 vehicles.Add(UniquePtr<Vehicle>(new Bicycle("Pegoretti", 30)));
90 UniquePtr<JsonValue> vehiclesJsonValue = ToJson(vehicles);
91 string vehiclesJsonStr = vehiclesJsonValue->ToString();
92 Console.WriteLine(vehiclesJsonStr);
93
94 UniquePtr<JsonValue> parsedVehiclesJsonValue = ParseJson(vehiclesJsonStr);
95 List<UniquePtr<Vehicle>> parsedVehicles;
96 FromJson(parsedVehiclesJsonValue.Get(), parsedVehicles);
97 for (const UniquePtr<Vehicle>& vehicle : parsedVehicles)
98 {
99 if (vehicle.Get() is Truck*)
100 {
101 Truck* truck = cast<Truck*>(vehicle.Get());
102 Console.Out() << truck->Model() << ": " << truck->RegistrationNumber() << " " << truck->Speed() << " " << truck->Weight() << endl();
103 }
104 else if (vehicle.Get() is Car*)
105 {
106 Car* car = cast<Car*>(vehicle.Get());
107 Console.Out() << car->Model() << ": " << car->RegistrationNumber() << " " << car->Speed() << endl();
108 }
109 else if (vehicle.Get() is Bicycle*)
110 {
111 Bicycle* bicycle = cast<Bicycle*>(vehicle.Get());
112 Console.Out() << bicycle->Brand() << ": " << bicycle->Speed() << endl();
113 }
114 }
115 }
116
117 internal int main(int argc, const char** argv)
118 {
119 try
120 {
121 bool one = false;
122 for (int i = 1; i < argc; ++i;)
123 {
124 string arg = argv[i];
125 if (arg == "1")
126 {
127 one = true;
128 }
129 }
130 if (one)
131 {
132 SerializeOneVehicle();
133 }
134 else
135 {
136 SerializeListOfVehicles();
137 }
138 }
139 catch (const Exception& ex)
140 {
141 Console.Error() << ex.Message() << endl();
142 return 1;
143 }
144 return 0;
145 }