1.1 --- a/json-beans/src/main/java/net/java/html/beans/JSONBeans.java Mon Aug 04 07:10:17 2014 +0200
1.2 +++ b/json-beans/src/main/java/net/java/html/beans/JSONBeans.java Mon Aug 04 14:25:41 2014 +0200
1.3 @@ -56,6 +56,8 @@
1.4 import java.lang.reflect.Method;
1.5 import java.util.HashMap;
1.6 import java.util.Map;
1.7 +import java.util.logging.Level;
1.8 +import java.util.logging.Logger;
1.9 import net.java.html.BrwsrCtx;
1.10 import net.java.html.json.Function;
1.11 import net.java.html.json.Model;
1.12 @@ -76,6 +78,8 @@
1.13 * @since 0.9
1.14 */
1.15 public class JSONBeans {
1.16 + private static final Logger LOG = Logger.getLogger(JSONBeans.class.getName());
1.17 +
1.18 private JSONBeans() {
1.19 }
1.20
1.21 @@ -115,6 +119,7 @@
1.22 private final PropertyDescriptor[] properties;
1.23 private final MethodDescriptor[] methods;
1.24 private final Method addPCL;
1.25 + private final Class<?> javaBeanClass;
1.26
1.27 Html4JavaType(
1.28 Class<?> javaBeanClass,
1.29 @@ -123,6 +128,7 @@
1.30 Method addPCL
1.31 ) {
1.32 super(javaBeanClass, javaBeanClass, pd.length, md.length);
1.33 + this.javaBeanClass = javaBeanClass;
1.34 this.properties = pd;
1.35 this.methods = md;
1.36 for (int i = 0; i < pd.length; i++) {
1.37 @@ -204,7 +210,35 @@
1.38
1.39 @Override
1.40 protected Object read(BrwsrCtx c, Object json) {
1.41 - throw new UnsupportedOperationException("Convert: " + json);
1.42 + try {
1.43 + Object bean = javaBeanClass.newInstance();
1.44 + Proto proto = findProto(bean, this, c);
1.45 +
1.46 + String[] names = new String[properties.length];
1.47 + for (int i = 0; i < names.length; i++) {
1.48 + if (properties[i].getWriteMethod() != null) {
1.49 + names[i] = properties[i].getName();
1.50 + }
1.51 + }
1.52 + Object[] values = new Object[properties.length];
1.53 + proto.extract(json, names, values);
1.54 + for (int i = 0; i < names.length; i++) {
1.55 + if (names[i] != null && values[i] != null) {
1.56 + try {
1.57 + properties[i].getWriteMethod().invoke(bean, values[i]);
1.58 + } catch (IllegalArgumentException ex) {
1.59 + LOG.log(Level.SEVERE, "Cannot set property " + names[i] + " of " + javaBeanClass.getName(), ex);
1.60 + } catch (InvocationTargetException ex) {
1.61 + LOG.log(Level.SEVERE, "Cannot set property " + names[i] + " of " + javaBeanClass.getName(), ex);
1.62 + }
1.63 + }
1.64 + }
1.65 + return bean;
1.66 + } catch (InstantiationException ex) {
1.67 + throw toRuntime(ex, RuntimeException.class);
1.68 + } catch (IllegalAccessException ex) {
1.69 + throw toRuntime(ex, RuntimeException.class);
1.70 + }
1.71 }
1.72
1.73 @Override
2.1 --- a/json-beans/src/test/java/net/java/html/beans/MapModelTest.java Mon Aug 04 07:10:17 2014 +0200
2.2 +++ b/json-beans/src/test/java/net/java/html/beans/MapModelTest.java Mon Aug 04 14:25:41 2014 +0200
2.3 @@ -43,6 +43,7 @@
2.4 package net.java.html.beans;
2.5
2.6 import java.beans.IntrospectionException;
2.7 +import java.io.ByteArrayInputStream;
2.8 import java.io.IOException;
2.9 import java.io.InputStream;
2.10 import java.lang.reflect.InvocationTargetException;
2.11 @@ -205,6 +206,17 @@
2.12 assertEquals(p.getSex(), Sex.FEMALE, "Changed");
2.13 }
2.14
2.15 + @Test public void storeAndRead() throws Exception {
2.16 + Person p = Models.bind(new Person(), c);
2.17 + p.setFirstName("Written");
2.18 +
2.19 + ByteArrayInputStream is = new ByteArrayInputStream(p.toString().getBytes("UTF-8"));
2.20 + Person p2 = Models.parse(c, Person.class, is);
2.21 +
2.22 + assertNotSame(p, p2);
2.23 + assertEquals(p.getFirstName(), p2.getFirstName(), "Should have the same values");
2.24 + }
2.25 +
2.26 @Test public void workWithArray() throws Exception {
2.27 People people = Models.bind(new People(), c);
2.28 Person p1 = Models.bind(new Person(), c);
2.29 @@ -326,7 +338,9 @@
2.30
2.31 @Override
2.32 public Object toJSON(InputStream is) throws IOException {
2.33 - throw new IOException();
2.34 + HashMap<String,Object> map = new HashMap<String, Object>();
2.35 + map.put("firstName", "Written");
2.36 + return map;
2.37 }
2.38
2.39 @Override