Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Parsing Integer Arrays using Jansson
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Juho Vepsäläinen  
View profile  
 More options Sep 28 2012, 6:04 am
From: Juho Vepsäläinen <beb...@gmail.com>
Date: Fri, 28 Sep 2012 03:04:53 -0700 (PDT)
Local: Fri, Sep 28 2012 6:04 am
Subject: Parsing Integer Arrays using Jansson

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/expor... 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Petri Lehtinen  
View profile  
 More options Sep 28 2012, 6:43 am
From: Petri Lehtinen <pe...@digip.org>
Date: Fri, 28 Sep 2012 13:43:44 +0300
Local: Fri, Sep 28 2012 6:43 am
Subject: Re: [jansson-users] Parsing Integer Arrays using Jansson

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Petri Lehtinen  
View profile  
 More options Sep 28 2012, 6:44 am
From: Petri Lehtinen <pe...@digip.org>
Date: Fri, 28 Sep 2012 13:44:51 +0300
Local: Fri, Sep 28 2012 6:44 am
Subject: Re: [jansson-users] Parsing Integer Arrays using Jansson

Petri Lehtinen wrote:
>   for(i = 0; i < json_array_length(distance1); i++)

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Juho Vepsäläinen  
View profile  
 More options Oct 15 2012, 3:10 am
From: Juho Vepsäläinen <beb...@gmail.com>
Date: Mon, 15 Oct 2012 00:10:06 -0700 (PDT)
Local: Mon, Oct 15 2012 3:10 am
Subject: Re: [jansson-users] Parsing Integer Arrays using Jansson

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/exporte r/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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Petri Lehtinen  
View profile  
 More options Oct 15 2012, 3:13 am
From: Petri Lehtinen <pe...@digip.org>
Date: Mon, 15 Oct 2012 10:13:08 +0300
Local: Mon, Oct 15 2012 3:13 am
Subject: Re: [jansson-users] Parsing Integer Arrays using Jansson

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »