Protocols and Dynamic Dispatch
Swift
Hard
protocols
Task «Protocols and Dynamic Dispatch»
Implement a data serialization processing system based on protocols. Define the `Serializable` protocol with the requirement of a `func serialize() -> String` method. Create the structures `User` (with fields name: String, age: Int), `Address` (city: String, street: String), `Product` (id: Int, title: String, price: Double). Each structure must conform to the `Serializable` protocol: return a JSON-like string (e.g., {"name":"John","age":30}). Write a function `func processSerialization<T: Serializable>(_ items: [T]) -> [String]` that takes an array of elements conforming to the protocol and returns an array of serialization strings. The order of elements is preserved. Constraints: strings may contain only Latin letters, spaces, and digits; no escaping is required. Output: each string on a new line.
Input Format
The first line is an integer N (1 ≤ N ≤ 100) — the number of objects. Then for each object: a line with the type (User, Address, Product) and the corresponding fields separated by spaces. For User: name (String) age (Int). For Address: city (String) street (String). For Product: id (Int) title (String) price (Double).
Output Format
N lines, each being the result of serializing the corresponding object in the specified format.
Examples
Example 1
INPUT
2
Address Paris RueDeRivoli
Product 42 Book 12.5
OUTPUT
{"city":"Paris","street":"RueDeRivoli"}
{"id":42,"title":"Book","price":12.5}
Example 2
INPUT
3
2
3
1
OUTPUT
Example 3
INPUT
3
https://httpbin.org/delay/1
https://httpbin.org/delay/2
https://httpbin.org/delay/0.5
1000
OUTPUT
main.swift