Skip to content

Commit

Permalink
Update Serializer to add the possibility to export into JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitgaudou committed May 8, 2022
1 parent e99eb13 commit b0470d1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion msi.gama.core/src/msi/gaml/statements/SaveStatement.java
Expand Up @@ -1113,7 +1113,7 @@ public String toCleanString(final Object o) {
* the var
* @return the string
*/
public String type(final ITyped var) {
public static String type(final ITyped var) {
switch (var.getGamlType().id()) {
case IType.BOOL:
return "Boolean";
Expand Down
13 changes: 9 additions & 4 deletions ummisco.gama.serialize/.classpath
@@ -1,14 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry exported="true" kind="lib" path="ext/mxparser-1.2.2.jar"/>
<classpathentry exported="true" kind="lib" path="ext/xstream-1.4.18.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gaml">
<attributes>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="ext/mxparser-1.2.2.jar"/>
<classpathentry kind="lib" path="ext/xstream-1.4.18.jar"/>
<classpathentry kind="lib" path="ext/jettison-1.4.1.jar"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion ummisco.gama.serialize/build.properties
Expand Up @@ -6,4 +6,5 @@ bin.includes = META-INF/,\
plugin.xml,\
models/,\
ext/mxparser-1.2.2.jar,\
ext/xstream-1.4.18.jar
ext/xstream-1.4.18.jar,\
ext/jettison-1.4.1.jar
Binary file added ummisco.gama.serialize/ext/jettison-1.4.1.jar
Binary file not shown.
Expand Up @@ -16,6 +16,7 @@

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.security.AnyTypePermission;

Expand All @@ -39,14 +40,23 @@ public abstract class StreamConverter {
private static Map<Class<?>, XStream> xStreamMap = Collections.synchronizedMap(new HashMap<Class<?>, XStream>());

private static XStream getXStreamInstance(Class<?> clazz) {
return getXStreamInstance(clazz, false);
}

private static XStream getXStreamInstance(Class<?> clazz, boolean toJSON) {
if (xStreamMap.containsKey(clazz)) {
return xStreamMap.get(clazz);
}
synchronized (clazz) {
if (xStreamMap.containsKey(clazz)) {
return xStreamMap.get(clazz);
}
XStream xStream = new XStream(new DomDriver());
XStream xStream;
if(toJSON) {
xStream = new XStream(new JettisonMappedXmlDriver());
} else {
xStream = new XStream(new DomDriver());
}
xStream.ignoreUnknownElements();
xStream.processAnnotations(clazz);
xStream.addPermission(AnyTypePermission.ANY);
Expand Down Expand Up @@ -76,6 +86,19 @@ public static void registerConverter(final XStream st,final Converter c) {
st.registerConverter(c);
}

/**
* Convert object to stream in JSON.
*
* @param scope
* the scope
* @param o
* the o
* @return the string
*/
public static synchronized String convertObjectToJSONStream(final IScope scope, final Object o) {
return loadAndBuild(new ConverterScope(scope),o, true).toXML(o);
}

/**
* Load and build.
*
Expand All @@ -84,9 +107,13 @@ public static void registerConverter(final XStream st,final Converter c) {
* @return the x stream
*/
public static XStream loadAndBuild(final ConverterScope cs, final Object o) {
return loadAndBuild(cs, o, false);
}

public static XStream loadAndBuild(final ConverterScope cs, final Object o, final boolean toJSON) {

final Converter[] cnv = Converters.converterFactory(cs);
XStream streamer=getXStreamInstance(o.getClass());
XStream streamer = getXStreamInstance(o.getClass(),toJSON);
for (final Converter c : cnv) { StreamConverter.registerConverter(streamer,c); }
// dataStreamer.setMode(XStream.ID_REFERENCES);
return streamer;
Expand Down

0 comments on commit b0470d1

Please sign in to comment.