create JSON data with escaped characters don't work with JANSSON

951 views
Skip to first unread message

Dieter Reuter

unread,
Jan 20, 2012, 1:00:33 PM1/20/12
to Jansson users
Hi,

I'm looking for a C lib to create JSON data, that I can use for my C
progs on Mac, Linux and Windows systems.
But I'm not able to automatically escape all special characters when
using JANSSON (tested on Mac and Win).

With the following PHP script I tested the encoding of special
characters and German umlaute on Mac OSX.

PHP-source:

<?php
$string1 = "/user/dieter/file_Ä~.dat";
$string2 = 'c:\user\dieter\file_éñ.dat';
echo "string1=".$string1."\n";
echo "string2=".$string2."\n";

$strings = array();
$strings[] = array('string1'=>$string1, 'string2'=>$string2);
echo json_encode($strings)."\n";
?>

Output:

string1=/user/dieter/file_Ä~.dat
string2=c:\user\dieter\file_éñ.dat
[{"string1":"\/user\/dieter\/file_\u00c4~.dat","string2":"c:\\user\
\dieter\\file_\u00e9\u00f1.dat"}]

Results:
1) with PHP, the encoded output is OK
2) if I use JANSSON, the special characters will not be escaped

All ideas to resolve this issue?

Dieter

Deron Meranda

unread,
Jan 20, 2012, 1:32:22 PM1/20/12
to jansso...@googlegroups.com
> I'm looking for a C lib to create JSON data, that I can use for my C
> progs on Mac, Linux and Windows systems.
> But I'm not able to automatically escape all special characters when
> using JANSSON (tested on Mac and Win).
...

> 1) with PHP, the encoded output is OK
> 2) if I use JANSSON, the special characters will not be escaped


If by "special characters" you mean anything that's not an ASCII
"letter", then you may want to use the JSON_ENSURE_ASCII flag during
your encoding. By default Jansson will assume the generated JSON is to
be in UTF-8 encoding (e.g., Unicode), which means that there is no
reason to escape "special" characters.

See http://www.digip.org/jansson/doc/2.2/apiref.html#encoding
--
Deron Meranda
http://deron.meranda.us/

Dieter Reuter

unread,
Jan 20, 2012, 2:46:12 PM1/20/12
to Jansson users

> If by "special characters" you mean anything that's not an ASCII
> "letter", then you may want to use the JSON_ENSURE_ASCII flag during
> your encoding. By default Jansson will assume the generated JSON is to
> be in UTF-8 encoding (e.g., Unicode), which means that there is no
> reason to escape "special" characters.
>
> Seehttp://www.digip.org/jansson/doc/2.2/apiref.html#encoding
> --
> Deron Merandahttp://deron.meranda.us/

Thanks for the clarification.

I just read the JSON spec carefully, and tested it with JANSSON. It's
only necessary to escape " and \ and the control characters \b \f \n
\r \t. All that works well with JANSSON.
I was just confused about the many espaces found in an PHP encoding.
Now I'm sure that's nonsense, and JANSSON will fulfill my
requirements.

Dieter

Deron Meranda

unread,
Jan 20, 2012, 2:58:43 PM1/20/12
to jansso...@googlegroups.com
> I just read the JSON spec carefully, and tested it with JANSSON. It's
> only necessary to escape " and \ and the control characters \b \f \n
> \r \t. All that works well with JANSSON.

Glad you got it working. Yes, many JSON implementations in different
languages do different things. Some of them, like Jansson, embrace
Unicode and the UTF-8 encoding so only escape where absolutely
necessary; while other implementations try to keep everything as
ASCII-like as possible.

Be assured that Jansson has been rigorously tested against the JSON
spec. You can read the chapter in the docs about it's compliance and
any quirks (mostly imposed by the limits of the C language).

With regards to characters, really the only thing it can't handle
correctly is the zero-byte (technically Unicode character U+0000).
Also, all non-printable control characters (i.e., 0x00 through 0x1F),
not just \b \f \n \r \t, must be escaped.

Dieter Reuter

unread,
Jan 20, 2012, 3:22:59 PM1/20/12
to Jansson users
Deron,

as a JSON beginner it's not easy to understand all details, but now I
think I got it. Producing UTF-8 with only the needed escapes will lead
to small and perfect formatted JSON data, complete and easy to read.

And it's good to know, to find such detailed help about Jansson here.

Dieter

Deron Meranda

unread,
Jan 20, 2012, 3:41:56 PM1/20/12
to jansso...@googlegroups.com
> as a JSON beginner it's not easy to understand all details, ...

The syntax diagrams on http://json.org/ are super simple and easy for
beginners to get a feel for JSON. But if you want all the nitty-gritty
details, then you have to go read the full spec, RFC 4627 at
http://tools.ietf.org/html/rfc4627.html

The Jansson conformance details are at
http://www.digip.org/jansson/doc/2.2/conformance.html


Of course, you still may want to force Jansson to output escapes using
the JSON_ENSURE_ASCII flag ... it really depends on what you're using
and if your other apps are prepared to deal with UTF-8, or just ASCII
(e.g., if you're hand-editing in a text editor, etc.) Jansson gives
you flexibility there.

Reply all
Reply to author
Forward
0 new messages