Last active
August 29, 2015 14:04
-
-
Save mh61503891/4597b6108f943ce8e305 to your computer and use it in GitHub Desktop.
A SWI-Prolog-like Json-Term converter for Prolog Cafe (Example Implementation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.ListIterator; | |
import java.util.Map; | |
import jp.ac.kobe_u.cs.prolog.lang.DoubleTerm; | |
import jp.ac.kobe_u.cs.prolog.lang.IntegerTerm; | |
import jp.ac.kobe_u.cs.prolog.lang.ListTerm; | |
import jp.ac.kobe_u.cs.prolog.lang.Prolog; | |
import jp.ac.kobe_u.cs.prolog.lang.StructureTerm; | |
import jp.ac.kobe_u.cs.prolog.lang.SymbolTerm; | |
import jp.ac.kobe_u.cs.prolog.lang.Term; | |
/** | |
* @author Masayuki Higashino | |
*/ | |
public class JsonUtils { | |
public static Term javaToProlog(Object o) throws IllegalArgumentException { | |
if (o == null) { | |
return new StructureTerm(SymbolTerm.makeSymbol("@", 1), new Term[] { SymbolTerm.makeSymbol("null") }); | |
} else if (o instanceof Boolean) { | |
return new StructureTerm(SymbolTerm.makeSymbol("@", 1), new Term[] { SymbolTerm.makeSymbol(o.toString()) }); | |
} else if (o instanceof String) { | |
return SymbolTerm.makeSymbol(o.toString()); | |
} else if (o instanceof Integer) { | |
return new IntegerTerm((Integer) o); | |
} else if (o instanceof Double) { | |
return new DoubleTerm((Double) o); | |
} else if (o instanceof List) { | |
Term term = Prolog.Nil; | |
List<?> list = (List<?>) o; | |
ListIterator<?> i = list.listIterator(list.size()); | |
while (i.hasPrevious()) | |
term = new ListTerm(javaToProlog(i.previous()), term); | |
return term; | |
} else if (o instanceof Map) { | |
SymbolTerm json = SymbolTerm.makeSymbol("json", 1); | |
Term list = Prolog.Nil; | |
for (Map.Entry<?, ?> entry : ((Map<?, ?>) o).entrySet()) { | |
SymbolTerm key = SymbolTerm.makeSymbol(entry.getKey().toString()); | |
Term value = javaToProlog(entry.getValue()); | |
Term[] args = new Term[] { key, value }; | |
StructureTerm e = new StructureTerm(SymbolTerm.makeSymbol("=", 2), args); | |
list = new ListTerm(e, list); | |
} | |
return new StructureTerm(json, new Term[] { list }); | |
} else { | |
throw new IllegalArgumentException("expect to null|Boolean|String|Integer|Double|List|Map: " + o.toString()); | |
} | |
} | |
public static Object prologToJava(Term o) throws IllegalArgumentException { | |
if (o.isSymbol()) { | |
return ((SymbolTerm) o).name(); | |
} else if (o.isInteger()) { | |
return ((IntegerTerm) o).intValue(); | |
} else if (o.isDouble()) { | |
return ((DoubleTerm) o).doubleValue(); | |
} else if (o.isList()) { | |
List<Object> list = new ArrayList<>(); | |
Term l = o; | |
while (!l.isNil()) { | |
Term e = ((ListTerm) l).car(); | |
list.add(prologToJava(e)); | |
l = ((ListTerm) l).cdr(); | |
} | |
return list; | |
} else if (o.isStructure()) { | |
StructureTerm t = (StructureTerm) o; | |
if (t.name().equals("@") && t.arity() == 1 && t.args()[0].isSymbol()) { | |
String value = ((SymbolTerm) t.args()[0]).name(); | |
if (value.equals("true")) { | |
return true; | |
} else if (value.equals("false")) { | |
return false; | |
} else if (value.equals("null")) { | |
return null; | |
} else { | |
throw new IllegalArgumentException("expect to true|false|null: " + o.toString()); | |
} | |
} else if (t.name().equals("json") && t.arity() == 1 && t.args()[0].isList()) { | |
Map<Object, Object> map = new HashMap<>(); | |
Term l = t.args()[0]; | |
while (!l.isNil()) { | |
Term e = ((ListTerm) l).car(); | |
if (e.isStructure()) { | |
StructureTerm entry = (StructureTerm) e; | |
if (entry.name().equals("=") && entry.arity() == 2) { | |
Term k = entry.args()[0]; | |
Term v = entry.args()[1]; | |
map.put(prologToJava(k), prologToJava(v)); | |
} else { | |
throw new IllegalArgumentException("expect to =/2: " + o.toString()); | |
} | |
} else { | |
throw new IllegalArgumentException("expect to =/2: " + o.toString()); | |
} | |
l = ((ListTerm) l).cdr(); | |
} | |
return map; | |
} else { | |
throw new IllegalArgumentException("expect to @/1|json/1: " + o.toString()); | |
} | |
} else { | |
throw new IllegalArgumentException("expect to symbol|integer|double|list|structure: " + o.toString()); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
Map<String, Object> java1 = new HashMap<String, Object>() { | |
private static final long serialVersionUID = 1L; | |
{ | |
put("jsonrpc", "2.0"); | |
put("method", "user.create"); | |
put("params", new HashMap<String, Object>() { | |
private static final long serialVersionUID = 1L; | |
{ | |
put("email", "user@example.net"); | |
put("password", "drowssap"); | |
put("contents", Arrays.asList("aa", "bb", true, null, false)); | |
} | |
}); | |
put("id", "12345"); | |
} | |
}; | |
Term prolog = javaToProlog(java1); | |
Object java2 = prologToJava(prolog); | |
System.err.println(java1); | |
System.err.println(prolog); | |
System.err.println(java2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment