Release notes for 1.9.1
[blerg.git] / common / json.c
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <yajl/yajl_gen.h>
8 #include "database.h"
9 #include "json.h"
10
11 void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record, int brief) {
12         char *data;
13         char number[21];
14         int len;
15
16         if (!blerg_fetch(b, record, &data, &len)) {
17                 fprintf(stderr, "Could not fetch record\n");
18                 return;
19         }
20
21         if (brief && len > 150) {
22                 len = 150;
23                 /* This is a bit of a hackish way to add an ellipsis, and will
24                  * probably cause weird results if we overwrite part of a UTF-8
25                  * char */
26                 data[149] = '.';
27                 data[148] = '.';
28                 data[147] = '.';
29         }
30
31         yajl_gen_map_open(g);
32         if (author != NULL) {
33                 yajl_gen_string(g, (unsigned char *)"author", 6);
34                 yajl_gen_string(g, (unsigned char *)author, strlen(author));
35         }
36         yajl_gen_string(g, (unsigned char *)"record", 6);
37         snprintf(number, 21, "%llu", record);
38         yajl_gen_string(g, (unsigned char *)number, strlen(number));
39         yajl_gen_string(g, (unsigned char *)"timestamp", 9);
40         yajl_gen_integer(g, blerg_get_timestamp(b, record));
41         yajl_gen_string(g, (unsigned char *)"data", 4);
42         yajl_gen_string(g, (unsigned char *)data, len);
43         yajl_gen_map_close(g);
44
45         free(data);
46 }
47