use of org.finos.legend.pure.runtime.java.extension.external.json.shared.JsonSerializationCache in project legend-pure by finos.
the class ToJson method execute.
@Override
public CoreInstance execute(ListIterable<? extends CoreInstance> params, final Stack<MutableMap<String, CoreInstance>> resolvedTypeParameters, final Stack<MutableMap<String, CoreInstance>> resolvedMultiplicityParameters, final VariableContext variableContext, final CoreInstance functionExpressionToUseInStack, final Profiler profiler, final InstantiationContext instantiationContext, final ExecutionSupport executionSupport, Context context, final ProcessorSupport processorSupport) throws PureExecutionException {
ListIterable<? extends CoreInstance> pureObjects = Instance.getValueForMetaPropertyToManyResolved(params.get(0), M3Properties.values, processorSupport);
if (pureObjects.getFirst() instanceof SimpleCoreInstance && !((SimpleCoreInstance) pureObjects.getFirst()).getClassifier().getName().equals("Map")) {
pureObjects = pureObjects.collect(SIMPLE_CORE_INSTANCE_TO_ANY);
}
CoreInstance jsonConfig = Instance.getValueForMetaPropertyToOneResolved(params.get(1), M3Properties.values, processorSupport);
String typeKeyName = PrimitiveUtilities.getStringValue(jsonConfig.getValueForMetaPropertyToOne("typeKeyName"));
boolean includeType = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("includeType"), false);
boolean fullyQualifiedTypePath = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("fullyQualifiedTypePath"), false);
boolean serializeQualifiedProperties = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("serializeQualifiedProperties"), false);
String dateTimeFormat = PrimitiveUtilities.getStringValue(jsonConfig.getValueForMetaPropertyToOne("dateTimeFormat"), null);
boolean serializePackageableElementName = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("serializePackageableElementName"), false);
boolean removePropertiesWithEmptyValues = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("removePropertiesWithEmptyValues"), false);
boolean serializeMultiplicityAsNumber = PrimitiveUtilities.getBooleanValue(jsonConfig.getValueForMetaPropertyToOne("serializeMultiplicityAsNumber"), false);
String encryptionKey = PrimitiveUtilities.getStringValue(jsonConfig.getValueForMetaPropertyToOne("encryptionKey"), null);
String decryptionKey = PrimitiveUtilities.getStringValue(jsonConfig.getValueForMetaPropertyToOne("decryptionKey"), null);
ListIterable<? extends CoreInstance> encryptionStereotypes = jsonConfig.getValueForMetaPropertyToMany("encryptionStereotypes");
ListIterable<? extends CoreInstance> decryptionStereotypes = jsonConfig.getValueForMetaPropertyToMany("decryptionStereotypes");
String json = JsonSerializer.toJson((ListIterable<? extends Any>) pureObjects, processorSupport, new JsonSerializationContext<CoreInstance, CoreInstance>(new JsonSerializationCache(), functionExpressionToUseInStack.getSourceInformation(), processorSupport, new Stack(), typeKeyName, includeType, fullyQualifiedTypePath, serializeQualifiedProperties, dateTimeFormat, serializePackageableElementName, removePropertiesWithEmptyValues, serializeMultiplicityAsNumber, encryptionKey, encryptionStereotypes, decryptionKey, decryptionStereotypes) {
@Override
protected Object extractPrimitiveValue(Object potentiallyWrappedPrimitive) {
Object val = potentiallyWrappedPrimitive instanceof PrimitiveCoreInstance ? ((PrimitiveCoreInstance) potentiallyWrappedPrimitive).getValue() : potentiallyWrappedPrimitive;
val = potentiallyWrappedPrimitive instanceof FloatCoreInstance && val instanceof BigDecimal ? ((BigDecimal) val).doubleValue() : val;
return val;
}
@Override
protected Object getValueForProperty(CoreInstance pureObject, Property property, String className) {
CoreInstance res = ToJson.this.functionExecution.executeProperty(property, true, resolvedTypeParameters, resolvedMultiplicityParameters, getParentOrEmptyVariableContext(variableContext), profiler, Lists.immutable.with(ValueSpecificationBootstrap.wrapValueSpecification(pureObject, true, processorSupport)), functionExpressionToUseInStack, instantiationContext, executionSupport);
if (res instanceof InstanceValue) {
RichIterable values = ((InstanceValue) res)._values();
return (values.getFirst() instanceof SimpleCoreInstance && !((SimpleCoreInstance) values.getFirst()).getClassifier().getName().equals("Map")) ? values.collect(SIMPLE_CORE_INSTANCE_TO_ANY) : values;
}
return res;
}
@Override
protected Object evaluateQualifiedProperty(CoreInstance pureObject, QualifiedProperty qualifiedProperty, Type type, Multiplicity multiplicity, String propertyName) {
Stack empty = new Stack();
CoreInstance res = ToJson.this.functionExecution.executeFunction(false, qualifiedProperty, Lists.immutable.with(ValueSpecificationBootstrap.wrapValueSpecification(pureObject, true, processorSupport)), empty, empty, getParentOrEmptyVariableContextForLambda(variableContext, qualifiedProperty), functionExpressionToUseInStack, profiler, instantiationContext, executionSupport);
if (res instanceof InstanceValue) {
return ((InstanceValue) res)._values();
}
return res;
}
@Override
protected CoreInstance getClassifier(CoreInstance pureObject) {
return pureObject.getClassifier();
}
@Override
protected RichIterable<CoreInstance> getMapKeyValues(CoreInstance pureObject) {
ListIterable<CoreInstance> newPureObjects = Lists.immutable.with(ValueSpecificationBootstrap.wrapValueSpecification(pureObject, true, processorSupport));
return KeyValues.executeMap(repository, newPureObjects, processorSupport);
}
}, functionExpressionToUseInStack.getSourceInformation());
return ValueSpecificationBootstrap.newStringLiteral(this.repository, json, processorSupport);
}
Aggregations