1.1 --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Wed Apr 03 09:19:03 2013 +0200
1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Wed Apr 03 10:04:24 2013 +0200
1.3 @@ -146,6 +146,7 @@
1.4 writeStringArray(functions, w);
1.5 w.append(" );\n");
1.6 w.append(" };\n");
1.7 + writeToString(m.properties(), w);
1.8 w.append("}\n");
1.9 } finally {
1.10 w.close();
1.11 @@ -767,4 +768,22 @@
1.12 }
1.13 w.write("\n }");
1.14 }
1.15 +
1.16 + private void writeToString(Property[] props, Writer w) throws IOException {
1.17 + w.write(" public String toString() {\n");
1.18 + w.write(" StringBuilder sb = new StringBuilder();\n");
1.19 + w.write(" sb.append('{');\n");
1.20 + String sep = "";
1.21 + for (Property p : props) {
1.22 + w.write(sep);
1.23 + w.append(" sb.append(\"" + p.name() + ": \");\n");
1.24 + w.append(" sb.append('\"');");
1.25 + w.append(" sb.append(prop_").append(p.name()).append(");\n");
1.26 + w.append(" sb.append('\"');");
1.27 + sep = " sb.append(',');\n";
1.28 + }
1.29 + w.write(" sb.append('}');\n");
1.30 + w.write(" return sb.toString();\n");
1.31 + w.write(" }\n");
1.32 + }
1.33 }
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/javaquery/api/src/test/java/org/apidesign/bck2brwsr/htmlpage/JSONTest.java Wed Apr 03 10:04:24 2013 +0200
2.3 @@ -0,0 +1,58 @@
2.4 +/**
2.5 + * Back 2 Browser Bytecode Translator
2.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
2.7 + *
2.8 + * This program is free software: you can redistribute it and/or modify
2.9 + * it under the terms of the GNU General Public License as published by
2.10 + * the Free Software Foundation, version 2 of the License.
2.11 + *
2.12 + * This program is distributed in the hope that it will be useful,
2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2.15 + * GNU General Public License for more details.
2.16 + *
2.17 + * You should have received a copy of the GNU General Public License
2.18 + * along with this program. Look for COPYING file in the top folder.
2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
2.20 + */
2.21 +package org.apidesign.bck2brwsr.htmlpage;
2.22 +
2.23 +import java.util.Iterator;
2.24 +import org.json.JSONException;
2.25 +import org.json.JSONObject;
2.26 +import org.json.JSONTokener;
2.27 +import org.testng.annotations.Test;
2.28 +import static org.testng.Assert.*;
2.29 +
2.30 +/** Need to verify that models produce reasonable JSON objects.
2.31 + *
2.32 + * @author Jaroslav Tulach <jtulach@netbeans.org>
2.33 + */
2.34 +public class JSONTest {
2.35 +
2.36 + @Test public void personToString() throws JSONException {
2.37 + Person p = new Person();
2.38 + p.setSex(Sex.MALE);
2.39 + p.setFirstName("Jarda");
2.40 + p.setLastName("Tulach");
2.41 +
2.42 + JSONTokener t = new JSONTokener(p.toString());
2.43 + JSONObject o;
2.44 + try {
2.45 + o = new JSONObject(t);
2.46 + } catch (JSONException ex) {
2.47 + throw new AssertionError("Can't parse " + p.toString(), ex);
2.48 + }
2.49 +
2.50 + Iterator it = o.sortedKeys();
2.51 + assertEquals(it.next(), "firstName");
2.52 + assertEquals(it.next(), "lastName");
2.53 + assertEquals(it.next(), "sex");
2.54 +
2.55 + assertEquals(o.getString("firstName"), "Jarda");
2.56 + assertEquals(o.getString("lastName"), "Tulach");
2.57 + assertEquals(o.getString("sex"), "MALE");
2.58 + }
2.59 +
2.60 +
2.61 +}