~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.2.1/pjlib-util/src/pjlib-util-test/json_test.c

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: json_test.c 4761 2014-02-24 09:02:44Z nanang $ */
 
2
/*
 
3
 * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
#include "test.h"
 
20
 
 
21
#define THIS_FILE       "json_test.c"
 
22
 
 
23
#if INCLUDE_JSON_TEST
 
24
 
 
25
#include <pjlib-util/json.h>
 
26
#include <pj/log.h>
 
27
#include <pj/string.h>
 
28
 
 
29
static char json_doc1[] =
 
30
"{\
 
31
    \"Object\": {\
 
32
       \"Integer\":  800,\
 
33
       \"Negative\":  -12,\
 
34
       \"Float\": -7.2,\
 
35
       \"String\":  \"A\\tString with tab\",\
 
36
       \"Object2\": {\
 
37
           \"True\": true,\
 
38
           \"False\": false,\
 
39
           \"Null\": null\
 
40
       },\
 
41
       \"Array1\": [116, false, \"string\", {}],\
 
42
       \"Array2\": [\
 
43
            {\
 
44
                   \"Float\": 123.,\
 
45
            },\
 
46
            {\
 
47
                   \"Float\": 123.,\
 
48
            }\
 
49
       ]\
 
50
     },\
 
51
   \"Integer\":  800,\
 
52
   \"Array1\": [116, false, \"string\"]\
 
53
}\
 
54
";
 
55
 
 
56
static int json_verify_1()
 
57
{
 
58
    pj_pool_t *pool;
 
59
    pj_json_elem *elem;
 
60
    char *out_buf;
 
61
    unsigned size;
 
62
    pj_json_err_info err;
 
63
 
 
64
    pool = pj_pool_create(mem, "json", 1000, 1000, NULL);
 
65
 
 
66
    size = (unsigned)strlen(json_doc1);
 
67
    elem = pj_json_parse(pool, json_doc1, &size, &err);
 
68
    if (!elem) {
 
69
        PJ_LOG(1, (THIS_FILE, "  Error: json_verify_1() parse error"));
 
70
        goto on_error;
 
71
    }
 
72
 
 
73
    size = (unsigned)strlen(json_doc1) * 2;
 
74
    out_buf = pj_pool_alloc(pool, size);
 
75
 
 
76
    if (pj_json_write(elem, out_buf, &size)) {
 
77
        PJ_LOG(1, (THIS_FILE, "  Error: json_verify_1() write error"));
 
78
        goto on_error;
 
79
    }
 
80
 
 
81
    PJ_LOG(3,(THIS_FILE, "Json document:\n%s", out_buf));
 
82
    pj_pool_release(pool);
 
83
    return 0;
 
84
 
 
85
on_error:
 
86
    pj_pool_release(pool);
 
87
    return 10;
 
88
}
 
89
 
 
90
 
 
91
int json_test(void)
 
92
{
 
93
    int rc;
 
94
 
 
95
    rc = json_verify_1();
 
96
    if (rc)
 
97
        return rc;
 
98
 
 
99
    return 0;
 
100
}
 
101
 
 
102
 
 
103
 
 
104
#else
 
105
int json_dummy;
 
106
#endif