1 | /***************************************************************************\ |
---|
2 | * * |
---|
3 | * BitlBee - An IRC to IM gateway * |
---|
4 | * Helper functions for json.c * |
---|
5 | * * |
---|
6 | * Copyright 2012 Wilmer van der Gaast <wilmer@gaast.net> * |
---|
7 | * * |
---|
8 | * This library is free software; you can redistribute it and/or * |
---|
9 | * modify it under the terms of the GNU Lesser General Public * |
---|
10 | * License as published by the Free Software Foundation, version * |
---|
11 | * 2.1. * |
---|
12 | * * |
---|
13 | * This library is distributed in the hope that it will be useful, * |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
---|
16 | * Lesser General Public License for more details. * |
---|
17 | * * |
---|
18 | * You should have received a copy of the GNU Lesser General Public License * |
---|
19 | * along with this library; if not, write to the Free Software Foundation, * |
---|
20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * |
---|
21 | * * |
---|
22 | ****************************************************************************/ |
---|
23 | |
---|
24 | #include <stdlib.h> |
---|
25 | #include <string.h> |
---|
26 | |
---|
27 | #include "json_util.h" |
---|
28 | |
---|
29 | json_value *json_o_get( const json_value *obj, const json_char *name ) |
---|
30 | { |
---|
31 | int i; |
---|
32 | |
---|
33 | if( !obj || obj->type != json_object ) |
---|
34 | return NULL; |
---|
35 | |
---|
36 | for( i = 0; i < obj->u.object.length; ++ i) |
---|
37 | if( strcmp( obj->u.object.values[i].name, name ) == 0 ) |
---|
38 | return obj->u.object.values[i].value; |
---|
39 | |
---|
40 | return NULL; |
---|
41 | } |
---|
42 | |
---|
43 | const char *json_o_str( const json_value *obj, const json_char *name ) |
---|
44 | { |
---|
45 | json_value *ret = json_o_get( obj, name ); |
---|
46 | |
---|
47 | if( ret && ret->type == json_string ) |
---|
48 | return ret->u.string.ptr; |
---|
49 | else |
---|
50 | return NULL; |
---|
51 | } |
---|
52 | |
---|
53 | char *json_o_strdup( const json_value *obj, const json_char *name ) |
---|
54 | { |
---|
55 | json_value *ret = json_o_get( obj, name ); |
---|
56 | |
---|
57 | if( ret && ret->type == json_string && ret->u.string.ptr ) |
---|
58 | return g_memdup( ret->u.string.ptr, ret->u.string.length + 1 ); |
---|
59 | else |
---|
60 | return NULL; |
---|
61 | } |
---|