| 
									
										
										
										
											2020-01-28 15:55:22 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-23 02:32:23 +08:00
										 |  |  | /** @typedef {import("./ObjectMiddleware").ObjectDeserializerContext} ObjectDeserializerContext */ | 
					
						
							|  |  |  | /** @typedef {import("./ObjectMiddleware").ObjectSerializerContext} ObjectSerializerContext */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-28 15:55:22 +08:00
										 |  |  | class ArraySerializer { | 
					
						
							| 
									
										
										
										
											2023-05-23 02:32:23 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @template T | 
					
						
							|  |  |  | 	 * @param {T[]} array array | 
					
						
							|  |  |  | 	 * @param {ObjectSerializerContext} context context | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	serialize(array, context) { | 
					
						
							|  |  |  | 		context.write(array.length); | 
					
						
							|  |  |  | 		for (const item of array) context.write(item); | 
					
						
							| 
									
										
										
										
											2020-01-28 15:55:22 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-23 02:32:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @template T | 
					
						
							|  |  |  | 	 * @param {ObjectDeserializerContext} context context | 
					
						
							|  |  |  | 	 * @returns {T[]} array | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	deserialize(context) { | 
					
						
							|  |  |  | 		/** @type {number} */ | 
					
						
							|  |  |  | 		const length = context.read(); | 
					
						
							|  |  |  | 		/** @type {T[]} */ | 
					
						
							| 
									
										
										
										
											2020-01-28 15:55:22 +08:00
										 |  |  | 		const array = []; | 
					
						
							|  |  |  | 		for (let i = 0; i < length; i++) { | 
					
						
							| 
									
										
										
										
											2023-05-23 02:32:23 +08:00
										 |  |  | 			array.push(context.read()); | 
					
						
							| 
									
										
										
										
											2020-01-28 15:55:22 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return array; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = ArraySerializer; |