本文整理汇总了Java中ca.uqac.lif.json.JsonElement类的典型用法代码示例。如果您正苦于以下问题:Java JsonElement类的具体用法?Java JsonElement怎么用?Java JsonElement使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
JsonElement类属于ca.uqac.lif.json包,在下文中一共展示了JsonElement类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testList1
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Test
public void testList1() throws SerializerException
{
List<Integer> obj_a = new LinkedList<Integer>();
obj_a.add(1);
obj_a.add(2);
obj_a.add(3);
JsonElement e = m_serializer.serialize(obj_a);
assertNotNull(e);
assertTrue("Serialized element should be a list, got a " + e.getClass().getSimpleName(), e instanceof JsonList);
JsonList e_list = (JsonList) e;
assertEquals(3, e_list.size());
Object o_des = m_serializer.deserializeAs(e, LinkedList.class);
assertNotNull(o_des);
assertTrue("Object should be an instance of LinkedList, got " + o_des.getClass().getSimpleName(), o_des instanceof LinkedList);
}
示例2: testJsonList1
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Test
public void testJsonList1() throws SerializerException
{
List<JsonElement> obj_a = new LinkedList<JsonElement>();
obj_a.add(new JsonString("1"));
obj_a.add(new JsonString("2"));
obj_a.add(new JsonNumber(3));
JsonElement e = m_serializer.serialize(obj_a);
assertNotNull(e);
assertTrue("Serialized element should be a list, got a " + e.getClass().getSimpleName(), e instanceof JsonList);
JsonList e_list = (JsonList) e;
assertEquals(3, e_list.size());
Object o_des = m_serializer.deserializeAs(e, LinkedList.class);
assertNotNull(o_des);
assertTrue("Object should be an instance of LinkedList, got " + o_des.getClass().getSimpleName(), o_des instanceof LinkedList);
Object l_elem;
l_elem = ((LinkedList<?>) o_des).get(0);
assertTrue(l_elem instanceof JsonString);
l_elem = ((LinkedList<?>) o_des).get(1);
assertTrue(l_elem instanceof JsonString);
l_elem = ((LinkedList<?>) o_des).get(2);
assertTrue(l_elem instanceof JsonNumber);
}
示例3: testSet1
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Test
public void testSet1() throws SerializerException
{
Set<Integer> obj_a = new HashSet<Integer>();
obj_a.add(1);
obj_a.add(2);
obj_a.add(3);
JsonElement e = m_serializer.serialize(obj_a);
assertNotNull(e);
assertTrue("Serialized element should be a list, got a " + e.getClass().getSimpleName(), e instanceof JsonList);
JsonList e_list = (JsonList) e;
assertEquals(3, e_list.size());
Object o_des = m_serializer.deserializeAs(e, HashSet.class);
assertNotNull(o_des);
assertTrue("Object should be an instance of HashSet, got " + o_des.getClass().getSimpleName(), o_des instanceof HashSet);
}
示例4: testMap1
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Test
public void testMap1() throws SerializerException
{
Map<String,Integer> obj_a = new HashMap<String,Integer>();
obj_a.put("a", 1);
obj_a.put("b", 2);
obj_a.put("c", 3);
JsonElement e = m_serializer.serialize(obj_a);
assertNotNull(e);
assertTrue("Serialized element should be a list, got a " + e.getClass().getSimpleName(), e instanceof JsonList);
JsonList e_list = (JsonList) e;
assertEquals(3, e_list.size());
Object o_des = m_serializer.deserializeAs(e, HashMap.class);
assertNotNull(o_des);
assertTrue("Object should be an instance of HashMap, got " + o_des.getClass().getSimpleName(), o_des instanceof HashMap);
@SuppressWarnings("unchecked")
HashMap<String,Integer> o_map = (HashMap<String,Integer>) o_des;
assertEquals(1, (int) o_map.get("a"));
}
示例5: testJson1
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Test
public void testJson1() throws SerializerException
{
JsonMap jm = new JsonMap();
jm.put("a", 1);
JsonList jl = new JsonList();
jl.add("b");
jm.put("z", jl);
JsonElement e = m_serializer.serialize(jm);
assertNotNull(e);
assertTrue("Serialized element should be a map, got a " + e.getClass().getSimpleName(), e instanceof JsonMap);
Object o_des = m_serializer.deserializeAs(e, JsonMap.class);
assertNotNull(o_des);
assertTrue("Object should be an instance of JsonMap, got " + o_des.getClass().getSimpleName(), o_des instanceof JsonMap);
JsonMap o_map = (JsonMap) o_des;
assertEquals(1, (int) o_map.getNumber("a").intValue());
}
示例6: getTypeInfo
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Override
public ca.uqac.lif.azrael.GenericSerializer.TypeInfo<JsonElement> getTypeInfo(JsonElement in, Class<?> target_class) throws ClassNotFoundException
{
TypeInfo<JsonElement> info = new TypeInfo<JsonElement>();
info.e = in;
info.clazz = target_class;
if (in instanceof JsonMap)
{
JsonMap map = (JsonMap) in;
if (map.containsKey(s_classAttribute))
{
String class_name = ((JsonString) map.get(s_classAttribute)).stringValue();
info.clazz = findClass(class_name);
}
}
if (in instanceof JsonNull)
{
info.clazz = null;
}
return info;
}
示例7: getElements
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Override
protected List<JsonElement> getElements(JsonElement e) throws SerializerException
{
JsonElement je = m_serializer.unwrapTypeInfo(e);
if (je == null || !(je instanceof JsonList))
{
throw new SerializerException("Input must be a JSON list");
}
JsonList j_list = (JsonList) je;
List<JsonElement> elements = new LinkedList<JsonElement>();
for (JsonElement el : j_list)
{
elements.add(el);
}
return elements;
}
示例8: getElements
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Override
protected Set<JsonElement> getElements(JsonElement e) throws SerializerException
{
JsonElement je = m_serializer.unwrapTypeInfo(e);
if (je == null || !(je instanceof JsonList))
{
throw new SerializerException("Input must be a JSON list");
}
JsonList j_list = (JsonList) je;
Set<JsonElement> elements = new HashSet<JsonElement>();
for (JsonElement el : j_list)
{
elements.add(el);
}
return elements;
}
示例9: deserializeAs
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Override
public Number deserializeAs(JsonElement e, Class<?> clazz)
{
if (e != null && e instanceof JsonNumber)
{
if (clazz.equals(Integer.class))
{
return ((JsonNumber) e).numberValue().intValue();
}
else if (clazz.equals(Long.class))
{
return ((JsonNumber) e).numberValue().longValue();
}
else if (clazz.equals(Float.class))
{
return ((JsonNumber) e).numberValue().floatValue();
}
else if (clazz.equals(Double.class))
{
return ((JsonNumber) e).numberValue().doubleValue();
}
return ((JsonNumber) e).numberValue();
}
return null;
}
示例10: saveToJson
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
/**
* Saves the state of the lab to a JSON element
* @return The JSON element with the assistant's state, or null
* if some error occurred
*/
public JsonElement saveToJson()
{
/* NOTE: this method should instead throw the exception and let
* higher levels of the GUI handle it, rather than silently fail
*/
try
{
JsonElement js_out = m_serializer.serializeAs(this, this.getClass());
return js_out;
}
catch (SerializerException e)
{
// Do nothing
}
return null;
}
示例11: toLatex
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
/**
* Exports the contents of this data point as a LaTeX command
* @return The string defining the command
*/
public String toLatex(boolean with_comments)
{
StringBuilder out = new StringBuilder();
if (with_comments)
{
out.append("% ").append(m_name).append(FileHelper.CRLF);
out.append("% ").append(m_description).append(FileHelper.CRLF);
}
JsonElement value = getValue();
out.append("\\newcommand{\\").append(m_name).append("}{\\href{").append(MacroNode.getDatapointId(this, "")).append("}{");
if (value instanceof JsonString)
{
out.append(LatexTableRenderer.escape(((JsonString) value).stringValue()));
}
else
{
out.append(LatexTableRenderer.escape(value.toString()));
}
out.append("}}").append(FileHelper.CRLF).append(FileHelper.CRLF);
return out.toString();
}
示例12: jsonCast
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
/**
* Casts a value into the most appropriate JSON element type. The method
* uses the following rules:
* <ul>
* <li>Any {@code JsonElement} is left as is</li>
* <li>A {@code null} becomes a {@code JsonNull}</li>
* <li>Any {@code Number} becomes a {@code JsonNumber}</li>
* <li>Any {@code String} is first attempted to be interpreted as a
* number; if so, it becomes a {@code JsonNumber}; otherwise, it becomes
* a {@code JsonString}</li>
* <li>Anything else is converted to a {@code JsonString} using the
* object's {@code toString()} method</li>
* </ul>
* @param o The value
* @return The corresponding JSON element
*/
public static JsonElement jsonCast(Object o)
{
if (o == null)
return JsonNull.instance;
if (o instanceof JsonElement)
return (JsonElement) o;
if (o instanceof Number)
return new JsonNumber((Number) o);
if (o instanceof String)
{
Number n = stringToNumber((String) o);
if (n == null)
{
return new JsonString((String) o);
}
return new JsonNumber(n);
}
return new JsonString(o.toString());
}
示例13: getValue
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Override
public JsonElement getValue()
{
float longest_time = 0f;
String algo_name = "None";
for (Experiment e : m_lab.getExperiments())
{
float time = e.readFloat("time");
if (time > longest_time)
{
longest_time = time;
algo_name = e.readString("name");
}
}
return new JsonString(algo_name);
}
示例14: testPrimitiveString1
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Test
public void testPrimitiveString1() throws SerializerException
{
String obj_a = "abc";
JsonElement e = m_serializer.serialize(obj_a);
assertNotNull(e);
assertTrue("Serialized element should be a String", e instanceof JsonString);
JsonString e_map = (JsonString) e;
assertEquals(3, e_map.stringValue().length());
}
示例15: testPrimitiveString2
import ca.uqac.lif.json.JsonElement; //导入依赖的package包/类
@Test
public void testPrimitiveString2() throws SerializerException
{
String obj_a = "";
JsonElement e = m_serializer.serialize(obj_a);
assertNotNull(e);
assertTrue("Serialized element should be a String", e instanceof JsonString);
JsonString e_map = (JsonString) e;
assertEquals(0, e_map.stringValue().length());
}