Niclas Hedhman wrote:

On Mon, May 10, 2010 at 9:01 PM, Georg Ragaller <[email protected]> wrote:

I guess this is the best way not only because of being compatible
with the old format, but with JSON itself.

Since the 'J' stands for JavaScript and JavaScript has only one
number type, which is a 64-bit floating point number.
So to be a good JSON citizen only fp-numbers should be written
as literals.

Interesting, but the JSON package being used will deserialize to a
variety of formats in Java. And that led to mixed collections if the
numbers varied in size.

I'll take a closer look at the spec (if one exists) and perhaps also
look at another implementation...

Cheers

Just to enlight the problem, try to run this and look at the number
literal and the number string in the Java source, the generated JavaScript number literals and at the alerts at runtime.

Cheers, Georg

<snip>
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.math.BigDecimal;

import org.json.JSONException;
import org.json.JSONObject;

public class Main
{
public static void main(String[] args) throws JSONException, FileNotFoundException
    {
        PrintWriter out = new PrintWriter("json.html");
        JSONObject jo = new JSONObject();

        jo.put("aDouble",0.30000000000000003);
        jo.put("aBigDecimal",new BigDecimal("0.30000000000000003"));

        out.println("<html><body><script type=\"text/javascript\">");
        out.println("var jo = ");
        out.println(jo.toString());
        out.println(";");
        out.println("alert(\"aDouble=\" + jo.aDouble);");
        out.println("alert(\"aBigDecimal=\" + jo.aBigDecimal);");
        out.println("</script></body></html>");

        out.close();

    }
}
</snip>


_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to