2025-11-23 14:49:37 +08:00
|
|
|
/**
|
|
|
|
|
* JSON asset loader
|
|
|
|
|
* JSON资产加载器
|
|
|
|
|
*/
|
|
|
|
|
|
2025-12-03 22:15:22 +08:00
|
|
|
import { AssetType } from '../types/AssetTypes';
|
|
|
|
|
import { IAssetLoader, IJsonAsset, IAssetParseContext } from '../interfaces/IAssetLoader';
|
|
|
|
|
import { IAssetContent, AssetContentType } from '../interfaces/IAssetReader';
|
2025-11-23 14:49:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* JSON loader implementation
|
|
|
|
|
* JSON加载器实现
|
|
|
|
|
*/
|
|
|
|
|
export class JsonLoader implements IAssetLoader<IJsonAsset> {
|
|
|
|
|
readonly supportedType = AssetType.Json;
|
|
|
|
|
readonly supportedExtensions = ['.json', '.jsonc'];
|
2025-12-03 22:15:22 +08:00
|
|
|
readonly contentType: AssetContentType = 'text';
|
2025-11-23 14:49:37 +08:00
|
|
|
|
|
|
|
|
/**
|
2025-12-03 22:15:22 +08:00
|
|
|
* Parse JSON from text content.
|
|
|
|
|
* 从文本内容解析JSON。
|
2025-11-23 14:49:37 +08:00
|
|
|
*/
|
2025-12-03 22:15:22 +08:00
|
|
|
async parse(content: IAssetContent, _context: IAssetParseContext): Promise<IJsonAsset> {
|
|
|
|
|
if (!content.text) {
|
|
|
|
|
throw new Error('JSON content is empty');
|
2025-11-23 14:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-03 22:15:22 +08:00
|
|
|
return {
|
|
|
|
|
data: JSON.parse(content.text)
|
|
|
|
|
};
|
2025-11-23 14:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Dispose loaded asset
|
|
|
|
|
* 释放已加载的资产
|
|
|
|
|
*/
|
|
|
|
|
dispose(asset: IJsonAsset): void {
|
|
|
|
|
(asset as any).data = null;
|
|
|
|
|
}
|
|
|
|
|
}
|