Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
尘离序散
obs-studio
提交
3cbc711f
O
obs-studio
项目概览
尘离序散
/
obs-studio
与 Fork 源项目一致
从无法访问的项目Fork
通知
30
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
O
obs-studio
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3cbc711f
编写于
4月 24, 2014
作者:
J
jp9000
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add obs_data JSON loading/saving functions
上级
21db9465
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
175 addition
and
5 deletion
+175
-5
libobs/obs-data.c
libobs/obs-data.c
+175
-5
未找到文件。
libobs/obs-data.c
浏览文件 @
3cbc711f
...
...
@@ -20,6 +20,8 @@
#include "util/darray.h"
#include "obs-data.h"
#include <jansson.h>
struct
obs_data_item
{
volatile
long
ref
;
struct
obs_data
*
parent
;
...
...
@@ -235,6 +237,154 @@ static inline void obs_data_item_setdata(
/* ------------------------------------------------------------------------- */
static
void
obs_data_add_json_item
(
obs_data_t
data
,
const
char
*
key
,
json_t
*
json
);
static
inline
void
obs_data_add_json_object_data
(
obs_data_t
data
,
json_t
*
jobj
)
{
const
char
*
item_key
;
json_t
*
jitem
;
json_object_foreach
(
jobj
,
item_key
,
jitem
)
{
obs_data_add_json_item
(
data
,
item_key
,
jitem
);
}
}
static
inline
void
obs_data_add_json_object
(
obs_data_t
data
,
const
char
*
key
,
json_t
*
jobj
)
{
obs_data_t
sub_obj
=
obs_data_create
();
obs_data_add_json_object_data
(
sub_obj
,
jobj
);
obs_data_setobj
(
data
,
key
,
sub_obj
);
obs_data_release
(
sub_obj
);
}
static
void
obs_data_add_json_array
(
obs_data_t
data
,
const
char
*
key
,
json_t
*
jarray
)
{
obs_data_array_t
array
=
obs_data_array_create
();
size_t
idx
;
json_t
*
jitem
;
json_array_foreach
(
jarray
,
idx
,
jitem
)
{
obs_data_t
item
;
if
(
!
json_is_object
(
jitem
))
continue
;
item
=
obs_data_create
();
obs_data_add_json_object_data
(
item
,
jitem
);
obs_data_array_push_back
(
array
,
item
);
obs_data_release
(
item
);
}
obs_data_setarray
(
data
,
key
,
array
);
obs_data_array_release
(
array
);
}
static
void
obs_data_add_json_item
(
obs_data_t
data
,
const
char
*
key
,
json_t
*
json
)
{
if
(
json_is_object
(
json
))
obs_data_add_json_object
(
data
,
key
,
json
);
else
if
(
json_is_array
(
json
))
obs_data_add_json_array
(
data
,
key
,
json
);
else
if
(
json_is_string
(
json
))
obs_data_setstring
(
data
,
key
,
json_string_value
(
json
));
else
if
(
json_is_integer
(
json
))
obs_data_setint
(
data
,
key
,
json_integer_value
(
json
));
else
if
(
json_is_real
(
json
))
obs_data_setdouble
(
data
,
key
,
json_real_value
(
json
));
else
if
(
json_is_true
(
json
))
obs_data_setbool
(
data
,
key
,
true
);
else
if
(
json_is_false
(
json
))
obs_data_setbool
(
data
,
key
,
false
);
}
/* ------------------------------------------------------------------------- */
static
inline
void
set_json_string
(
json_t
*
json
,
const
char
*
name
,
obs_data_item_t
item
)
{
const
char
*
val
=
obs_data_item_getstring
(
item
);
json_object_set_new
(
json
,
name
,
json_string
(
val
));
}
static
inline
void
set_json_number
(
json_t
*
json
,
const
char
*
name
,
obs_data_item_t
item
)
{
enum
obs_data_number_type
type
=
obs_data_item_numtype
(
item
);
if
(
type
==
OBS_DATA_NUM_INT
)
{
long
long
val
=
obs_data_item_getint
(
item
);
json_object_set_new
(
json
,
name
,
json_integer
(
val
));
}
else
{
double
val
=
obs_data_item_getdouble
(
item
);
json_object_set_new
(
json
,
name
,
json_real
(
val
));
}
}
static
inline
void
set_json_bool
(
json_t
*
json
,
const
char
*
name
,
obs_data_item_t
item
)
{
bool
val
=
obs_data_item_getbool
(
item
);
json_object_set_new
(
json
,
name
,
val
?
json_true
()
:
json_false
());
}
static
json_t
*
obs_data_to_json
(
obs_data_t
data
);
static
inline
void
set_json_obj
(
json_t
*
json
,
const
char
*
name
,
obs_data_item_t
item
)
{
obs_data_t
obj
=
obs_data_item_getobj
(
item
);
json_object_set_new
(
json
,
name
,
obs_data_to_json
(
obj
));
obs_data_release
(
obj
);
}
static
inline
void
set_json_array
(
json_t
*
json
,
const
char
*
name
,
obs_data_item_t
item
)
{
json_t
*
jarray
=
json_array
();
obs_data_array_t
array
=
obs_data_item_getarray
(
item
);
size_t
count
=
obs_data_array_count
(
array
);
for
(
size_t
idx
=
0
;
idx
<
count
;
idx
++
)
{
obs_data_t
sub_item
=
obs_data_array_item
(
array
,
idx
);
json_t
*
jitem
=
obs_data_to_json
(
sub_item
);
json_array_append_new
(
jarray
,
jitem
);
}
json_object_set_new
(
json
,
name
,
jarray
);
obs_data_array_release
(
array
);
}
static
json_t
*
obs_data_to_json
(
obs_data_t
data
)
{
json_t
*
json
=
json_object
();
obs_data_item_t
item
=
obs_data_first
(
data
);
while
(
item
)
{
enum
obs_data_type
type
=
obs_data_item_gettype
(
item
);
const
char
*
name
=
get_item_name
(
item
);
if
(
type
==
OBS_DATA_STRING
)
set_json_string
(
json
,
name
,
item
);
else
if
(
type
==
OBS_DATA_NUMBER
)
set_json_number
(
json
,
name
,
item
);
else
if
(
type
==
OBS_DATA_BOOLEAN
)
set_json_bool
(
json
,
name
,
item
);
else
if
(
type
==
OBS_DATA_OBJECT
)
set_json_obj
(
json
,
name
,
item
);
obs_data_item_next
(
&
item
);
}
return
json
;
}
/* ------------------------------------------------------------------------- */
obs_data_t
obs_data_create
()
{
struct
obs_data
*
data
=
bzalloc
(
sizeof
(
struct
obs_data
));
...
...
@@ -245,9 +395,21 @@ obs_data_t obs_data_create()
obs_data_t
obs_data_create_from_json
(
const
char
*
json_string
)
{
/* TODO */
UNUSED_PARAMETER
(
json_string
);
return
NULL
;
obs_data_t
data
=
obs_data_create
();
json_error_t
error
;
json_t
*
root
=
json_loads
(
json_string
,
JSON_REJECT_DUPLICATES
,
&
error
);
if
(
root
)
{
obs_data_add_json_object_data
(
data
,
root
);
json_decref
(
root
);
}
else
{
blog
(
LOG_ERROR
,
"obs-data.c: [obs_data_create_from_json] "
"Failed reading json string (%d): %s"
,
error
.
line
,
error
.
text
);
}
return
data
;
}
void
obs_data_addref
(
obs_data_t
data
)
...
...
@@ -266,7 +428,8 @@ static inline void obs_data_destroy(struct obs_data *data)
item
=
next
;
}
bfree
(
data
->
json
);
/* NOTE: don't use bfree for json text, allocated by json */
free
(
data
->
json
);
bfree
(
data
);
}
...
...
@@ -282,7 +445,14 @@ const char *obs_data_getjson(obs_data_t data)
{
if
(
!
data
)
return
NULL
;
/* TODO */
/* NOTE: don't use libobs bfree for json text */
free
(
data
->
json
);
data
->
json
=
NULL
;
json_t
*
root
=
obs_data_to_json
(
data
);
data
->
json
=
json_dumps
(
root
,
JSON_PRESERVE_ORDER
|
JSON_INDENT
(
4
));
json_decref
(
root
);
return
data
->
json
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录