Serialization fixed

This commit is contained in:
barsdeveloper
2021-11-08 22:28:26 +01:00
parent ad8c34cdab
commit 39e26bc0c2
12 changed files with 195 additions and 48 deletions

View File

@@ -3,9 +3,9 @@ import Serializer from "./Serializer"
export default class GeneralSerializer extends Serializer {
constructor(keyword, entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter) {
constructor(wrap, entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter) {
super(entityType, prefix, separator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter)
this.keyword = keyword ?? ""
this.wrap = wrap ?? (v => `(${v})`)
}
read(value) {
@@ -19,7 +19,7 @@ export default class GeneralSerializer extends Serializer {
}
write(object) {
let result = `${this.keyword}(${this.subWrite([], object)})`
let result = this.wrap(this.subWrite([], object))
return result
}
}

View File

@@ -8,6 +8,7 @@ import Parsimmon from "parsimmon"
import PinEntity from "../entity/PinEntity"
import PinReferenceEntity from "../entity/PinReferenceEntity"
import Utility from "../Utility"
import PathSymbol from "../entity/primitive/PathSymbol"
let P = Parsimmon
@@ -24,8 +25,8 @@ export default class Grammar {
String = _ => P.regex(/(?:[^"\\]|\\")*/).wrap(P.string('"'), P.string('"')).desc('string (with possibility to escape the quote using \")')
Word = _ => P.regex(/[a-zA-Z]+/).desc("a word")
Guid = _ => P.regex(/[0-9a-zA-Z]{32}/).map(v => new Guid(v)).desc("32 digit hexadecimal (accepts all the letters for safety) value")
PathSymbol = _ => P.regex(/[0-9a-zA-Z_]+/)
ReferencePath = r => P.seq(P.string("/"), r.PathSymbol.sepBy1(P.string(".")).tieWith("."))
PathSymbol = _ => P.regex(/[0-9a-zA-Z_]+/).map(v => new PathSymbol(v))
ReferencePath = r => P.seq(P.string("/"), r.PathSymbol.map(v => v.toString()).sepBy1(P.string(".")).tieWith("."))
.tie()
.atLeast(2)
.tie()

View File

@@ -49,7 +49,7 @@ export default class ObjectSerializer extends Serializer {
* @returns
*/
write(object) {
let result = `Begin Object Class=${object.Class} Name=${object.Name}
let result = `Begin Object Class=${object.Class} Name="${object.Name}"
${this.subWrite([], object)
+ object
.CustomProperties.map(pin => this.separator + this.prefix + "CustomProperties " + SerializerFactory.getSerializer(PinEntity).write(pin))

View File

@@ -23,6 +23,7 @@ export default class Serializer {
if (value === null) {
return "()"
}
const serialize = v => SerializerFactory.getSerializer(Utility.getType(v)).write(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:
@@ -34,8 +35,11 @@ export default class Serializer {
case String:
return `"${value}"`
}
if (value instanceof Array) {
return `(${value.map(v => serialize(v) + ",")})`
}
if (value instanceof Entity) {
return SerializerFactory.getSerializer(Utility.getType(value)).write(value)
return serialize(value)
}
if (value instanceof Primitive) {
return value.toString()
@@ -51,7 +55,8 @@ export default class Serializer {
const value = object[property]
if (object[property]?.constructor === Object) {
// Recursive call when finding an object
result += this.subWrite(fullKey, value, this.prefix, this.separator)
result += (result.length ? this.separator : "")
+ this.subWrite(fullKey, value)
} else if (this.showProperty(fullKey, value)) {
result += (result.length ? this.separator : "")
+ this.prefix
@@ -60,7 +65,7 @@ export default class Serializer {
+ this.writeValue(value)
}
}
if (this.trailingSeparator && result.length) {
if (this.trailingSeparator && result.length && fullKey.length === 0) {
// append separator at the end if asked and there was printed content
result += this.separator
}