-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Serialization / deserialization of POJO
- Loading branch information
1 parent
72ac65d
commit 63ed710
Showing
10 changed files
with
349 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
tests/org.eclipse.sirius.emfjson.tests/src/main/resources/model/TestPojoDataTypeImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.<br> | ||
* | ||
* @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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.