Parsing Integer Arrays using Jansson

1,364 views
Skip to first unread message

Juho Vepsäläinen

unread,
Sep 28, 2012, 6:04:53 AM9/28/12
to jansso...@googlegroups.com
Hi,

I know I must be missing something really simple here. What is the correct way to parse arrays using Jansson?

My data structure looks like this: {"distance1": [0, 1, 2, ...], ...}. Each array may have an arbitrary amount of those integers. If needed I can pass the array length to my JSON.

Currently I'm extracting the data like this:

int distance1[10000];
int distance2[10000];
int ambient_light[10000];
int sound_pressure_level[10000];

json_unpack(root, "{s:[i], s:[i], s:[i], s:[i]}",
	"distance1", distance1,
	"distance2", distance2,
	"ambient_light", ambient_light,
	"sound_pressure", sound_pressure_level
);

It appears this parses only the first items of those lists. I tried various alternative matchers (ie. [i!] and [i*]) but it really didn't work out. You can see the full source at https://github.com/hacklab-jkl/elovalo/blob/master/src/exporter/exporter.c in case you are interested.

I guess I must be using the API in some really wrong way. Insight is appreciated! :)

Sincerely,
Juho Vepsäläinen
nixtu.blogspot.com

Petri Lehtinen

unread,
Sep 28, 2012, 6:43:44 AM9/28/12
to jansso...@googlegroups.com
Juho Vepsäläinen wrote:
> Hi,

Hello.

> json_unpack(root, "{s:[i], s:[i], s:[i], s:[i]}",
> "distance1", distance1,
> "distance2", distance2,
> "ambient_light", ambient_light,
> "sound_pressure", sound_pressure_level
> );

When using [i], only the first item is indeed extracted. What you
should be doing is to extract the array as-is using the 'o' format
specifier.

json_t *distance1, *distance2, *ambient_light; *sound_pressure_level;

json_unpack(root, "{s:o, s:o, s:o, s:o}",
"distance1", distance1,
"distance2", distance2,
"ambient_light", ambient_light,
"sound_pressure", sound_pressure_level);

Then you can use the json_array_*() functions to access the items of
the array. For example, to store the values to a normal C array, you
would do this:

size_t i;
int *distance1_values = malloc(json_array_size(distance1) * sizeof(int));

for(i = 0; i < json_array_length(distance1); i++)
distance1_values[i] = json_integer_value(json_array_get(dinstance1, i));

Hope this helps :)

Petri

Petri Lehtinen

unread,
Sep 28, 2012, 6:44:51 AM9/28/12
to jansso...@googlegroups.com
Petri Lehtinen wrote:
> for(i = 0; i < json_array_length(distance1); i++)

And this would of course be json_array_size(distance1), too...

Juho Vepsäläinen

unread,
Oct 15, 2012, 3:10:06 AM10/15/12
to jansso...@googlegroups.com
Hi,

Thanks for help. json_unpack works as long as I do something like this:

json_t *distance1;

json_unpack(root, "{s:o}", 
    "distance1", distance1, 
);

As soon as I add another property there (ie. {s:o, s:o} and "distance2") things start to fall apart. I keep getting EXC_BAD_ACCESS. Here's a trace:

(gdb) run sine 1000 tmp/sensors.json
Starting program: /Users/juhovepsalainen/Projects/Koodilehto/lightpylon/elovalo/build/exporter/exporter sine 1000 tmp/sensors.json

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x000000000000000e
0x000000010000e191 in unpack ()
(gdb) bt
#0  0x000000010000e191 in unpack ()
#1  0x000000010000dba7 in unpack ()
#2  0x000000010000e379 in json_vunpack_ex ()
#3  0x000000010000e4a5 in json_unpack ()
#4  0x0000000100003fa1 in export_effect ()
#5  0x0000000100003e81 in main ()

Any idea what might be causing this?

I am using version 2.3 of Jansson btw (MacPorts).

--
Juho

Petri Lehtinen

unread,
Oct 15, 2012, 3:13:08 AM10/15/12
to jansso...@googlegroups.com
Juho Vepsäläinen wrote:
> Hi,
>
> Thanks for help. json_unpack works as long as I do something like this:
>
> json_t *distance1;
>
> json_unpack(root, "{s:o}",
> "distance1", distance1,
> );

You need to use &distance1, i.e. pass a pointer to the variable,
because json_unpack() sets the variable to point to the correct
structure.

This might also fix the crash you're seeing.

Petri
Reply all
Reply to author
Forward
0 new messages