Skip to content

Commit

Permalink
Handling of enums
Browse files Browse the repository at this point in the history
  • Loading branch information
sylvainhalle committed Apr 25, 2019
1 parent 8fd9a96 commit 2a4f96b
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 9 deletions.
23 changes: 16 additions & 7 deletions Source/Core/src/ca/uqac/lif/azrael/ObjectPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public abstract class ObjectPrinter<T>
* types
*/
protected List<PrintHandler<T>> m_handlers;

/**
* The default handler to use when no other handler accepts an object
*/
protected PrintHandler<T> m_reflectionHandler = new ReflectionPrintHandler<T>(this);

/**
* Whether to use the {@link Readable} interface when an object
* implements it
*/
protected boolean m_usePrintable = true;

/**
* Creates a new object printer
*/
Expand All @@ -54,7 +54,7 @@ public ObjectPrinter()
super();
m_handlers = new ArrayList<PrintHandler<T>>();
}

/**
* Serializes the contents of an object. If the object implements the
* {@link Printable} interface, it is serialized by calling its
Expand Down Expand Up @@ -85,7 +85,7 @@ public T print(Object o) throws PrintException
}
return m_reflectionHandler.handle(o);
}

/**
* Wraps an object into a structure that contains its type declaration
* and its printed contents
Expand All @@ -95,7 +95,7 @@ public T print(Object o) throws PrintException
* @throws PrintException Thrown if the print operation caused an error
*/
public abstract T wrap(Object o, T t) throws PrintException;

/**
* Resets the state of the printer
*/
Expand All @@ -110,7 +110,7 @@ public void reset()
m_reflectionHandler.reset();
}
}

/**
* Tells whether the object printer uses the {@link Printable}
* interface when an object implements it
Expand All @@ -120,4 +120,13 @@ public boolean usesPrintable()
{
return m_usePrintable;
}

/**
* Prints a version number
* @return The number
*/
public static String getVersionString()
{
return "2.0";
}
}
2 changes: 2 additions & 0 deletions Source/Core/src/ca/uqac/lif/azrael/ObjectReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -57,6 +58,7 @@ public ObjectReader()
{
super();
m_handlers = new ArrayList<ReadHandler<T>>();
m_classLoaders = new HashSet<ClassLoader>();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected static List<Field> getAllFields(Class<?> type)
while (it.hasNext())
{
Field f = it.next();
if (f.getName().startsWith("$") || f.getName().startsWith(("!")))
if (f.getClass().isEnum() || f.getName().startsWith("$") || f.getName().startsWith(("!")))
{
it.remove();
}
Expand Down
42 changes: 42 additions & 0 deletions Source/Json/src/ca/uqac/lif/azrael/json/EnumPrintHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Azrael, a serializer for Java objects
Copyright (C) 2016-2019 Sylvain Hallé
Laboratoire d'informatique formelle
Université du Québec à Chicoutimi, Canada
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ca.uqac.lif.azrael.json;

import ca.uqac.lif.azrael.PrintException;
import ca.uqac.lif.json.JsonElement;

public class EnumPrintHandler extends JsonPrintHandler
{
public EnumPrintHandler(JsonPrinter printer)
{
super(printer);
}

@Override
public boolean canHandle(Object o)
{
return o.getClass().isEnum();
}

@Override
public JsonElement handle(Object o) throws PrintException
{
return m_printer.wrap(o, m_printer.print(o.toString()));
}
}
52 changes: 52 additions & 0 deletions Source/Json/src/ca/uqac/lif/azrael/json/EnumReadHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Azrael, a serializer for Java objects
Copyright (C) 2016-2019 Sylvain Hallé
Laboratoire d'informatique formelle
Université du Québec à Chicoutimi, Canada
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ca.uqac.lif.azrael.json;

import ca.uqac.lif.azrael.ReadException;
import ca.uqac.lif.json.JsonElement;
import ca.uqac.lif.json.JsonString;

public class EnumReadHandler extends JsonReadHandler
{

public EnumReadHandler(JsonReader reader)
{
super(reader);
}

@Override
public boolean canHandle(JsonElement o) throws ReadException
{
if (!m_reader.isWrapped(o))
{
return false;
}
Class<?> clazz = m_reader.unwrapType(o);
return clazz.isEnum();
}

@Override
public String handle(JsonElement o) throws ReadException
{
//Class<?> clazz = m_reader.unwrapType(o);
JsonString s = (JsonString) m_reader.unwrapContents(o);
return s.stringValue();
}

}
3 changes: 2 additions & 1 deletion Source/Json/src/ca/uqac/lif/azrael/json/JsonPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,16 @@ public class JsonPrinter extends ObjectPrinter<JsonElement>
public JsonPrinter()
{
super();
m_handlers.add(new RawPrintHandler(this));
m_handlers.add(new NullPrintHandler(this));
m_handlers.add(new BooleanPrintHandler(this));
m_handlers.add(new NumberPrintHandler(this));
m_handlers.add(new StringPrintHandler(this));
m_handlers.add(new ListPrintHandler(this));
m_handlers.add(new QueuePrintHandler(this));
m_handlers.add(new SetPrintHandler(this));
m_handlers.add(new EnumPrintHandler(this));
m_handlers.add(new MapPrintHandler(this));
m_handlers.add(new RawPrintHandler(this));
}

@Override
Expand Down
1 change: 1 addition & 0 deletions Source/Json/src/ca/uqac/lif/azrael/json/JsonReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public JsonReader()
m_handlers.add(new NumberReadHandler(this));
m_handlers.add(new StringReadHandler(this));
m_handlers.add(new ListReadHandler(this));
m_handlers.add(new EnumReadHandler(this));
m_handlers.add(new QueueReadHandler(this));
m_handlers.add(new SetReadHandler(this));
m_handlers.add(new MapReadHandler(this));
Expand Down
22 changes: 22 additions & 0 deletions Source/Json/src/ca/uqac/lif/azrael/json/JsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ public void testJson4() throws PrintException, ReadException
assertEquals("bar", nco.m_objects.get(1).m_y);
}

@Test
public void testEnum1() throws PrintException, ReadException
{
EnumObject eo = new EnumObject();
JsonPrinter printer = new JsonPrinter();
JsonElement je = printer.print(eo);
assertNotNull(je);
JsonReader reader = new JsonReader();
Object o = reader.read(je);
assertNotNull(o);
assertTrue(o instanceof EnumObject);
EnumObject nco = (EnumObject) o;
assertEquals(nco.me, EnumObject.MyEnum.FOO);
}

protected static class SimpleObject implements Printable, Readable
{
int m_x;
Expand Down Expand Up @@ -217,4 +232,11 @@ public void add(SimpleObject o)
m_objects.add(o);
}
}

protected static class EnumObject
{
public static enum MyEnum {FOO, BAR}

public MyEnum me = MyEnum.FOO;
}
}
42 changes: 42 additions & 0 deletions Source/Xml/src/ca/uqac/lif/azrael/xml/EnumPrintHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Azrael, a serializer for Java objects
Copyright (C) 2016-2019 Sylvain Hallé
Laboratoire d'informatique formelle
Université du Québec à Chicoutimi, Canada
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ca.uqac.lif.azrael.xml;

import ca.uqac.lif.azrael.PrintException;
import ca.uqac.lif.xml.XmlElement;

public class EnumPrintHandler extends XmlPrintHandler
{
public EnumPrintHandler(XmlPrinter printer)
{
super(printer);
}

@Override
public boolean canHandle(Object o)
{
return o.getClass().isEnum();
}

@Override
public XmlElement handle(Object o) throws PrintException
{
return m_printer.wrap(o, m_printer.print(o.toString()));
}
}
50 changes: 50 additions & 0 deletions Source/Xml/src/ca/uqac/lif/azrael/xml/EnumReadHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Azrael, a serializer for Java objects
Copyright (C) 2016-2019 Sylvain Hallé
Laboratoire d'informatique formelle
Université du Québec à Chicoutimi, Canada
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ca.uqac.lif.azrael.xml;

import ca.uqac.lif.azrael.ReadException;
import ca.uqac.lif.xml.XmlElement;

public class EnumReadHandler extends XmlReadHandler
{

public EnumReadHandler(XmlReader reader)
{
super(reader);
}

@Override
public boolean canHandle(XmlElement o) throws ReadException
{
if (!m_reader.isWrapped(o))
{
return false;
}
Class<?> clazz = m_reader.unwrapType(o);
return clazz.isEnum();
}

@Override
public String handle(XmlElement o) throws ReadException
{
String s = (String) m_reader.unwrapContents(o);
return s;
}

}
15 changes: 15 additions & 0 deletions Source/Xml/src/ca/uqac/lif/azrael/xml/XmlReadHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ca.uqac.lif.azrael.xml;

import ca.uqac.lif.azrael.ReadHandler;
import ca.uqac.lif.xml.XmlElement;

public abstract class XmlReadHandler implements ReadHandler<XmlElement>
{
protected XmlReader m_reader;

public XmlReadHandler(XmlReader printer)
{
super();
m_reader = printer;
}
}

0 comments on commit 2a4f96b

Please sign in to comment.