1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java Wed Apr 03 10:20:17 2013 +0200
1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java Wed Apr 03 11:37:52 2013 +0200
1.3 @@ -19,6 +19,7 @@
1.4
1.5 import java.util.ArrayList;
1.6 import java.util.Collection;
1.7 +import java.util.Iterator;
1.8 import org.apidesign.bck2brwsr.core.JavaScriptOnly;
1.9
1.10 /**
1.11 @@ -92,7 +93,25 @@
1.12 notifyChange();
1.13 return ret;
1.14 }
1.15 -
1.16 +
1.17 + @Override
1.18 + public String toString() {
1.19 + Iterator<T> it = iterator();
1.20 + if (!it.hasNext()) {
1.21 + return "[]";
1.22 + }
1.23 + String sep = "";
1.24 + StringBuilder sb = new StringBuilder();
1.25 + sb.append('[');
1.26 + while (it.hasNext()) {
1.27 + T t = it.next();
1.28 + sb.append(sep);
1.29 + sb.append(ConvertTypes.toJSON(t));
1.30 + sep = ",";
1.31 + }
1.32 + sb.append(']');
1.33 + return sb.toString();
1.34 + }
1.35
1.36
1.37 @JavaScriptOnly(name = "koArray", value = "function() { return this.toArray___3Ljava_lang_Object_2(); }")
2.1 --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Wed Apr 03 10:20:17 2013 +0200
2.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Wed Apr 03 11:37:52 2013 +0200
2.3 @@ -77,5 +77,35 @@
2.4 assertEquals(o.get("sex"), JSONObject.NULL);
2.5 }
2.6
2.7 -
2.8 + @Test public void personsInArray() throws JSONException {
2.9 + Person p1 = new Person();
2.10 + p1.setFirstName("One");
2.11 +
2.12 + Person p2 = new Person();
2.13 + p2.setFirstName("Two");
2.14 +
2.15 + People arr = new People();
2.16 + arr.getInfo().add(p1);
2.17 + arr.getInfo().add(p2);
2.18 + arr.getNicknames().add("Prvn\u00ed k\u016f\u0148");
2.19 + final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed";
2.20 + arr.getNicknames().add(n2);
2.21 + arr.getAge().add(33);
2.22 + arr.getAge().add(73);
2.23 +
2.24 +
2.25 + final String json = arr.toString();
2.26 +
2.27 + JSONTokener t = new JSONTokener(json);
2.28 + JSONObject o;
2.29 + try {
2.30 + o = new JSONObject(t);
2.31 + } catch (JSONException ex) {
2.32 + throw new AssertionError("Can't parse " + json, ex);
2.33 + }
2.34 +
2.35 + assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One");
2.36 + assertEquals(o.getJSONArray("nicknames").getString(1), n2);
2.37 + assertEquals(o.getJSONArray("age").getInt(1), 73);
2.38 + }
2.39 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PeopleImpl.java Wed Apr 03 11:37:52 2013 +0200
3.3 @@ -0,0 +1,34 @@
3.4 +/**
3.5 + * Back 2 Browser Bytecode Translator
3.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
3.7 + *
3.8 + * This program is free software: you can redistribute it and/or modify
3.9 + * it under the terms of the GNU General Public License as published by
3.10 + * the Free Software Foundation, version 2 of the License.
3.11 + *
3.12 + * This program is distributed in the hope that it will be useful,
3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3.15 + * GNU General Public License for more details.
3.16 + *
3.17 + * You should have received a copy of the GNU General Public License
3.18 + * along with this program. Look for COPYING file in the top folder.
3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
3.20 + */
3.21 +package org.apidesign.bck2brwsr.htmlpage;
3.22 +
3.23 +import org.apidesign.bck2brwsr.htmlpage.api.Model;
3.24 +import org.apidesign.bck2brwsr.htmlpage.api.Property;
3.25 +
3.26 +/**
3.27 + *
3.28 + * @author Jaroslav Tulach <jtulach@netbeans.org>
3.29 + */
3.30 +@Model(className = "People", properties = {
3.31 + @Property(array = true, name = "info", type = PersonImpl.class),
3.32 + @Property(array = true, name = "nicknames", type = String.class),
3.33 + @Property(array = true, name = "age", type = int.class),
3.34 +})
3.35 +public class PeopleImpl {
3.36 +
3.37 +}