Skip to content

Commit

Permalink
[34] Add the ability to add or retreive custom data
Browse files Browse the repository at this point in the history
Bug: #34
Signed-off-by: Michaël Charfadi <[email protected]>
  • Loading branch information
mcharfadi authored and sbegaudeau committed Apr 10, 2024
1 parent 126649a commit 2a7d161
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.eclipse.sirius.emfjson.resource;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.io.InputStream;
import java.io.OutputStream;
Expand Down Expand Up @@ -495,6 +496,97 @@ public void onCrossReferenceURICreated(EObject eObject, EReference eReference, S
}
}

/**
* An option to provide an IJsonResourceProcessor.
*/
String OPTION_JSON_RESSOURCE_PROCESSOR = "OPTION_JSON_RESSOURCE_PROCESSOR"; //$NON-NLS-1$

/**
* Used to add data to be serialized in the document, or to retrieve data during deserialization.
*
* @author <a href="mailto:[email protected]">Michael Charfadi</a>
*/
interface IJsonResourceProcessor {

/**
* Called when a JsonResource is deserialized.
*
* @param resource
* The JsonResource that is deserialized
* @param jsonObject
* The root jsonObject
*/
void preDeserialization(JsonResource resource, JsonObject jsonObject);

/**
* Called when a JsonResource is serialized.
*
* @param resource
* The JsonResource that is serialized
* @param jsonObject
* The root jsonObject
*/
void postSerialization(JsonResource resource, JsonObject jsonObject);

/**
* Called during the parsing of JsonResources after loading an eObject. As such the eObject will have all his
* features set. The jsonObject is the one that was used to set the features and can be used to retrieve values
* of unknown features.
*
* @param eObject
* the eObject that have been loaded.
* @param jsonObject
* the jsonObject used to load the eObject.
* @param isTopObject
* if the given JsonObject is a top element.
*/
void postObjectLoading(EObject eObject, JsonObject jsonObject, boolean isTopObject);

/**
* Called during the parsing of JsonResource (at loading time). If a feature value has changed since a previous
* version, use this method to return the correct expected value. Return null if it did not change.
*
* @param eObject
* the object containing the feature.
* @param feature
* the feature to set value.
* @param value
* the initial serialized value.
* @return The new value.
*/
Object getValue(EObject eObject, EStructuralFeature feature, Object value);

/**
* Implementation of the interface which does nothing.
*
* @author <a href="mailto:[email protected]">Michael Charfadi</a>
*/
class NoOp implements IJsonResourceProcessor {

@Override
public void preDeserialization(JsonResource resource, JsonObject jsonObject) {
// Do nothing
}

@Override
public void postSerialization(JsonResource resource, JsonObject jsonObject) {
// Do nothing
}

@Override
public Object getValue(EObject eObject, EStructuralFeature feature, Object value) {
// Do nothing
return null;
}

@Override
public void postObjectLoading(EObject eObject, JsonObject jsonObject, boolean isTopObject) {
// Do nothing
}

}
}

/**
* Associate an ID to the {@link EObject}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.emfjson.resource.JsonResource.IEObjectHandler;
import org.eclipse.sirius.emfjson.resource.JsonResource.IJsonResourceProcessor;
import org.eclipse.sirius.emfjson.resource.JsonResource.URIHandler;
import org.eclipse.sirius.emfjson.resource.PackageNotFoundException;

Expand Down Expand Up @@ -134,6 +135,8 @@ public class GsonEObjectDeserializer implements JsonDeserializer<List<EObject>>
*/
private IEObjectHandler eObjectHandler;

private IJsonResourceProcessor jsonResourceProcessor;

/**
* The constructor.
*
Expand All @@ -150,9 +153,15 @@ public GsonEObjectDeserializer(Resource resource, Map<?, ?> options) {
this.options = options;
}

this.jsonResourceProcessor = (IJsonResourceProcessor) this.options.get(JsonResource.OPTION_JSON_RESSOURCE_PROCESSOR);
if (this.jsonResourceProcessor == null) {
this.jsonResourceProcessor = new IJsonResourceProcessor.NoOp();
}

this.helper = (JsonHelper) this.options.get(JsonResource.OPTION_CUSTOM_HELPER);
if (this.helper == null) {
this.helper = new JsonHelper(resource);
this.helper.setJsonResourceProcessor(this.jsonResourceProcessor);
}
if (resource instanceof JsonResource) {
this.resource = (JsonResource) resource;
Expand All @@ -175,6 +184,7 @@ public GsonEObjectDeserializer(Resource resource, Map<?, ?> options) {
} else {
this.extendedMetaData = (ExtendedMetaData) this.options.get(JsonResource.OPTION_EXTENDED_META_DATA);
}

if (this.extendedMetaData != null) {
this.helper.setExtendedMetaData(this.extendedMetaData);
}
Expand Down Expand Up @@ -245,6 +255,8 @@ public List<EObject> deserialize(JsonElement jsonElement, Type type, JsonDeseria
}
}

this.jsonResourceProcessor.preDeserialization(this.resource, jsonRoot);

// json content
this.deserializeContent(jsonRoot);

Expand Down Expand Up @@ -401,7 +413,7 @@ private EObject loadObject(JsonObject object, boolean isTopObject) {
this.resource.setID(eObject, idJsonElement.getAsString());
}
}

this.jsonResourceProcessor.postObjectLoading(eObject, object, isTopObject);
return eObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.emfjson.resource.JsonResource.EStructuralFeaturesFilter;
import org.eclipse.sirius.emfjson.resource.JsonResource.IEObjectHandler;
import org.eclipse.sirius.emfjson.resource.JsonResource.IJsonResourceProcessor;
import org.eclipse.sirius.emfjson.resource.JsonResource.ISerializationListener;
import org.eclipse.sirius.emfjson.resource.JsonResource.ResourceEntityHandler;
import org.eclipse.sirius.emfjson.resource.exception.DanglingHREFException;
Expand Down Expand Up @@ -130,6 +131,10 @@ public class GsonEObjectSerializer implements JsonSerializer<List<EObject>> {

private ISerializationListener serializationListener;

private IJsonResourceProcessor jsonResourceProcessor;

private Resource eResource;

/**
* The constructor.
*
Expand All @@ -141,7 +146,7 @@ public class GsonEObjectSerializer implements JsonSerializer<List<EObject>> {
public GsonEObjectSerializer(Resource resource, Map<?, ?> options) {
super();
Map<?, ?> serializedOptions = options;

this.eResource = resource;
this.resourceEntityHandler = (ResourceEntityHandler) serializedOptions.get(JsonResource.OPTION_RESOURCE_ENTITY_HANDLER);
if (this.resourceEntityHandler instanceof JsonResource.URIHandler && !serializedOptions.containsKey(JsonResource.OPTION_URI_HANDLER)) {
Map<Object, Object> newOptions = new LinkedHashMap<Object, Object>(serializedOptions);
Expand Down Expand Up @@ -174,6 +179,10 @@ public GsonEObjectSerializer(Resource resource, Map<?, ?> options) {
if (this.serializationListener == null) {
this.serializationListener = new ISerializationListener.NoOp();
}
this.jsonResourceProcessor = (IJsonResourceProcessor) serializedOptions.get(JsonResource.OPTION_JSON_RESSOURCE_PROCESSOR);
if (this.jsonResourceProcessor == null) {
this.jsonResourceProcessor = new IJsonResourceProcessor.NoOp();
}

this.declareSchemaLocation = Boolean.TRUE.equals(options.get(JsonResource.OPTION_SCHEMA_LOCATION));

Expand Down Expand Up @@ -202,6 +211,9 @@ public JsonElement serialize(List<EObject> eObjects, Type type, JsonSerializatio
JsonObject jsonObject = new JsonObject();
jsonObject.add(IGsonConstants.JSON, jsonHeader);
jsonObject.add(IGsonConstants.NS, nsHeader);

this.jsonResourceProcessor.postSerialization((JsonResource) this.eResource, jsonObject);

if (schemaLocationHeader != null) {
jsonObject.add(IGsonConstants.SCHEMA_LOCATION, schemaLocationHeader);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.emf.ecore.util.ExtendedMetaData;
import org.eclipse.emf.ecore.util.InternalEList;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.emfjson.resource.JsonResource.IJsonResourceProcessor;
import org.eclipse.sirius.emfjson.resource.JsonResource.URIHandler;
import org.eclipse.sirius.emfjson.resource.exception.DanglingHREFException;

Expand All @@ -58,7 +59,7 @@ public class JsonHelper {
/**
* This class is used to register uri and all prefixes that refers to this uri.
*/
private class PrefixToURI extends BasicEMap<String, String> {
private final class PrefixToURI extends BasicEMap<String, String> {

/**
* .
Expand Down Expand Up @@ -179,6 +180,11 @@ protected void didRemove(Entry<String, String> entry) {
*/
private DanglingHREFException danglingHREFException;

/**
* IJsonResourceProcessor.
*/
private IJsonResourceProcessor jsonResourceProcessor;

/**
* How manage dangling references.
*
Expand Down Expand Up @@ -218,6 +224,15 @@ public Resource getResource() {
return this.resource;
}

/**
* Set the jsonResourceProcessor.
*
* @param jsonResourceProcessor
*/
public void setJsonResourceProcessor(IJsonResourceProcessor jsonResourceProcessor) {
this.jsonResourceProcessor = jsonResourceProcessor;
}

/**
* Set the resource and also its package registry and if its URI should be deresolved.
*
Expand Down Expand Up @@ -593,12 +608,16 @@ public EClassifier getType(EFactory eFactory, String typeName) {
* the value to set
*/
public void setValue(EObject object, EStructuralFeature feature, Object value) {
Object newValue = this.jsonResourceProcessor.getValue(object, feature, value);
if (newValue == null) {
newValue = value;
}
if (feature.isMany()) {
@SuppressWarnings("unchecked")
Collection<Object> collection = (Collection<Object>) object.eGet(feature);
collection.add(value);
collection.add(newValue);
} else {
object.eSet(feature, value);
object.eSet(feature, newValue);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annota
org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable
org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
Expand All @@ -33,6 +34,7 @@ org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled
org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled
org.eclipse.jdt.core.compiler.problem.discouragedReference=warning
org.eclipse.jdt.core.compiler.problem.emptyStatement=warning
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable=ignore
org.eclipse.jdt.core.compiler.problem.fallthroughCase=error
Expand Down Expand Up @@ -89,6 +91,7 @@ org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments=igno
org.eclipse.jdt.core.compiler.problem.redundantSuperinterface=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic=ignore
org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic=ignore
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled
org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=error
org.eclipse.jdt.core.compiler.problem.suppressOptionalErrors=disabled
Expand Down Expand Up @@ -120,7 +123,8 @@ org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning
org.eclipse.jdt.core.compiler.problem.unusedTypeParameter=ignore
org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning
org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning
org.eclipse.jdt.core.compiler.source=1.6
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8
org.eclipse.jdt.core.formatter.align_assignment_statements_on_columns=false
org.eclipse.jdt.core.formatter.align_fields_grouping_blank_lines=2147483647
org.eclipse.jdt.core.formatter.align_type_members_on_columns=false
Expand Down
Loading

0 comments on commit 2a7d161

Please sign in to comment.