1.1 --- a/json/src/main/java/org/apidesign/html/json/impl/JSON.java Tue Sep 17 12:20:40 2013 +0200
1.2 +++ b/json/src/main/java/org/apidesign/html/json/impl/JSON.java Wed Sep 18 13:19:55 2013 +0200
1.3 @@ -139,10 +139,50 @@
1.4 if (Boolean.class == type) {
1.5 val = boolValue(val);
1.6 }
1.7 + if (String.class == type) {
1.8 + val = stringValue(val);
1.9 + }
1.10 + if (Character.class == type) {
1.11 + val = charValue(val);
1.12 + }
1.13 + if (Integer.class == type) {
1.14 + val = val instanceof Number ? ((Number)val).intValue() : 0;
1.15 + }
1.16 + if (Long.class == type) {
1.17 + val = val instanceof Number ? ((Number)val).longValue() : 0;
1.18 + }
1.19 + if (Short.class == type) {
1.20 + val = val instanceof Number ? ((Number)val).shortValue() : 0;
1.21 + }
1.22 + if (Byte.class == type) {
1.23 + val = val instanceof Number ? ((Number)val).byteValue() : 0;
1.24 + }
1.25 + if (Double.class == type) {
1.26 + val = val instanceof Number ? ((Number)val).doubleValue() : Double.NaN;
1.27 + }
1.28 + if (Float.class == type) {
1.29 + val = val instanceof Number ? ((Number)val).floatValue() : Float.NaN;
1.30 + }
1.31 return type.cast(val);
1.32 }
1.33
1.34 + protected static boolean isNumeric(Object val) {
1.35 + return ((val instanceof Integer) || (val instanceof Long) || (val instanceof Short) || (val instanceof Byte));
1.36 + }
1.37 +
1.38 public static String stringValue(Object val) {
1.39 + if (val instanceof Boolean) {
1.40 + return ((Boolean)val ? "true" : "false");
1.41 + }
1.42 + if (isNumeric(val)) {
1.43 + return Long.toString(((Number)val).longValue());
1.44 + }
1.45 + if (val instanceof Float) {
1.46 + return Float.toString((Float)val);
1.47 + }
1.48 + if (val instanceof Double) {
1.49 + return Double.toString((Double)val);
1.50 + }
1.51 return (String)val;
1.52 }
1.53
1.54 @@ -154,10 +194,23 @@
1.55 return Double.NaN;
1.56 }
1.57 }
1.58 + if (val instanceof Boolean) {
1.59 + return (Boolean)val ? 1 : 0;
1.60 + }
1.61 return (Number)val;
1.62 }
1.63
1.64 public static Character charValue(Object val) {
1.65 + if (val instanceof Number) {
1.66 + return Character.toChars(numberValue(val).intValue())[0];
1.67 + }
1.68 + if (val instanceof Boolean) {
1.69 + return (Boolean)val ? (char)1 : (char)0;
1.70 + }
1.71 + if (val instanceof String) {
1.72 + String s = (String)val;
1.73 + return s.isEmpty() ? (char)0 : s.charAt(0);
1.74 + }
1.75 return (Character)val;
1.76 }
1.77
1.78 @@ -165,6 +218,10 @@
1.79 if (val instanceof String) {
1.80 return Boolean.parseBoolean((String)val);
1.81 }
1.82 + if (val instanceof Number) {
1.83 + return numberValue(val).doubleValue() != 0.0;
1.84 + }
1.85 +
1.86 return Boolean.TRUE.equals(val);
1.87 }
1.88
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/json/src/test/java/org/apidesign/html/json/impl/JSONTest.java Wed Sep 18 13:19:55 2013 +0200
2.3 @@ -0,0 +1,62 @@
2.4 +/**
2.5 + * HTML via Java(tm) Language Bindings
2.6 + * Copyright (C) 2013 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. apidesign.org
2.16 + * designates this particular file as subject to the
2.17 + * "Classpath" exception as provided by apidesign.org
2.18 + * in the License file that accompanied this code.
2.19 + *
2.20 + * You should have received a copy of the GNU General Public License
2.21 + * along with this program. Look for COPYING file in the top folder.
2.22 + * If not, see http://wiki.apidesign.org/wiki/GPLwithClassPathException
2.23 + */
2.24 +package org.apidesign.html.json.impl;
2.25 +
2.26 +import static org.testng.Assert.*;
2.27 +import org.testng.annotations.Test;
2.28 +
2.29 +/**
2.30 + *
2.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
2.32 + */
2.33 +public class JSONTest {
2.34 +
2.35 + public JSONTest() {
2.36 + }
2.37 +
2.38 + @Test public void longToStringValue() {
2.39 + assertEquals(JSON.stringValue(Long.valueOf(1)), "1");
2.40 + }
2.41 +
2.42 + @Test public void booleanIsSortOfNumber() {
2.43 + assertEquals(JSON.numberValue(Boolean.TRUE), Integer.valueOf(1));
2.44 + assertEquals(JSON.numberValue(Boolean.FALSE), Integer.valueOf(0));
2.45 + }
2.46 +
2.47 + @Test public void numberToChar() {
2.48 + assertEquals(JSON.charValue(65), Character.valueOf('A'));
2.49 + }
2.50 + @Test public void booleanToChar() {
2.51 + assertEquals(JSON.charValue(false), Character.valueOf((char)0));
2.52 + assertEquals(JSON.charValue(true), Character.valueOf((char)1));
2.53 + }
2.54 + @Test public void stringToChar() {
2.55 + assertEquals(JSON.charValue("Ahoj"), Character.valueOf('A'));
2.56 + }
2.57 + @Test public void stringToBoolean() {
2.58 + assertEquals(JSON.boolValue("false"), Boolean.FALSE);
2.59 + assertEquals(JSON.boolValue("True"), Boolean.TRUE);
2.60 + }
2.61 + @Test public void numberToBoolean() {
2.62 + assertEquals(JSON.boolValue(0), Boolean.FALSE);
2.63 + assertEquals(JSON.boolValue(1), Boolean.TRUE);
2.64 + }
2.65 +}