Package siena

Examples of siena.Json


    assertEquals("bar", list.at(1).at(1).str());
    assertEquals("baz", list.at(1).at(2).str());
  }
 
  public void testAddAll() {
    Json json = Json.list(1, 2, 3);
    json.addAll(Json.list(4, 5, 6));
   
    assertEquals(6, json.size());
  }
View Full Code Here


   
    assertEquals(6, json.size());
  }
 
  public void testAddAt() {
    Json json = Json.list(1, 3);
    json.addAt(1, 2);
   
    assertEquals(3, json.size());
    assertEquals(1, json.at(0).asInt());
    assertEquals(2, json.at(1).asInt());
    assertEquals(3, json.at(2).asInt());
  }
View Full Code Here

 
  public void testDumpQueryOption() {
    Query<PersonLongAutoID> query = pm.createQuery(PersonLongAutoID.class);
   
    QueryOption opt = query.option(QueryOptionPage.ID);
    Json dump = opt.dump();
    String str = JsonSerializer.serialize(dump).toString();
    assertNotNull(str);
    assertEquals("{\"value\": {\"pageType\": \"TEMPORARY\", \"state\": \"PASSIVE\", \"pageSize\": 0, \"type\": 1}, \"type\": \""+QueryOptionPage.class.getName()+"\"}", str);
  }
View Full Code Here

  public static Json serialize(Object obj) {
    return serialize(obj, null);
  }
 
  public static Json serialize(Object obj, Field f) {
    if(obj == null) return new Json(null);
   
    Class<?> clazz = obj.getClass();
   
    //if(obj instanceof Map<?, ?>) {
    if(Map.class.isAssignableFrom(clazz)){
      Map<?, ?> map = (Map<?, ?>) obj;
      Json result = map();
      for (Map.Entry<?, ?> entry : map.entrySet()) {
        String key = entry.getKey().toString();
        Json value = serialize(entry.getValue(), null);
        result.put(key, value);
      }
      return result;
    }

    //if(obj instanceof Collection<?>) {
    if(Collection.class.isAssignableFrom(clazz)){
      Json result = list();
      Collection<?> col = (Collection<?>) obj;
      for (Object object : col) {
        result.add(serialize(object));
      }
      return result;
    }

    if(Json.class.isAssignableFrom(clazz)){
      return new Json(obj);
    }
   
    if(Field.class.isAssignableFrom(clazz)){
      return new Json(obj);
    }
   
    if(clazz.isArray()){
      return new Json(obj);
    }
   
    if(clazz == Class.class){
      return new Json(obj);
    }
   
    if(JsonDumpable.class.isAssignableFrom(obj.getClass())) {
      return ((JsonDumpable)obj).dump();
    }

    try {
      EmbeddedList list = obj.getClass().getAnnotation(EmbeddedList.class);
      if(list != null) {
        return serializeList(obj);
      }
      EmbeddedMap map = obj.getClass().getAnnotation(EmbeddedMap.class);
      if(map != null) {
        return serializeMap(obj);
      }
    } catch(SienaException e) {
      throw e;
    } catch(Exception e) {
      throw new SienaException(e);
    }
   
    if(f != null) {
      Format format = f.getAnnotation(Format.class);
      if(format != null) {
        if(obj.getClass() == Date.class) {
          Date date = (Date) obj;
          SimpleDateFormat sdf = new SimpleDateFormat(format.value());
          return new Json(sdf.format(date));
        }
      }
    }
   
    return new Json(obj);
  }
View Full Code Here

    return new Json(obj);
  }
 
  public static Json serializeMap(Object obj) throws Exception {
    Field[] fields = obj.getClass().getDeclaredFields();
    Json result = map();
    for (Field f : fields) {
      if(mustIgnore(f)) continue;
     
      Key k = f.getAnnotation(Key.class);
      if(k != null) {
        result.put(k.value(), serialize(f.get(obj), f));
      } else {
        result.put(f.getName(), serialize(f.get(obj), f));
      }
    }
   
    // TEST
    // serializes super classes
    Class<?> clazz = obj.getClass().getSuperclass();
    while(clazz!=null){
      fields = clazz.getDeclaredFields();
      for (Field f : fields) {
        if(mustIgnore(f)) continue;
       
        Key k = f.getAnnotation(Key.class);
        if(k != null) {
          result.put(k.value(), serialize(Util.readField(obj, f), f));
        } else {
          result.put(f.getName(), serialize(Util.readField(obj, f), f));
        }
      }
      clazz = clazz.getSuperclass();
    }
    // TEST
View Full Code Here

    return result;
  }
 
  public static Json serializeList(Object obj) throws Exception {
    Field[] fields = obj.getClass().getDeclaredFields();
    Json result = list();
    for (Field f : fields) {
      if(mustIgnore(f)) continue;
     
      At at = f.getAnnotation(At.class);
      if(at == null) throw new SienaException("Field "+obj.getClass()+"."+f.getName()+" must be annotated with @At(n)");
      result.addAt(at.value(), serialize(f.get(obj), f));
    }
   
    // TEST
    // serializes super classes
    Class<?> clazz = obj.getClass().getSuperclass();
    while(clazz!=null){
      fields = clazz.getDeclaredFields();
      for (Field f : fields) {
        if(mustIgnore(f)) continue;
       
        At at = f.getAnnotation(At.class);
        if(at == null) throw new SienaException("Field "+obj.getClass()+"."+f.getName()+" must be annotated with @At(n)");
        result.addAt(at.value(), serialize(f.get(obj), f));
      }
      clazz = clazz.getSuperclass();
    }
    // TEST
    return result;
View Full Code Here

        for (Field f : fields) {
          if(mustIgnore(f)) continue;
         
          At at = f.getAnnotation(At.class);
          if(at == null) throw new SienaException("Field "+obj.getClass()+"."+f.getName()+" must be annotated with @At(n)");
          Json value = data.at(at.value());
          Util.setField(obj, f, deserialize(f, value));
        }
       
        // deserializes super classes
        Class<?> superclazz = obj.getClass().getSuperclass();
        while(superclazz!=null){
          fields = superclazz.getDeclaredFields();
          for (Field f : fields) {
            if(mustIgnore(f)) continue;
           
            At at = f.getAnnotation(At.class);
            if(at == null) throw new SienaException("Field "+obj.getClass()+"."+f.getName()+" must be annotated with @At(n)");
            Json value = data.at(at.value());
            Util.setField(obj, f, deserialize(f, value));
          }
          superclazz = superclazz.getSuperclass();
        }
        return obj;
View Full Code Here

    }

    if(field.getAnnotation(Embedded.class) != null && value != null && java.sql.Clob.class.isAssignableFrom(value.getClass())) {
      java.sql.Clob clob = (java.sql.Clob)value;
      try {
        Json data = Json.load(new BufferedReader(clob.getCharacterStream()));
        return JsonSerializer.deserialize(field, data);
      } catch (SQLException e) {
        throw new SienaException(e);
      }
    }
View Full Code Here

            try {
              com.google.gson.JsonParser parser = new com.google.gson.JsonParser();
              parser.parse(jsonStr[0]);
             
              params.remove(name + "." + field.getName());
              Json json = Json.loads(jsonStr[0]);
              if(json!=null)
                bw.set(field.getName(), o, json);
              else Validation.addError(name+"."+field.getName(), "validation.notParsable");
            }catch(JsonParseException ex){
              ex.printStackTrace();
View Full Code Here

TOP

Related Classes of siena.Json

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.