Skip to content

Commit

Permalink
#1136 - Async cannot serialize JSONMap and JSONList classes: "om.thou…
Browse files Browse the repository at this point in the history
…ghtworks.xstream.converters.ConversionException: No converter available"
  • Loading branch information
ipolevoy committed Jun 28, 2021
1 parent d43d9df commit 2380bec
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 2 deletions.
5 changes: 5 additions & 0 deletions javalite-async/src/main/java/org/javalite/async/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.security.AnyTypePermission;
import com.thoughtworks.xstream.security.NoTypePermission;
import org.javalite.async.xstream.CDATAXppDriver;
import org.javalite.async.xstream.JSONListConverter;
import org.javalite.async.xstream.JSONMapConverter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -41,6 +44,8 @@ public abstract class Command {
private static final XStream X_STREAM;
static {
X_STREAM = new XStream(new CDATAXppDriver());
X_STREAM.registerConverter(new JSONMapConverter(X_STREAM.getMapper()));
X_STREAM.registerConverter(new JSONListConverter(X_STREAM.getMapper()));
X_STREAM.addPermission(NoTypePermission.NONE );
X_STREAM.addPermission(AnyTypePermission.ANY);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package org.javalite.async;
/*
Copyright 2009-(CURRENT YEAR) Igor Polevoy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.javalite.async.xstream;

import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
Expand All @@ -10,7 +25,7 @@
/**
* @author Igor Polevoy on 1/31/16.
*/
class CDATAXppDriver extends XppDriver {
public class CDATAXppDriver extends XppDriver {

@Override
public HierarchicalStreamWriter createWriter(Writer out) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2009-(CURRENT YEAR) Igor Polevoy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.javalite.async.xstream;

import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.mapper.Mapper;
import org.javalite.json.JSONList;

public class JSONListConverter extends CollectionConverter{
public JSONListConverter(Mapper mapper) {
super(mapper);
}

@Override
public boolean canConvert(Class type) {
return type.equals(JSONList.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2009-(CURRENT YEAR) Igor Polevoy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.javalite.async.xstream;

import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.mapper.Mapper;
import org.javalite.json.JSONMap;

public class JSONMapConverter extends MapConverter {
public JSONMapConverter(Mapper mapper) {
super(mapper);
}

@Override
public boolean canConvert(Class type) {
return type.equals(JSONMap.class);
}
}
45 changes: 45 additions & 0 deletions javalite-async/src/test/java/org/javalite/async/CommandSpec.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package org.javalite.async;

import org.javalite.json.JSONList;
import org.javalite.json.JSONMap;
import org.junit.Test;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.javalite.common.Collections.list;
import static org.javalite.common.Collections.map;
import static org.javalite.test.jspec.JSpec.a;
import static org.javalite.test.jspec.JSpec.the;

Expand All @@ -31,4 +36,44 @@ public void shouldSerializeDeserializeBinary() throws IOException {
a(helloCommand1.getMessage()).shouldBeEqual("Thanks for all the fish...");
a(helloCommand).shouldNotBeTheSameAs(helloCommand1);
}

@Test
public void shouldSerializeJSONClasses(){

class MyCommand extends Command{
private JSONMap map;
private JSONList list;
public MyCommand(JSONMap map, JSONList list) {
this.map = map;
this.list = list;
}

@Override
public void execute() {
System.out.println("");
}

Map getMap(){
return map;
}

List getList(){
return list;
}

}
JSONMap map = new JSONMap(map("name", "John"));
MyCommand command1 = new MyCommand(map, new JSONList(list(1, 2,3)));

the(command1.getList()).shouldContain(1);
the(command1.getList()).shouldContain(2);
the(command1.getList()).shouldContain(3);

MyCommand command = Command.fromXml(command1.toXml());

the(command.getList()).shouldContain(1);
the(command.getList()).shouldContain(2);
the(command.getList()).shouldContain(3);
the(command.getMap().get("name")).shouldEqual("John");
}
}
2 changes: 2 additions & 0 deletions javalite-common/src/main/java/org/javalite/json/JSONList.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public JSONList(String jsonList){
super(JSONHelper.toList(jsonList));
}

public JSONList(){}

/**
* Returns a JSONMap at a provided index. If the object is not a map, it will throw an exception.
*
Expand Down
2 changes: 2 additions & 0 deletions javalite-common/src/main/java/org/javalite/json/JSONMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public JSONMap(Map map){
super(map);
}

public JSONMap(){}

/**
* Returns a <code>JSONList</code> for a list name. It is expected that this list is an immediate child of this map.
* If the object is not a list, the method will throw an exception.
Expand Down

0 comments on commit 2380bec

Please sign in to comment.