1
0
mirror of https://github.com/balkian/SOJA.git synced 2024-12-28 20:58:13 +00:00

Added JSON to JASON conversion

This commit is contained in:
J.Fernando Sánchez 2012-04-18 17:25:58 +02:00
parent 4860b9d6b9
commit b23bbd0451
3 changed files with 64 additions and 3 deletions

View File

@ -6,8 +6,13 @@ package es.upm.dit.gsi.jason.utils;
import jason.asSyntax.Literal; import jason.asSyntax.Literal;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/** /**
* *
* Project: Web40SOJason * Project: Web40SOJason
@ -84,4 +89,60 @@ public abstract class CollectionUtils {
return strArray; return strArray;
} }
public static String toPerceptsObject(JSONObject jso){
StringBuilder sb = new StringBuilder();
Iterator<String> it = jso.keys();
while(it.hasNext()){
String key = it.next();
try {
Object temp= jso.get(key);
String res = toPercepts(temp);
sb.append(key+"(");
sb.append(res);
sb.append(")");
} catch (JSONException e) {
e.printStackTrace();
}
if(it.hasNext()){
sb.append(",");
}
}
return sb.toString();
}
public static String toPerceptsArray(JSONArray jsa){
StringBuilder sb = new StringBuilder();
int top = jsa.length();
int topm = top-1;
sb.append("[");
for(int i=0;i<jsa.length();i++){
try{
sb.append(toPercepts(jsa.get(i)));
}catch(JSONException ex){
ex.printStackTrace();
}
if(i<topm){
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
public static String toPercepts(Object obj){
if(obj instanceof JSONArray){
return toPerceptsArray((JSONArray) obj);
}
else if (obj instanceof JSONObject) {
return toPerceptsObject((JSONObject) obj);
}
else{
return NotationUtils.compact(obj.toString());
}
}
} }

View File

@ -45,10 +45,10 @@ public class NotationUtils {
if (isValidAtom (str)) {return str;} if (isValidAtom (str)) {return str;}
if (!isCompactable(str)) {return null;} if (!isCompactable(str)) {return null;}
str = str.toLowerCase();
str = str.replace("_", "___"); str = str.replace("_", "___");
str = str.replace(" ", "_"); str = str.replace(" ", "_");
str = str.replace("ñ", "n"); return str.replace("ñ", "n");
return str.toLowerCase();
} }
/** /**

View File

@ -98,7 +98,7 @@ public class SocketIOConnector implements AsyncWebServiceConnector{
model.setDataInbox(agName, lit); model.setDataInbox(agName, lit);
logger.info("DataInbox set"); logger.info("DataInbox set");
} }
}, new JSONObject().put("Hello", "Planeta")); }, new JSONObject().put("Sending", "From Jason"));
} catch (JSONException e) { } catch (JSONException e) {
// TODO Auto-generated catch block // TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();