Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on javax.xml.bind.DatatypeConverter #16

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bundles/org.eclipse.sirius.emfjson/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ Bundle-Localization: bundle
Export-Package: org.eclipse.sirius.emfjson.resource;version="1.1.0",
org.eclipse.sirius.emfjson.resource.exception;version="1.0.0",
org.eclipse.sirius.emfjson.utils;version="2.1.0"
Import-Package: javax.xml.bind;version="0.0.0",
com.google.gson;version="2.2.4",
Import-Package: com.google.gson;version="2.2.4",
com.google.gson.reflect;version="2.2.4",
com.google.gson.stream;version="2.2.4",
org.eclipse.emf.common.util;version="0.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.DatatypeConverter;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.common.util.URI;
Expand Down Expand Up @@ -90,6 +88,11 @@ public class GsonEObjectSerializer implements JsonSerializer<List<EObject>> {
*/
private static final int CROSS_DOC = 2;

/**
* Hexadecimal digits.
*/
private static final char[] DIGITS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };

/**
* the helper.
*/
Expand Down Expand Up @@ -1057,20 +1060,37 @@ private JsonElement serializeEByteArrayEAttribute(EObject eObject, EAttribute eA
Collection<?> collection = (Collection<?>) value;
for (Object object : collection) {
if (object instanceof byte[]) {
jsonArray.add(new JsonPrimitive(DatatypeConverter.printHexBinary((byte[]) object)));
jsonArray.add(new JsonPrimitive(this.toHexString((byte[]) object)));
}
}
}
jsonElement = jsonArray;
} else {
if (value instanceof byte[]) {
jsonElement = new JsonPrimitive(DatatypeConverter.printHexBinary((byte[]) value));
jsonElement = new JsonPrimitive(this.toHexString((byte[]) value));
}
}

return jsonElement;
}

/**
* Converts an array of bytes into an hexadecimal string.
*
* @param source
* the source bytes.
* @return an hexadecimal representation of the source bytes.
*/
private String toHexString(byte[] source) {
char[] target = new char[source.length * 2];
for (int i = 0; i < source.length; i++) {
byte b = source[i];
target[2 * i] = DIGITS[(b >> 4) & 0xf];
target[2 * i + 1] = DIGITS[b & 0x0f];
}
return new String(target);
}

/**
* Returns the JsonElement representing an EDate.
*
Expand Down
Loading