Several serialization and deserialization fixes

This commit is contained in:
barsdeveloper
2023-04-15 12:20:53 +02:00
parent d10589f0bd
commit baf40a9094
17 changed files with 366 additions and 149 deletions

View File

@@ -1,13 +1,14 @@
import Configuration from "../Configuration.js"
import Grammar from "./Grammar.js"
import Serializer from "./Serializer.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import PinEntity from "../entity/PinEntity.js"
import Serializer from "./Serializer.js"
import SerializerFactory from "./SerializerFactory.js"
export default class ObjectSerializer extends Serializer {
constructor() {
super(ObjectEntity, undefined, "\n", false, undefined, k => ` ${k}`)
super(ObjectEntity, undefined, "\n", true, undefined, Serializer.same)
}
showProperty(entity, key) {
@@ -21,6 +22,11 @@ export default class ObjectSerializer extends Serializer {
return super.showProperty(entity, key)
}
/** @param {ObjectEntity} value */
write(value, insideString = false) {
return this.doWrite(value, insideString) + "\n"
}
/** @param {String} value */
doRead(value) {
const parseResult = Grammar.objectEntity.parse(value)
@@ -50,24 +56,48 @@ export default class ObjectSerializer extends Serializer {
doWrite(
entity,
insideString,
indentation = "",
wrap = this.wrap,
attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator,
attributeValueConjunctionSign = this.attributeValueConjunctionSign,
attributeKeyPrinter = this.attributeKeyPrinter
attributeKeyPrinter = this.attributeKeyPrinter,
) {
const moreIndentation = indentation + Configuration.indentation
if (!(entity instanceof ObjectEntity)) {
return super.doWrite(entity, insideString)
return super.doWrite(
entity,
insideString,
indentation,
wrap,
attributeSeparator,
trailingSeparator,
attributeValueConjunctionSign,
key => entity[key] instanceof ObjectEntity ? "" : attributeKeyPrinter(key)
)
}
let result = `Begin Object Class=${entity.Class.path} Name=${this.doWriteValue(entity.Name, insideString)}\n`
+ super.doWrite(entity, insideString)
let result = indentation + "Begin Object"
+ (entity.Class.path ? ` Class=${entity.Class.path}` : "")
+ (entity.Name ? ` Name=${this.doWriteValue(entity.Name, insideString)}` : "")
+ "\n"
+ super.doWrite(
entity,
insideString,
moreIndentation,
wrap,
attributeSeparator,
true,
attributeValueConjunctionSign,
key => entity[key] instanceof ObjectEntity ? "" : attributeKeyPrinter(key)
)
+ entity.CustomProperties.map(pin =>
this.attributeSeparator
+ " CustomProperties "
+ moreIndentation
+ attributeKeyPrinter("CustomProperties ")
+ SerializerFactory.getSerializer(PinEntity).doWrite(pin, insideString)
)
.join("")
+ "\nEnd Object\n"
+ indentation + "End Object"
return result
}
}