use of org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer in project legend-pure by finos.
the class BinaryModelSourceSerializer method findSerializer.
private ExternalReferenceSerializer findSerializer(CoreInstance instance) {
// Find serializer by instance type
CoreInstance type = instance.getClassifier();
ListIterable<CoreInstance> generalizations = Type.getGeneralizationResolutionOrder(type, this.processorSupport);
for (CoreInstance genl : generalizations) {
String genlPath = PackageableElement.getUserPathForPackageableElement(genl);
ExternalReferenceSerializer serializer = this.serializerLibrary.getSerializer(genlPath);
if (serializer != null) {
return serializer;
}
}
return null;
}
use of org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer in project legend-pure by finos.
the class BinaryModelSourceDeserializer method readOtherExternalReference.
private Reference readOtherExternalReference(int i, Reference[] externalReferences, byte[][] externalReferenceBytes) {
Reference externalReference = externalReferences[i];
if (externalReference == null) {
Reader reader = BinaryReaders.newBinaryReader(externalReferenceBytes[i]);
String serializerType = readStringById(reader);
ExternalReferenceSerializer serializer = this.serializerLibrary.getSerializer(serializerType);
if (serializer == null) {
throw new RuntimeException("Cannot find serializer for type: " + serializerType);
}
externalReference = serializer.deserialize(new BinaryExternalReferenceDeserializationHelper(reader, externalReferences, externalReferenceBytes));
externalReferences[i] = externalReference;
}
return externalReference;
}
use of org.finos.legend.pure.m3.serialization.runtime.binary.reference.ExternalReferenceSerializer in project legend-pure by finos.
the class BinaryModelSourceSerializer method serializeOtherExternalReference.
private void serializeOtherExternalReference(CoreInstance referenceInstance, Writer writer) {
writeOtherExternalReference(referenceInstance, writer);
if (!this.externalOtherReferenceSerializations.containsKey(referenceInstance)) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Writer instanceWriter = BinaryWriters.newBinaryWriter(stream);
ExternalReferenceSerializer serializer = findSerializer(referenceInstance);
if (serializer == null) {
// Couldn't find a serializer
throwUnsupportedExternalReferenceException(referenceInstance);
}
int serializerId = getStringReferenceId(serializer.getTypePath());
instanceWriter.writeInt(serializerId);
try {
serializer.serialize(referenceInstance, new BinaryExternalReferenceSerializationHelper(instanceWriter));
} catch (Exception e) {
// Exception occurred during serialization
StringBuilder message = new StringBuilder("Error serializaing ");
message.append(referenceInstance);
message.append(" with serializer for type ");
message.append(serializer.getTypePath());
SourceInformation sourceInfo = referenceInstance.getSourceInformation();
if (sourceInfo != null) {
sourceInfo.appendMessage(message.append(" (source information: ")).append(')');
}
throw new RuntimeException(message.toString(), e);
}
this.externalOtherReferenceSerializations.put(referenceInstance, stream.toByteArray());
}
}
Aggregations