Skip to content

Commit

Permalink
Improving some of the serialization structure, need to improve desera…
Browse files Browse the repository at this point in the history
…ilziation
  • Loading branch information
Tom White committed Mar 20, 2013
1 parent b6b7841 commit 86b9223
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
31 changes: 28 additions & 3 deletions simplCore/src/simpl/deserialization/json/JsonDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public JsonDeserializer()
{
}

private CompositeInterpretation(String tagOrField, ClassDescriptor contextCD, ISimplTypesScope sts)
{

}

public CompositeInterpretation deserialize(String objRepr, ISimplTypesScope sts) throws SIMPLTranslationException
{
Object val = JSONValue.parse(objRepr);
Expand All @@ -36,8 +41,31 @@ public CompositeInterpretation deserialize(String objRepr, ISimplTypesScope sts)

JSONObject innerObject = (JSONObject)jsonRepr.get(rootCD.getTagName());

if(innerObject.keySet().contains("simpl.id") && innerObject.keySet().contains("simpl.ref"))
{
throw new SIMPLTranslationException("Cannot contain both an ID and a Ref!");
}

if(innerObject.keySet().contains("simpl.ref"))
{
if(innerObject.keySet().size() > 1)
{
throw new SIMPLTranslationException("References should not have additional contents!");
}

ci.setRefString((String)innerObject.get("simpl.ref"));
}

if(innerObject.keySet().contains("simpl.id"))
{
String id = (String)innerObject.get("simpl.id");
ci.setIDString(id);
}

for(Object key : innerObject.keySet())
{
String keyVal = (String) key;

Object fieldValue = innerObject.get(key); // todo; handle polymorphic wrapping yo.

if(rootCD.fields().Scalars.contains((String)key))
Expand All @@ -52,8 +80,5 @@ public CompositeInterpretation deserialize(String objRepr, ISimplTypesScope sts)
{
throw new SIMPLTranslationException("Malformed root object! Expecting only one key at the highest level.");
}



}
}
6 changes: 2 additions & 4 deletions simplCore/src/simpl/serialization/json/JsonSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public JsonSerializer()
translators = new HashMap<Class<?>, JsonTranslationUnit>();
registerTranslator(new JsonScalarTranslation());
registerTranslator(new JsonCompositeTranslation(this)); // pass this a reference to the json serializer b/c it needs to processTranslationUnits

}

private void registerTranslator(JsonTranslationUnit jtu)
Expand All @@ -39,11 +40,8 @@ public void processTranslationUnit(SimplInterpretation si, JSONObject obj) throw
jtu.appendToIR(si, obj);
}

public String serialize(Object obj) throws SIMPLTranslationException
public String serialize(SimplInterpretation interp) throws SIMPLTranslationException
{
SimplInterpreter si = new SimplInterpreter();
SimplInterpretation interp = si.interpretInstance(obj);

if(!(interp instanceof CompositeInterpretation))
{
throw new SIMPLTranslationException("Root object must be a composite interpretation!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.Test;

import simpl.exceptions.SIMPLTranslationException;
import simpl.interpretation.SimplInterpretation;
import simpl.interpretation.SimplInterpreter;
import simpl.serialization.Circle;
import simpl.serialization.Point;

Expand All @@ -22,7 +24,10 @@ public void testBasicCompositeCase() throws SIMPLTranslationException {

c.center = center;

String result = new JsonSerializer().serialize(c);
SimplInterpreter si = new SimplInterpreter();
SimplInterpretation interp = si.interpretInstance(c);

String result = new JsonSerializer().serialize(interp);

assertEquals("{\"circle\":{\"center\":{\"y\":\"-1\",\"x\":\"0\"},\"radius\":\"1.337\"}}", result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.Test;

import simpl.exceptions.SIMPLTranslationException;
import simpl.interpretation.SimplInterpretation;
import simpl.interpretation.SimplInterpreter;
import simpl.serialization.Point;

public class testSerializeScalars {
Expand All @@ -16,7 +18,11 @@ public void testBasicScalarCase() throws SIMPLTranslationException {
x.y = 3;

JsonSerializer json = new JsonSerializer();
String result = json.serialize(x);


SimplInterpreter si = new SimplInterpreter();
SimplInterpretation interp = si.interpretInstance(x);
String result = json.serialize(interp);

assertNotNull("Result should not be null!", result);
assertEquals("{\"point\":{\"y\":\"3\",\"x\":\"1\"}}", result);
Expand Down

0 comments on commit 86b9223

Please sign in to comment.