mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:50:33 +08:00
String encode/decode
This commit is contained in:
@@ -20,7 +20,7 @@ export default class ISerializer {
|
||||
this.attributeKeyPrinter = attributeKeyPrinter ?? (k => k.join("."))
|
||||
}
|
||||
|
||||
writeValue(value) {
|
||||
writeValue(value, fullKey = undefined) {
|
||||
if (value === null) {
|
||||
return "()"
|
||||
}
|
||||
@@ -28,13 +28,13 @@ export default class ISerializer {
|
||||
// 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())
|
||||
return this.writeValue(value(), fullKey)
|
||||
case Boolean:
|
||||
return Utility.FirstCapital(value.toString())
|
||||
case Number:
|
||||
return value.toString()
|
||||
case String:
|
||||
return `"${value}"`
|
||||
return `"${Utility.encodeString(value)}"`
|
||||
}
|
||||
if (value instanceof Array) {
|
||||
return `(${value.map(v => serialize(v) + ",").join("")})`
|
||||
@@ -65,7 +65,7 @@ export default class ISerializer {
|
||||
+ this.prefix
|
||||
+ this.attributeKeyPrinter(fullKey)
|
||||
+ this.attributeValueConjunctionSign
|
||||
+ this.writeValue(value)
|
||||
+ this.writeValue(value, fullKey)
|
||||
}
|
||||
}
|
||||
if (this.trailingSeparator && result.length && fullKey.length === 1) {
|
||||
|
||||
@@ -46,7 +46,7 @@ export default class ObjectSerializer extends ISerializer {
|
||||
* @param {ObjectEntity} object
|
||||
*/
|
||||
write(object) {
|
||||
let result = `Begin Object Class=${object.Class.path} Name=${this.writeValue(object.Name)}
|
||||
let result = `Begin Object Class=${object.Class.path} Name=${this.writeValue(object.Name, "Name")}
|
||||
${this.subWrite([], object)
|
||||
+ object
|
||||
.CustomProperties.map(pin =>
|
||||
|
||||
20
js/serialization/PinSerializer.js
Executable file
20
js/serialization/PinSerializer.js
Executable file
@@ -0,0 +1,20 @@
|
||||
// @ts-check
|
||||
|
||||
import PinEntity from "../entity/PinEntity"
|
||||
import Utility from "../Utility"
|
||||
import GeneralSerializer from "./GeneralSerializer"
|
||||
|
||||
export default class PinSerializer extends GeneralSerializer {
|
||||
|
||||
constructor() {
|
||||
super(v => `${PinEntity.lookbehind} (${v})`, PinEntity, "", ",", true)
|
||||
}
|
||||
|
||||
writeValue(value, fullKey) {
|
||||
if (value?.constructor === String && fullKey == "DefaultValue") {
|
||||
// @ts-expect-error
|
||||
return `"${Utility.encodeInputString(value)}"`
|
||||
}
|
||||
return super.writeValue(value, fullKey)
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import GeneralSerializer from "./GeneralSerializer"
|
||||
import GuidEntity from "../entity/GuidEntity"
|
||||
import IdentifierEntity from "../entity/IdentifierEntity"
|
||||
import IntegerEntity from "../entity/IntegerEntity"
|
||||
import InvariantTextEntity from "../entity/InvariantTextEntity"
|
||||
import KeyBindingEntity from "../entity/KeyBindingEntity"
|
||||
import LocalizedTextEntity from "../entity/LocalizedTextEntity"
|
||||
import ObjectEntity from "../entity/ObjectEntity"
|
||||
@@ -14,9 +15,9 @@ import ObjectSerializer from "./ObjectSerializer"
|
||||
import PathSymbolEntity from "../entity/PathSymbolEntity"
|
||||
import PinEntity from "../entity/PinEntity"
|
||||
import PinReferenceEntity from "../entity/PinReferenceEntity"
|
||||
import PinSerializer from "./PinSerializer"
|
||||
import SerializerFactory from "./SerializerFactory"
|
||||
import ToStringSerializer from "./ToStringSerializer"
|
||||
import InvariantTextEntity from "../entity/InvariantTextEntity"
|
||||
|
||||
export default function initializeSerializerFactory() {
|
||||
|
||||
@@ -26,8 +27,8 @@ export default function initializeSerializerFactory() {
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
PinEntity,
|
||||
new GeneralSerializer(v => `${PinEntity.lookbehind} (${v})`, PinEntity, "", ",", true)
|
||||
PinEntity,
|
||||
new PinSerializer()
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
|
||||
Reference in New Issue
Block a user