Skip to content

Commit

Permalink
[29] Add the ability to order features during the serialization
Browse files Browse the repository at this point in the history
Bug: #29
  • Loading branch information
vrichard12 authored and sbegaudeau committed Jan 11, 2024
1 parent e11bdfd commit 8be6444
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.Map;

import org.eclipse.emf.common.util.URI;
Expand Down Expand Up @@ -418,6 +419,12 @@ interface IEObjectHandler {
*/
String OPTION_ID_MANAGER = "OPTION_ID_MANAGER"; //$NON-NLS-1$

/**
* An option to provide a {@link Comparator} of {@link EStructuralFeature} to determine the order of the attributes
* in the serialized Json.
*/
Object OPTION_SAVE_FEATURES_ORDER_COMPARATOR = "OPTION_SAVE_FEATURES_ORDER_COMPARATOR"; //$NON-NLS-1$

/**
* Associate an ID to the {@link EObject}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.EMap;
Expand Down Expand Up @@ -871,10 +873,18 @@ private JsonElement createJsonHeader() {
* The EObject to serialize
* @return A JsonObject containing all the properties of the given object
*/
@SuppressWarnings("unchecked")
private JsonObject serializeEAllStructuralFeatures(EObject eObject) {
JsonObject properties = new JsonObject();
EClass eClass = eObject.eClass();
EList<EStructuralFeature> eAllStructuralFeatures = eClass.getEAllStructuralFeatures();
List<EStructuralFeature> eAllStructuralFeatures = eClass.getEAllStructuralFeatures();

Object orderFeatures = this.options.get(JsonResource.OPTION_SAVE_FEATURES_ORDER_COMPARATOR);
if (orderFeatures instanceof Comparator<?>) {
eAllStructuralFeatures = eAllStructuralFeatures.stream() //
.sorted((Comparator<? super EStructuralFeature>) orderFeatures) //
.collect(Collectors.toList());
}

for (EStructuralFeature eStructuralFeature : eAllStructuralFeatures) {
if (this.shouldSerialize(eObject, eStructuralFeature)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;

import org.eclipse.emf.common.util.BasicEList;
Expand Down Expand Up @@ -450,4 +451,23 @@ public void postLoad(JsonResource resource, InputStream inputStream, Map<?, ?> l
this.testSave("SimpleModelWithOnlyDefaultValueForResourceHandlerPreSavingTest.xmi"); //$NON-NLS-1$

}

/**
* Test the serialization with feature order comparator.
*/
@Test
public void testSerializationWithFeatureOrderComparator() {
// A comparator to sort the feature on their name length.
Comparator<EStructuralFeature> featuresComparator = new Comparator<EStructuralFeature>() {
@Override
public int compare(EStructuralFeature o1, EStructuralFeature o2) {
return o1.getName().length() - o2.getName().length();
}
};

this.options.put(JsonResource.OPTION_SAVE_FEATURES_ORDER_COMPARATOR, featuresComparator);
this.testSave("TestSerializationWithFeatureOrderComparator.xmi"); //$NON-NLS-1$

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"json": {
"version": "1.0",
"encoding": "utf-8"
},
"ns": {
"nodes": "http://www.obeo.fr/EMFJson"
},
"schemaLocation": {
"http://www.obeo.fr/EMFJson": "../../../nodes.ecore"
},
"content": [
{
"eClass": "nodes:NodeSingleValueAttribute",
"data": {
"singleIntAttribute": 12,
"singleStringAttribute": "String value",
"singleBooleanAttribute": true
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="ASCII"?>
<nodes:NodeSingleValueAttribute
xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:nodes="http://www.obeo.fr/EMFJson"
xsi:schemaLocation="http://www.obeo.fr/EMFJson ../../../nodes.ecore"
singleBooleanAttribute="true"
singleIntAttribute="12"
singleStringAttribute="String value"/>

0 comments on commit 8be6444

Please sign in to comment.