# HG changeset patch # User Jaroslav Tulach # Date 1364981872 -7200 # Node ID cd0a40987abb28fc77656810b03f2a05a9057633 # Parent e2977ec1ef6e0c5c43a308fe8fab9523cb91dc77 Persists arrays to JSON diff -r e2977ec1ef6e -r cd0a40987abb javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java Wed Apr 03 10:20:17 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/KOList.java Wed Apr 03 11:37:52 2013 +0200 @@ -19,6 +19,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import org.apidesign.bck2brwsr.core.JavaScriptOnly; /** @@ -92,7 +93,25 @@ notifyChange(); return ret; } - + + @Override + public String toString() { + Iterator it = iterator(); + if (!it.hasNext()) { + return "[]"; + } + String sep = ""; + StringBuilder sb = new StringBuilder(); + sb.append('['); + while (it.hasNext()) { + T t = it.next(); + sb.append(sep); + sb.append(ConvertTypes.toJSON(t)); + sep = ","; + } + sb.append(']'); + return sb.toString(); + } @JavaScriptOnly(name = "koArray", value = "function() { return this.toArray___3Ljava_lang_Object_2(); }") diff -r e2977ec1ef6e -r cd0a40987abb javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java --- a/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Wed Apr 03 10:20:17 2013 +0200 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Wed Apr 03 11:37:52 2013 +0200 @@ -77,5 +77,35 @@ assertEquals(o.get("sex"), JSONObject.NULL); } - + @Test public void personsInArray() throws JSONException { + Person p1 = new Person(); + p1.setFirstName("One"); + + Person p2 = new Person(); + p2.setFirstName("Two"); + + People arr = new People(); + arr.getInfo().add(p1); + arr.getInfo().add(p2); + arr.getNicknames().add("Prvn\u00ed k\u016f\u0148"); + final String n2 = "Druh\u00fd hlem\u00fd\u017e\u010f, star\u0161\u00ed"; + arr.getNicknames().add(n2); + arr.getAge().add(33); + arr.getAge().add(73); + + + final String json = arr.toString(); + + JSONTokener t = new JSONTokener(json); + JSONObject o; + try { + o = new JSONObject(t); + } catch (JSONException ex) { + throw new AssertionError("Can't parse " + json, ex); + } + + assertEquals(o.getJSONArray("info").getJSONObject(0).getString("firstName"), "One"); + assertEquals(o.getJSONArray("nicknames").getString(1), n2); + assertEquals(o.getJSONArray("age").getInt(1), 73); + } } diff -r e2977ec1ef6e -r cd0a40987abb javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PeopleImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/PeopleImpl.java Wed Apr 03 11:37:52 2013 +0200 @@ -0,0 +1,34 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage; + +import org.apidesign.bck2brwsr.htmlpage.api.Model; +import org.apidesign.bck2brwsr.htmlpage.api.Property; + +/** + * + * @author Jaroslav Tulach + */ +@Model(className = "People", properties = { + @Property(array = true, name = "info", type = PersonImpl.class), + @Property(array = true, name = "nicknames", type = String.class), + @Property(array = true, name = "age", type = int.class), +}) +public class PeopleImpl { + +}