Package com.avaje.ebean.text.json

Examples of com.avaje.ebean.text.json.JsonContext


    ResetBasicData.reset();

    List<Customer> list = Ebean.find(Customer.class).select("id, status, shippingAddress")
        .fetch("contacts").order().desc("id").findList();

    JsonContext json = Ebean.json();

    // test that lazy loading on
    PathProperties pp = PathProperties.parse("(id,name,contacts(firstName))");
    JsonWriteOptions o = new JsonWriteOptions();
    o.setPathProperties(pp);

    System.out.println("expecting lazy load of Customer beans to fetch customer name");
    String s = json.toJson(list, o);
    System.out.println(s);
    Assert.assertTrue(s.contains("\"contacts\""));
    Assert.assertTrue(s.contains("\"name\""));

  }
View Full Code Here


    List<Customer> list = Ebean.find(Customer.class).select("id, name, status, shippingAddress")
    // .fetch("contacts")
        .order().desc("id").findList();

    JsonContext json = Ebean.json();

    // test that lazy loading on
    PathProperties pp = PathProperties.parse("(id,name,contacts(firstName))");
    JsonWriteOptions o = new JsonWriteOptions();
    o.setPathProperties(pp);

    System.out.println("expecting lazy load of Customer contacts ");
    String s = json.toJson(list, o);
    System.out.println(s);
    Assert.assertTrue(s.contains("\"contacts\""));
    Assert.assertTrue(s.contains("\"name\""));

  }
View Full Code Here

    Cat cat = new Cat();
    cat.setName("Gemma");

    Ebean.save(cat);

    JsonContext json = Ebean.json();
    String jsonContent = json.toJson(cat);

    Cat cat2 = json.toBean(Cat.class, jsonContent);

    Assert.assertEquals(cat.getId(), cat2.getId());
    Assert.assertEquals(cat.getName(), cat2.getName());
    Assert.assertEquals(cat.getVersion(), cat2.getVersion());

    String noDiscriminator = "{\"id\":1,\"name\":\"Gemma\",\"version\":1}";

    Cat cat3 = json.toBean(Cat.class, noDiscriminator);

    Assert.assertEquals(1L, cat3.getId().longValue());
    Assert.assertEquals("Gemma", cat3.getName());
    Assert.assertEquals(1L, cat3.getVersion().longValue());

    Dog dog = new Dog();
    dog.setRegistrationNumber("ABC123");
    dog.setDateOfBirth(new Date(System.currentTimeMillis()));

    Ebean.save(dog);

    List<Animal> animals = Ebean.find(Animal.class).findList();

    String listJson = json.toJson(animals);

    List<Animal> animals2 = json.toList(Animal.class, listJson);
    Assert.assertEquals(animals.size(), animals2.size());

    String noDiscList = "[{\"id\":1,\"name\":\"Gemma\",\"version\":1},{\"name\":\"PussCat\",\"version\":1},{\"species\":\"CAT\",\"name\":\"PussCat\",\"version\":1}]";
    List<Cat> cats = json.toList(Cat.class, noDiscList);
    Assert.assertEquals(cats.size(), 3);

  }
View Full Code Here

        .fetch("contacts", "firstName,email")
        .order().desc("id").findList();

    EbeanServer server = Ebean.getServer(null);

    JsonContext json = server.json();

    String jsonOutput = json.toJson(list);
    System.out.println(jsonOutput);

    List<Customer> mList = json.toList(Customer.class, jsonOutput);

    Assert.assertEquals(list.size(), mList.size());
  }
View Full Code Here

    List<Customer> list = Ebean.find(Customer.class).select("id, name").order().desc("id")
        .findList();

    EbeanServer server = Ebean.getServer(null);

    JsonContext json = server.json();

    if (list.size() > 1) {
      Customer customer = list.get(0);

      String s = json.toJson(customer);
      System.out.println(s);
      int statusPos = s.indexOf("status");
      Assert.assertEquals(-1, statusPos);
    }
View Full Code Here

    List<Vehicle> list = Ebean.find(Vehicle.class).setAutofetch(false).findList();

    Assert.assertEquals(2, list.size());

    JsonContext jsonContext = Ebean.json();
    String jsonString = jsonContext.toJson(list);
    System.out.println(jsonString);

    List<Vehicle> rebuiltList = jsonContext.toList(Vehicle.class, jsonString);

    Assert.assertEquals(2, rebuiltList.size());

  }
View Full Code Here

    Map<String, String> m = new LinkedHashMap<String, String>();
    m.put("hello", "rob");
    m.put("test", "me");

    JsonContext jsonContext = Ebean.json();
    String jsonString = jsonContext.toJson(m);
    System.out.println(jsonString);

    String s = "{\"parishId\":\"18\",\"contentId\":null,\"contentStatus\":null,\"contentType\":\"pg-hello\",\"content\":\"asd\"}";

    Object jsonElement = EJson.parse(s);
View Full Code Here

    BeanWithTimeZone bean = new BeanWithTimeZone();
    bean.setName("foo");
    bean.setTimezone(TimeZone.getDefault());

    JsonContext jsonContext = Ebean.json();
    String jsonContent = jsonContext.toJson(bean);

    BeanWithTimeZone bean2 = jsonContext.toBean(BeanWithTimeZone.class, jsonContent);

    Assert.assertEquals(bean.getTimezone(), bean2.getTimezone());

    Ebean.save(bean);
    BeanWithTimeZone bean3 = Ebean.find(BeanWithTimeZone.class, bean.getId());
View Full Code Here

    ResetBasicData.reset();

    SpiEbeanServer server = (SpiEbeanServer)Ebean.getServer(null);
   
    JsonContext jsonContext = Ebean.json();

    Product product = Ebean.getReference(Product.class, 1);

    BeanState beanState0 = Ebean.getBeanState(product);
    if (!beanState0.isReference()) {
      // got a cached value from beanCache

    } else {

      String jsonString = jsonContext.toJson(product);
      System.out.println(jsonString);

      Product refProd = jsonContext.toBean(Product.class, jsonString);

      BeanDescriptor<Product> prodDesc = server.getBeanDescriptor(Product.class);
      EntityBean eb = (EntityBean)refProd;
      prodDesc.isReference(eb._ebean_getIntercept());
     
      BeanState beanState = Ebean.getBeanState(refProd);
      Assert.assertTrue(beanState.isNew());
     
      String name = refProd.getName();
      Assert.assertNull(name);

      // Set to be 'loaded' to invoke lazy loading
      beanState.setLoaded();
      String name2 = refProd.getName();
      Assert.assertNotNull(name2);

    }

    List<Order> orders = Ebean.find(Order.class)
    // .setUseCache(false)
        .select("status, orderDate, shipDate, customer").fetch("details", "*")
        // .fetch("details.product","id")
        .order().asc("id").findList();

    Order order = orders.get(0);

    JsonWriteOptions options = JsonWriteOptions.parsePath("*,details(id,orderQty,product(id))");

    String jsonOrder = jsonContext.toJson(order, options);
    System.out.println(jsonOrder);

    Order o2 = jsonContext.toBean(Order.class, jsonOrder);
    Customer customer = o2.getCustomer();
   
    BeanDescriptor<Customer> custDesc = server.getBeanDescriptor(Customer.class);

    Assert.assertTrue(custDesc.isReference(((EntityBean)customer)._ebean_getIntercept()));
View Full Code Here

TOP

Related Classes of com.avaje.ebean.text.json.JsonContext

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.