New LinearColorEntity, string serialization fixed

This commit is contained in:
barsdeveloper
2022-05-11 21:01:54 +02:00
parent 3c643f0c6a
commit e416591784
20 changed files with 707 additions and 125 deletions

View File

@@ -7,6 +7,9 @@ import SerializerFactory from "./SerializerFactory"
import TypeInitialization from "../entity/TypeInitialization"
import Utility from "../Utility"
/**
* @template {IEntity} T
*/
export default class ISerializer {
static grammar = Parsimmon.createLanguage(new Grammar())
@@ -20,21 +23,66 @@ export default class ISerializer {
this.attributeKeyPrinter = attributeKeyPrinter ?? (k => k.join("."))
}
writeValue(value, fullKey = undefined) {
/**
* @param {String} value
* @returns {T}
*/
deserialize(value) {
return this.read(value)
}
/**
* @param {T} object
* @param {Boolean} insideString
* @returns {String}
*/
serialize(object, insideString) {
insideString ||= object.isShownAsString()
let result = this.write(object, insideString)
if (object.isShownAsString()) {
result = `"${result}"`
}
return result
}
/**
* @param {String} value
* @returns {T}
*/
read(value) {
throw new Error("Not implemented")
}
/**
* @param {T} object
* @param {Boolean} insideString
* @returns {String}
*/
write(object, insideString) {
throw new Error("Not implemented")
}
/**
* @param {String[]} fullKey
* @param {Boolean} insideString
*/
writeValue(value, fullKey, insideString) {
if (value === null) {
return "()"
}
const serialize = v => SerializerFactory.getSerializer(Utility.getType(v)).write(v)
const serialize = v => SerializerFactory.getSerializer(Utility.getType(v)).serialize(v)
// This is an exact match (and not instanceof) to hit also primitive types (by accessing value.constructor they are converted to objects automatically)
switch (value?.constructor) {
case Function:
return this.writeValue(value(), fullKey)
return this.writeValue(value(), fullKey, insideString)
case Boolean:
return Utility.FirstCapital(value.toString())
case Number:
return value.toString()
case String:
return `"${Utility.encodeString(value)}"`
return insideString
? `\\"${Utility.encodeString(value)}\\"`
: `"${Utility.encodeString(value)}"`
}
if (value instanceof Array) {
return `(${value.map(v => serialize(v) + ",").join("")})`
@@ -47,25 +95,26 @@ export default class ISerializer {
/**
* @param {String[]} key
* @param {Object} object
* @param {Boolean} insideString
* @returns {String}
*/
subWrite(key, object) {
subWrite(key, object, insideString) {
let result = ""
let fullKey = key.concat("")
const last = fullKey.length - 1
for (const property of Object.getOwnPropertyNames(object)) {
fullKey[last] = property
const value = object[property]
if (object[property]?.constructor === Object) {
if (value?.constructor === Object) {
// Recursive call when finding an object
result += (result.length ? this.separator : "")
+ this.subWrite(fullKey, value)
+ this.subWrite(fullKey, value, insideString)
} else if (value !== undefined && this.showProperty(object, fullKey, value)) {
result += (result.length ? this.separator : "")
+ this.prefix
+ this.attributeKeyPrinter(fullKey)
+ this.attributeValueConjunctionSign
+ this.writeValue(value, fullKey)
+ this.writeValue(value, fullKey, insideString)
}
}
if (this.trailingSeparator && result.length && fullKey.length === 1) {