From 9ee6461fe4acd52fa696183dd472c3608f645405 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Wed, 22 Nov 2023 17:18:17 +0100 Subject: [PATCH] Add support for Serialization / deserialization of POJO --- .../sirius/emfjson/resource/JsonResource.java | 9 + .../utils/GsonEObjectDeserializer.java | 34 +++- .../emfjson/utils/GsonEObjectSerializer.java | 39 +++- .../internal/unit/load/DataTypeLoadTests.java | 38 ++++ .../ExtendedMetaDataAttributesLoadTests.java | 6 +- .../ExtendedMetaDataReferencesLoadTests.java | 8 +- .../unit/load/JsonHelperDataLoadTests.java | 18 +- .../internal/unit/save/DataTypeSaveTests.java | 38 ++++ .../resources/model/TestPojoDataTypeImpl.java | 166 ++++++++++++++++++ .../src/main/resources/nodes.ecore | 5 + .../NodeMultiValuedAttributePojoDataType.json | 33 ++++ .../NodeMultiValuedAttributePojoDataType.xmi | 11 ++ .../NodeSingleValueAttributePojoDataType.json | 23 +++ .../NodeSingleValueAttributePojoDataType.xmi | 8 + 14 files changed, 422 insertions(+), 14 deletions(-) create mode 100644 tests/org.eclipse.sirius.emfjson.tests/src/main/resources/model/TestPojoDataTypeImpl.java create mode 100644 tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeMultiValuedAttributePojoDataType.json create mode 100644 tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeMultiValuedAttributePojoDataType.xmi create mode 100644 tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeSingleValueAttributePojoDataType.json create mode 100644 tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeSingleValueAttributePojoDataType.xmi diff --git a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/resource/JsonResource.java b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/resource/JsonResource.java index 89057c1..7c8f607 100644 --- a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/resource/JsonResource.java +++ b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/resource/JsonResource.java @@ -12,14 +12,17 @@ *******************************************************************************/ package org.eclipse.sirius.emfjson.resource; +import com.google.gson.Gson; import com.google.gson.JsonElement; import java.io.InputStream; import java.io.OutputStream; import java.util.Comparator; import java.util.Map; +import java.util.function.Function; import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EDataType; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EPackage; import org.eclipse.emf.ecore.EStructuralFeature; @@ -425,6 +428,12 @@ interface IEObjectHandler { */ Object OPTION_SAVE_FEATURES_ORDER_COMPARATOR = "OPTION_SAVE_FEATURES_ORDER_COMPARATOR"; //$NON-NLS-1$ + /** + * Specify a {@link Function} returning {@link Boolean#TRUE} if a given {@link EDataType} should be serialized + * to/from Json with {@link Gson}, {@link Boolean#FALSE} otherwise. + */ + String OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER = "isEDataTypeSerializableInJsonTester"; //$NON-NLS-1$ + /** * Associate an ID to the {@link EObject}. * diff --git a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectDeserializer.java b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectDeserializer.java index 15c64e1..2bba2de 100644 --- a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectDeserializer.java +++ b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectDeserializer.java @@ -12,6 +12,7 @@ *******************************************************************************/ package org.eclipse.sirius.emfjson.utils; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; @@ -31,6 +32,7 @@ import java.util.Map.Entry; import java.util.Optional; import java.util.Set; +import java.util.function.Function; import org.eclipse.emf.common.util.TreeIterator; import org.eclipse.emf.common.util.URI; @@ -910,21 +912,47 @@ private EObject loadEGenericType(JsonObject jsonObject) { private void deserializeEAttribute(EAttribute eAttribute, JsonElement jsonElement, EObject eObject) { EDataType dataType = eAttribute.getEAttributeType(); if (!eAttribute.isMany()) { - String newValue = this.getAsFlexibleString(jsonElement); - Object value = this.tryCreateDataTypeFromString(dataType, newValue); + Object value = null; + if (this.isEDataTypeSerializableInJsonTester(dataType)) { + value = new Gson().fromJson(jsonElement, eAttribute.getEType().getInstanceClass()); + } else { + String newValue = this.getAsFlexibleString(jsonElement); + value = this.tryCreateDataTypeFromString(dataType, newValue); + } this.helper.setValue(eObject, eAttribute, value); } else { JsonArray asJsonArray = this.getAsFlexibleArray(jsonElement); Object eGet = this.helper.getValue(eObject, eAttribute); if (eGet instanceof Collection) { for (JsonElement jElement : asJsonArray) { - Object value = this.tryCreateDataTypeFromString(dataType, jElement.getAsString()); + Object value = null; + if (this.isEDataTypeSerializableInJsonTester(dataType)) { + value = new Gson().fromJson(jElement, eAttribute.getEType().getInstanceClass()); + } else { + value = this.tryCreateDataTypeFromString(dataType, jElement.getAsString()); + } this.helper.setValue(eObject, eAttribute, value); } } } } + /** + * Test the given {@link EDataType} with the {@link JsonResource#OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER} + * option. If the option is not set, return false. + * + * @param eDataType + * @return true if the given {@link EDataType} should be deserialized from a Json tree. + */ + private boolean isEDataTypeSerializableInJsonTester(EDataType eDataType) { + @SuppressWarnings("unchecked") + Function isEDataTypeJsonSerializableTester = (Function) this.options.get(JsonResource.OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER); + if (isEDataTypeJsonSerializableTester != null) { + return isEDataTypeJsonSerializableTester.apply(eDataType).booleanValue(); + } + return false; + } + /** * Try to create a {@link EDataType} instance from a serialized form of the value. If the serialization is not * compatible with the given EDataType, returns the serialized form of the value unchanged. diff --git a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java index 87442ab..20b1347 100644 --- a/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java +++ b/bundles/org.eclipse.sirius.emfjson/src/main/java/org/eclipse/sirius/emfjson/utils/GsonEObjectSerializer.java @@ -12,8 +12,10 @@ *******************************************************************************/ package org.eclipse.sirius.emfjson.utils; +import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonNull; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; @@ -31,6 +33,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.function.Function; import java.util.stream.Collectors; import org.eclipse.emf.common.util.EList; @@ -1040,22 +1043,48 @@ private JsonElement serializeEDataType(EObject eObject, EAttribute eAttribute) { if (value instanceof Collection) { Collection collection = (Collection) value; for (Object object : collection) { - jsonArray.add(new JsonPrimitive(eFactoryInstance.convertToString(eAttribute.getEAttributeType(), object))); + if (object == null) { + jsonArray.add(JsonNull.INSTANCE); + } else if (this.isEDataTypeSerializableInJsonTester(eAttribute.getEAttributeType())) { + jsonArray.add(new Gson().toJsonTree(object)); + } else { + jsonArray.add(new JsonPrimitive(eFactoryInstance.convertToString(eAttribute.getEAttributeType(), object))); + } } } jsonElement = jsonArray; } else { String stringValue = eFactoryInstance.convertToString(eAttribute.getEAttributeType(), value); - - if (stringValue == null) { - stringValue = ""; //$NON-NLS-1$ + if (this.isEDataTypeSerializableInJsonTester(eAttribute.getEAttributeType())) { + jsonElement = new Gson().toJsonTree(value); + } else { + if (stringValue == null) { + jsonElement = JsonNull.INSTANCE; + } else { + jsonElement = new JsonPrimitive(stringValue); + } } - jsonElement = new JsonPrimitive(stringValue); } return jsonElement; } + /** + * Test the given {@link EDataType} with the {@link JsonResource#OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER} + * option. If the option is not set, return false. + * + * @param eDataType + * @return true if the given {@link EDataType} should be serialized as a Json tree. + */ + private boolean isEDataTypeSerializableInJsonTester(EDataType eDataType) { + @SuppressWarnings("unchecked") + Function isEDataTypeJsonSerializableTester = (Function) this.options.get(JsonResource.OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER); + if (isEDataTypeJsonSerializableTester != null) { + return isEDataTypeJsonSerializableTester.apply(eDataType).booleanValue(); + } + return false; + } + /** * Returns the JsonElement representing an EByteArray. * diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/DataTypeLoadTests.java b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/DataTypeLoadTests.java index ad7837c..7ee20e2 100644 --- a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/DataTypeLoadTests.java +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/DataTypeLoadTests.java @@ -13,9 +13,15 @@ package org.eclipse.sirius.emfjson.tests.internal.unit.load; +import java.util.function.Function; + +import org.eclipse.emf.ecore.EDataType; +import org.eclipse.sirius.emfjson.resource.JsonResource; import org.eclipse.sirius.emfjson.tests.internal.AbstractEMFJsonTests; import org.junit.Test; +import model.TestPojoDataTypeImpl; + /** * Tests class for EDataType deserialization. * @@ -49,4 +55,36 @@ public void testMultipleSerialiationDataType() { this.testLoad("NodeMultipleCustomDataType.xmi"); //$NON-NLS-1$ } + /** + * Test deserialization of POJO EDataType EAttribute monovalued. + */ + @Test + public void testLoadSingleValueAttributePojoDataType() { + Function eDataTypeJsonSerializableTester = new Function() { + @Override + public Boolean apply(EDataType eDataType) { + return Boolean.valueOf(eDataType.getInstanceClass() == TestPojoDataTypeImpl.class); + } + }; + this.options.put(JsonResource.OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER, eDataTypeJsonSerializableTester); + + this.testLoad("NodeSingleValueAttributePojoDataType.xmi"); //$NON-NLS-1$ + } + + /** + * Test deserialization of POJO EDataType EAttribute multivalued. + */ + @Test + public void testLoadMultiValuedAttributePojoDataType() { + Function eDataTypeJsonSerializableTester = new Function() { + @Override + public Boolean apply(EDataType eDataType) { + return Boolean.valueOf(eDataType.getInstanceClass() == TestPojoDataTypeImpl.class); + } + }; + this.options.put(JsonResource.OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER, eDataTypeJsonSerializableTester); + + this.testLoad("NodeMultiValuedAttributePojoDataType.xmi"); //$NON-NLS-1$ + } + } diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/ExtendedMetaDataAttributesLoadTests.java b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/ExtendedMetaDataAttributesLoadTests.java index 3273e0e..596eb8f 100644 --- a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/ExtendedMetaDataAttributesLoadTests.java +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/ExtendedMetaDataAttributesLoadTests.java @@ -45,7 +45,8 @@ public EStructuralFeature getElement(EClass eClass, String namespace, String nam // $NON-NLS-1$ //$NON-NLS-2$ return eClass.getEStructuralFeature("singleValuedReference"); //$NON-NLS-1$ } - // return super.getElement(eClass, namespace, name); // Doesn't work + // return super.getElement(eClass, namespace, name); // Doesn't work because the super implementation in + // Ecore ends up checking that the string is XMI return eClass.getEStructuralFeature(name); } }; @@ -65,7 +66,8 @@ public EStructuralFeature getElement(EClass eClass, String namespace, String nam if ("NodeMultiValuedAttribute".equals(eClass.getName()) && "multiStringAttributeOld".equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$ return eClass.getEStructuralFeature("multiStringAttribute"); //$NON-NLS-1$ } - // return super.getElement(eClass, namespace, name); // Doesn't work + // return super.getElement(eClass, namespace, name); // Doesn't work because the super implementation in + // Ecore ends up checking that the string is XMI return eClass.getEStructuralFeature(name); } }; diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/ExtendedMetaDataReferencesLoadTests.java b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/ExtendedMetaDataReferencesLoadTests.java index 8372ea5..935ae04 100644 --- a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/ExtendedMetaDataReferencesLoadTests.java +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/ExtendedMetaDataReferencesLoadTests.java @@ -44,7 +44,9 @@ public EStructuralFeature getElement(EClass eClass, String namespace, String nam if ("NodeSingleValueAttribute".equals(eClass.getName()) && "singleStringAttributeOld".equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$ return eClass.getEStructuralFeature("singleStringAttribute"); //$NON-NLS-1$ } - return super.getElement(eClass, namespace, name); + // return super.getElement(eClass, namespace, name); // Doesn't work because the super implementation in + // Ecore ends up checking that the string is XMI + return eClass.getEStructuralFeature(name); } }; @@ -71,7 +73,9 @@ public EStructuralFeature getElement(EClass eClass, String namespace, String nam if ("NodeMultiValuedAttribute".equals(eClass.getName()) && "multiStringAttributeOld".equals(name)) { //$NON-NLS-1$ //$NON-NLS-2$ return eClass.getEStructuralFeature("multiStringAttribute"); //$NON-NLS-1$ } - return super.getElement(eClass, namespace, name); + // return super.getElement(eClass, namespace, name); // Doesn't work because the super implementation in + // Ecore ends up checking that the string is XMI + return eClass.getEStructuralFeature(name); } }; diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/JsonHelperDataLoadTests.java b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/JsonHelperDataLoadTests.java index 548828a..b323adf 100644 --- a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/JsonHelperDataLoadTests.java +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/load/JsonHelperDataLoadTests.java @@ -10,6 +10,9 @@ *******************************************************************************/ package org.eclipse.sirius.emfjson.tests.internal.unit.load; +import java.util.function.Function; + +import org.eclipse.emf.ecore.EDataType; import org.eclipse.emf.ecore.EObject; import org.eclipse.emf.ecore.EStructuralFeature; import org.eclipse.sirius.emfjson.resource.JsonResource; @@ -17,6 +20,8 @@ import org.eclipse.sirius.emfjson.utils.JsonHelper; import org.junit.Test; +import model.TestPojoDataTypeImpl; + /** * Tests loading with ExtendedMetaData. */ @@ -42,13 +47,22 @@ public void testChangeAttributeTypeMono() { public void setValue(EObject object, EStructuralFeature feature, Object value) { Object newValue = value; if ("NodeSingleValueAttribute".equals(feature.getEContainingClass().getName()) && "singleIntAttribute".equals(feature.getName())) { //$NON-NLS-1$ //$NON-NLS-2$ - newValue = new Integer(((String) value).length()); + newValue = Integer.valueOf(((String) value).length()); } super.setValue(object, feature, newValue); } }; this.options.put(JsonResource.OPTION_CUSTOM_HELPER, jsonHelper); + + Function eDataTypeJsonSerializableTester = new Function() { + @Override + public Boolean apply(EDataType eDataType) { + return Boolean.valueOf(eDataType.getInstanceClass() == TestPojoDataTypeImpl.class); + } + }; + this.options.put(JsonResource.OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER, eDataTypeJsonSerializableTester); + this.testLoad("TestChangeAttributeTypeMono.xmi"); //$NON-NLS-1$ } @@ -62,7 +76,7 @@ public void testChangeAttributeTypeMulti() { public void setValue(EObject object, EStructuralFeature feature, Object value) { Object newValue = value; if ("NodeMultiValuedAttribute".equals(feature.getEContainingClass().getName()) && "multiIntAttribute".equals(feature.getName())) { //$NON-NLS-1$ //$NON-NLS-2$ - newValue = new Integer(((String) value).length()); + newValue = Integer.valueOf(((String) value).length()); } super.setValue(object, feature, newValue); } diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/save/DataTypeSaveTests.java b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/save/DataTypeSaveTests.java index 68e05d5..b870f53 100644 --- a/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/save/DataTypeSaveTests.java +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/java/org/eclipse/sirius/emfjson/tests/internal/unit/save/DataTypeSaveTests.java @@ -13,9 +13,15 @@ package org.eclipse.sirius.emfjson.tests.internal.unit.save; +import java.util.function.Function; + +import org.eclipse.emf.ecore.EDataType; +import org.eclipse.sirius.emfjson.resource.JsonResource; import org.eclipse.sirius.emfjson.tests.internal.AbstractEMFJsonTests; import org.junit.Test; +import model.TestPojoDataTypeImpl; + /** * Tests class for EDataType serialization. * @@ -49,4 +55,36 @@ public void testMultipleSerialiationDataType() { this.testSave("NodeMultipleCustomDataType.xmi"); //$NON-NLS-1$ } + /** + * Test serialization of POJO EDataType EAttribute monovalued. + */ + @Test + public void testSaveSingleValueAttributePojoDataType() { + Function eDataTypeJsonSerializableTester = new Function() { + @Override + public Boolean apply(EDataType eDataType) { + return Boolean.valueOf(eDataType.getInstanceClass() == TestPojoDataTypeImpl.class); + } + }; + this.options.put(JsonResource.OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER, eDataTypeJsonSerializableTester); + + this.testSave("NodeSingleValueAttributePojoDataType.xmi"); //$NON-NLS-1$ + } + + /** + * Test serialization of POJO EDataType EAttribute multivalued. + */ + @Test + public void testSaveMultiValuedAttributePojoDataType() { + Function eDataTypeJsonSerializableTester = new Function() { + @Override + public Boolean apply(EDataType eDataType) { + return Boolean.valueOf(eDataType.getInstanceClass() == TestPojoDataTypeImpl.class); + } + }; + this.options.put(JsonResource.OPTION_IS_EDATATYPE_SERIALIZABLE_IN_JSON_TESTER, eDataTypeJsonSerializableTester); + + this.testSave("NodeMultiValuedAttributePojoDataType.xmi"); //$NON-NLS-1$ + } + } diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/model/TestPojoDataTypeImpl.java b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/model/TestPojoDataTypeImpl.java new file mode 100644 index 0000000..0a3f0fb --- /dev/null +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/model/TestPojoDataTypeImpl.java @@ -0,0 +1,166 @@ +package model; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/******************************************************************************* + * Copyright (c) 2023 Obeo. All rights reserved. This program and the accompanying materials are made available under + * the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: Obeo - initial API and implementation + *******************************************************************************/ + +public class TestPojoDataTypeImpl { + + /** + * A String value. + */ + private String stringValue = null; + + /** + * An integer value. + */ + private int intValue = 0; + + /** + * Not serialized. + */ + private transient int transientIntValue = 0; + + /** + * Empty constructor for Json serialization. + * + */ + public TestPojoDataTypeImpl() { + + } + + /** + * Returns the stringValue. + * + * @return The stringValue + */ + public String getStringValue() { + return this.stringValue; + } + + /** + * Sets the stringValue. + * + * @param stringValue + * The stringValue to set + */ + public void setStringValue(String stringValue) { + this.stringValue = stringValue; + } + + /** + * Returns the intValue. + * + * @return The intValue + */ + public int getIntValue() { + return this.intValue; + } + + /** + * Sets the intValue. + * + * @param intValue + * The intValue to set + */ + public void setIntValue(int intValue) { + this.intValue = intValue; + } + + /** + * Returns the transientIntValue. + * + * @return The transientIntValue + */ + public int getTransientIntValue() { + return this.transientIntValue; + } + + /** + * Sets the transientIntValue. + * + * @param transientIntValue + * The transientIntValue to set + */ + public void setTransientIntValue(int transientIntValue) { + this.transientIntValue = transientIntValue; + } + + /** + * Used by the XMI serialization. This doesn't produce as a Json string on purpose, not to interfere with the Json + * serialization.
+ * + * @return A string representing this {@link TestPojoDataTypeImpl} instance. + */ + @Override + public String toString() { + return String.format("TestPojoDataTypeImpl('%s', %d)", this.stringValue.replaceAll("'", "'"), Integer.valueOf(this.intValue)); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + } + + /** + * Used by the XMI deserialization. This doesn't take a Json string on purpose, not to interfere with the Json + * deserialization. + * + * @param serialized + * The serialized form of a {@link TestPojoDataTypeImpl} as produced by + * {@link TestPojoDataTypeImpl#toString()}. + * @return A new instance of {@link TestPojoDataTypeImpl} if the given serialized form is consistent, null + * otherwise. + */ + public static TestPojoDataTypeImpl valueOf(String serialized) { + TestPojoDataTypeImpl value = null; + Matcher matcher = Pattern.compile("TestPojoDataTypeImpl\\('([^']*)', ([0-9]+)\\)").matcher(serialized); //$NON-NLS-1$ + if (matcher.matches()) { + value = new TestPojoDataTypeImpl(); + value.setStringValue(matcher.group(1).replaceAll("'", "'")); //$NON-NLS-1$ //$NON-NLS-2$ + value.setIntValue(Integer.parseInt(matcher.group(2))); + } + return value; + } + + /** + * {@inheritDoc} + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + this.intValue; + result = prime * result + ((this.stringValue == null) ? 0 : this.stringValue.hashCode()); + return result; + } + + /** + * {@inheritDoc} + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (this.getClass() != obj.getClass()) + return false; + TestPojoDataTypeImpl other = (TestPojoDataTypeImpl) obj; + if (this.intValue != other.intValue) + return false; + if (this.stringValue == null) { + if (other.stringValue != null) + return false; + } else if (!this.stringValue.equals(other.stringValue)) + return false; + return true; + } + +} diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/nodes.ecore b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/nodes.ecore index 0747385..9391e96 100644 --- a/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/nodes.ecore +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/nodes.ecore @@ -61,6 +61,8 @@ + + + diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeMultiValuedAttributePojoDataType.json b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeMultiValuedAttributePojoDataType.json new file mode 100644 index 0000000..125177c --- /dev/null +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeMultiValuedAttributePojoDataType.json @@ -0,0 +1,33 @@ +{ + "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:NodeMultiValuedAttribute", + "data": { + "multiPojoDataTypeAttribute": [ + { + "stringValue": "Eleven", + "intValue": 11 + }, + { + "stringValue": "Twelve", + "intValue": 12 + }, + { + "stringValue": "Forty two", + "intValue": 42 + } + ] + } + } + ] +} \ No newline at end of file diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeMultiValuedAttributePojoDataType.xmi b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeMultiValuedAttributePojoDataType.xmi new file mode 100644 index 0000000..3730971 --- /dev/null +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeMultiValuedAttributePojoDataType.xmi @@ -0,0 +1,11 @@ + + + TestPojoDataTypeImpl('Eleven', 11) + TestPojoDataTypeImpl('Twelve', 12) + TestPojoDataTypeImpl('Forty two', 42) + diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeSingleValueAttributePojoDataType.json b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeSingleValueAttributePojoDataType.json new file mode 100644 index 0000000..e6c48a7 --- /dev/null +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeSingleValueAttributePojoDataType.json @@ -0,0 +1,23 @@ +{ + "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": { + "singleTestPojoDataTypeAttribute": { + "stringValue": "Twelve", + "intValue": 12 + } + } + } + ] +} \ No newline at end of file diff --git a/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeSingleValueAttributePojoDataType.xmi b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeSingleValueAttributePojoDataType.xmi new file mode 100644 index 0000000..1aaf797 --- /dev/null +++ b/tests/org.eclipse.sirius.emfjson.tests/src/main/resources/unit/attributes/datatypes/NodeSingleValueAttributePojoDataType.xmi @@ -0,0 +1,8 @@ + +