mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:50:33 +08:00
39 lines
1.1 KiB
JavaScript
Executable File
39 lines
1.1 KiB
JavaScript
Executable File
// @ts-check
|
|
|
|
import Grammar from "./Grammar"
|
|
import ISerializer from "./ISerializer"
|
|
|
|
export default class GeneralSerializer extends ISerializer {
|
|
|
|
constructor(wrap, entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter) {
|
|
wrap = wrap ?? (v => `(${v})`)
|
|
super(entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter)
|
|
this.wrap = wrap
|
|
}
|
|
|
|
/**
|
|
* @template T
|
|
* @param {String} value
|
|
* @returns {T}
|
|
*/
|
|
read(value) {
|
|
let grammar = Grammar.getGrammarForType(ISerializer.grammar, this.entityType)
|
|
const parseResult = grammar.parse(value)
|
|
if (!parseResult.status) {
|
|
console.error("Error when trying to parse the entity " + this.entityType.prototype.constructor.name)
|
|
return parseResult
|
|
}
|
|
return parseResult.value
|
|
}
|
|
|
|
/**
|
|
* @template T
|
|
* @param {T} object
|
|
* @returns {String}
|
|
*/
|
|
write(object) {
|
|
let result = this.wrap(this.subWrite([], object))
|
|
return result
|
|
}
|
|
}
|