examples for jannson

1,816 views
Skip to first unread message

Rifo

unread,
Feb 27, 2015, 7:22:42 AM2/27/15
to jansso...@googlegroups.com
Hello all,

I am new to Jansson and json and would like to generate some json texts. I have read the tutorial and the API but I think that if I can get my hands on some further examples, things would be easier for me. When searching in github, I am usually lost among forks of jannson repository. I would be very happy if you can provide some links for jannson examples.

An example Json that I would like to build is below.

Thank you very much for your help
rifo

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            27.6071834564209,
            38.58359966761715
          ],
          [
            27.608342170715332,
            38.584807345233095
          ],
          [
            27.608299255371094,
            38.58953722034559
          ],
          [
            27.604308128356934,
            38.591650468096404
          ],
          [
            27.613234519958496,
            38.5771583773376
          ]
        ]
      }
    }
  ]
}

Graeme Smecher

unread,
Feb 27, 2015, 3:16:04 PM2/27/15
to jansso...@googlegroups.com
Hi Rifo,
You should be able to do most of this with json_pack(), which is well
documented:

http://jansson.readthedocs.org/en/2.7/apiref.html#building-values

If you'd like some specific help building the JSON structure above, I
suggest you start by posting some code showing what you've tried.

best,
Graeme

Rifo

unread,
Mar 12, 2015, 9:09:50 AM3/12/15
to jansso...@googlegroups.com
Hello Graeme,

Thanks a lot for your help. I was busy with other problems and just found the time to work on this issue. Following your advice and going through other forum posts. I prepared the below code. Can you please have a quick look and comment on it, I am not really sure whether I was able to free the resources correctly?

thank one more time

Kodu buraya girin...#include <stdlib.h>
#include <string.h>

#include <jansson.h>

// What I would like to build
// {
//   "type": "FeatureCollection",
//   "features": [
//     {
//       "type": "Feature",
//       "properties": {},
//       "geometry": {
//         "type": "LineString",
//         "coordinates": [
//           [
//             27.6071834564209,
//             38.58359966761715
//           ],
//           [
//             27.608342170715332,
//             38.584807345233095
//           ],
//           [
//             27.608299255371094,
//             38.58953722034559
//           ],
//           [
//             27.604308128356934,
//             38.591650468096404
//           ],
//           [
//             27.613234519958496,
//             38.5771583773376
//           ]
//         ]
//       }
//     }
//   ]
// }
int main(int argc, char *argv[])
{
   
double coordinates[10] = { 27.6071834564209,  38.58359966761715,
                             
27.608342170715332,38.584807345233095,
                             
27.608299255371094,38.58953722034559,
                             
27.604308128356934,38.591650468096404,
                             
27.613234519958496,38.5771583773376   };
    size_t i
;
   
char *text;

    json_t
*root;
    json_error_t error
;

    json_t
*array2 = json_array();

   
   
for (i = 0; i < 5; i++) {
            json_t
*array  = json_array();
            json_array_append_new
(array, json_real(coordinates[2*i]) );
            json_array_append_new
(array, json_real(coordinates[2*i+1]) );
            json_array_append_new
(array2, array);

           
// TODO: Ask below question?
           
// Is it true that when I later on use json_decref(array2)
           
// all the resources allocated for json_t* array in the for loop
           
// will be freed too?
                           
   
}

    root
= json_pack("{s:s,s:[{s:s,s:{},s:{s:s,s:o}}]}",
                   
"type","FeatureCollection","features", "type","Feature","properties",
                   
"geometry","type","LineString","coordinates",array2);

    text
= json_dumps(root, JSON_INDENT(2));
    puts
(text);
    free
(text);

   
// free resources
    json_decref
(root);
    json_decref
(array2);

   
return(0);
}




27 Şubat 2015 Cuma 22:16:04 UTC+2 tarihinde Graeme Smecher yazdı:

Graeme Smecher

unread,
Mar 12, 2015, 11:40:02 AM3/12/15
to jansso...@googlegroups.com
Hi Rifo,

> Thanks a lot for your help. I was busy with other problems and just
> found the time to work on this issue. Following your advice and going
> through other forum posts. I prepared the below code. Can you please
> have a quick look and comment on it, I am not really sure whether I
> was able to free the resources correctly?

Great! Thanks for posting code; it makes it much easier to help out.

Please see comments inline.

> |
> intmain(intargc,char*argv[])
> {
> doublecoordinates[10]={27.6071834564209,38.58359966761715,
> 27.608342170715332,38.584807345233095,
> 27.608299255371094,38.58953722034559,
> 27.604308128356934,38.591650468096404,
> 27.613234519958496,38.5771583773376};
> size_t i;
> char*text;
>
> json_t *root;
> json_error_t error;
>
> json_t *array2 =json_array();
>
>
> for(i =0;i <5;i++){
> json_t *array =json_array();
> json_array_append_new(array,json_real(coordinates[2*i]));
> json_array_append_new(array,json_real(coordinates[2*i+1]));
> json_array_append_new(array2,array);
> |

|You can replace the above four lines with the following:

json_array_append_new(array2,
json_pack("[ff]",
coordinates[2*i],
coordinates[2*i+1]));

Both are equivalent, but I find this version simpler. (It's up to you,
of course!)

|
> |
> // TODO: Ask below question?
> // Is it true that when I later on use json_decref(array2)
> // all the resources allocated for json_t* array in the for loop
> // will be freed too?
>
> }
>
> root =json_pack("{s:s,s:[{s:s,s:{},s:{s:s,s:o}}]}",
> "type","FeatureCollection","features","type","Feature","properties",
> "geometry","type","LineString","coordinates",array2);
>
> text =json_dumps(root,JSON_INDENT(2));
> puts(text);
> free(text);
>
> // free resources
> json_decref(root);
> json_decref(array2);
> |

There's a double-free bug here. When you pack array2 into root, you
steal a reference to array2. That means "decref(root)" also frees
"array2"|, and it's incorrect to free it again.||

If you want to re-use 'array2' after freeing 'root', use "s:O" in the
json_pack string above. That will obtain a second reference for 'root',
and you will need to decref(array2) as above.

For debugging reference counting, I recommend you try valgrind: it makes
it trivial to double-check that all your references are freed.

best,
Graeme|

Rifat Türsen

unread,
Mar 13, 2015, 9:29:42 AM3/13/15
to jansso...@googlegroups.com
Hello Graeme,

Thank a lot for your feedbacks. I'll be sure to follow them. Still, I believe that it can be really nice to gather example jansson programs in the github repository. This way newcomers like me can save time and more people can make use of this really nice library.

have a nice day:)



best,
Graeme|

--
--
Jansson users mailing list
jansso...@googlegroups.com
http://groups.google.com/group/jansson-users
--- You received this message because you are subscribed to a topic in the Google Groups "Jansson users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jansson-users/wZHfAbo83xw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jansson-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages