当前位置:网站首页>Unified interface for reading and writing data files in xml/json/ini and ubjson formats
Unified interface for reading and writing data files in xml/json/ini and ubjson formats
2022-07-02 17:57:00 【jim. li】
Reading and writing XML/JSON/INI and UBJSON Data files in other formats
Put the abstraction into the code , Details are put into metadata .
Developing applications , Will often use various data files ( Such as configuration data and metadata ), Common data file formats are INI、XML、JSON and UBJSON, For a complex application , Many different formats of data files may be used at the same time .
Usually , The functions that operate on these data files are different , For programmers, it is a burden of learning , It's also a burden on memory .AWTK It provides a set of unified interface functions , The same set of interface functions , You can operate data files in different formats .
1. Basic usage
After loading the data file successfully, you get a object Example , It can be used object To add it 、 modify 、 Delete and query . If you are familiar with object Usage of , That's very simple .
1.1 Reading data
The following function can read any basic type of data .
/** * @method tk_object_get_prop * Gets the value of the specified property . * * @annotation ["scriptable"] * @param {tk_object_t*} obj object object . * @param {const char*} name Name of property . * @param {value_t*} v Returns the value of the property . * * @return {ret_t} return RET_OK It means success , Otherwise, it means failure . */
ret_t tk_object_get_prop(tk_object_t* obj, const char* name, value_t* v);
For ease of use , It also provides wrapper functions for common data types . Such as :
/** * @method tk_object_get_prop_str * Get the value of the string type of the specified property . * * @annotation ["scriptable"] * @param {tk_object_t*} obj object object . * @param {const char*} name Name of property . * * @return {const char*} Returns the value of the string type of the specified property . */
const char* tk_object_get_prop_str(tk_object_t* obj, const char* name);
1.2 increase / Modifying data
The following functions can be added / Modify any basic type of data .
/** * @method tk_object_set_prop * Set the value of the specified property . * * @annotation ["scriptable"] * @param {tk_object_t*} obj object object . * @param {const char*} name Name of property . * @param {value_t*} value The value of the property . * * @return {ret_t} return RET_OK It means success , Otherwise, it means failure . */
ret_t tk_object_set_prop(tk_object_t* obj, const char* name, const value_t* value);
For ease of use , It also provides wrapper functions for common data types . Such as :
/** * @method tk_object_set_prop_str * Set the value of the string type of the specified property . * * @annotation ["scriptable"] * @param {tk_object_t*} obj object object . * @param {const char*} name Name of property . * @param {const char*} value The value of the property . * * @return {ret_t} return RET_OK It means success , Otherwise, it means failure . */
ret_t tk_object_set_prop_str(tk_object_t* obj, const char* name, const char* value);
1.3 Delete data
/** * @method tk_object_remove_prop * Delete the specified attribute . * * @annotation ["scriptable"] * @param {tk_object_t*} obj object object . * @param {const char*} name Name of property . * * @return {ret_t} return RET_OK It means success , Otherwise, it means failure . */
ret_t tk_object_remove_prop(tk_object_t* obj, const char* name);
1.4 path The basic format of
- Configuration information is usually hierarchical , In order to access all data ,path It must also be multi-level , Use “.” Separate .
“.” Cannot be part of the name , If appear , Need to replace with other characters .
{
"network" : {
"eth0" : {
"ip" : "192.169.0.100",
"gateway" : "192.169.0.1",
"mask" : "255.255.255.0"
}
}
}
In this case , To visit ip This configuration of , It can be used "network.eth0.ip":
- Access by index
During the visit , The name of each level , Index can be used instead , The format is :[+ Indexes +], such as [0] Indicates the first item ,[2] Indicates the third item . This is not clear about the name of the case , Or traverse
All configuration , Very helpful .
- use #size Get the number of child nodes
Example :
[wifi]
on = 1
name = awtk
[server]
port = 8080
timeout = 1.000000
In the example above ,"#size" The number of obtained groups is 2,"wifi.#size" obtain wifi The number of sub items under is 2.
- use #name Get the name of the item .
Example :
[wifi]
on = 1
name = awtk
[server]
port = 8080
timeout = 1.000000
In the example above ,“[0].#name” Get the name of the first group as "wifi",“wifi.[0].#name” obtain wifi The name of the next item is “on”.
- use #index Get the ordinal number of the configuration item in the sibling node .
[wifi]
on = 1
name = awtk
[server]
port = 8080
timeout = 1.000000
In the example above ,“wifi.#index” obtain wifi The ordinal of is 0,“server.#index” obtain server The ordinal of is 1.
2. Advanced usage
The above function is easy to use , But when there is a large amount of data , It's better to operate directly node More efficient , If you have this requirement, you can check src/conf_io/conf_node.h Middle function , And the usage in unit testing , No more details here .
3. Open and save
At this time, you need to use functions in specific formats
3.1 JSON Format
- open
/** * @method conf_json_load * From specified URL load JSON object . * @annotation ["constructor"] * * @param {const char*} url route ( Usually the file path ). * @param {bool_t} create_if_not_exist If not, whether to create . * * @return {tk_object_t*} Return the configuration object . */
tk_object_t* conf_json_load(const char* url, bool_t create_if_not_exist);
- preservation
/** * @method conf_json_save_as * take doc Object is saved to the specified URL. * @annotation ["static"] * * @param {tk_object_t*} obj doc object . * @param {const char*} url Saved in . * * @return {ret_t} return RET_OK It means success , Otherwise, it means failure */
ret_t conf_json_save_as(tk_object_t* obj, const char* url);
3.2 XML Format
- open
/** * @method conf_xml_load * From specified URL load XML object . * * @annotation ["constructor"] * * @param {const char*} url route ( Usually the file path ). * @param {bool_t} create_if_not_exist If not, whether to create . * * @return {tk_object_t*} Return the configuration object . */
tk_object_t* conf_xml_load(const char* url, bool_t create_if_not_exist);
- preservation
/** * @method conf_xml_save_as * take doc Object is saved to the specified URL. * @annotation ["static"] * * @param {tk_object_t*} obj doc object . * @param {const char*} url Saved in . * * @return {ret_t} return RET_OK It means success , Otherwise, it means failure */
ret_t conf_xml_save_as(tk_object_t* obj, const char* url);
3.3 INI Format
- open
/** * @method conf_ini_load * From specified URL load INI object . * * @annotation ["constructor"] * * @param {const char*} url route ( Usually the file path ). * @param {bool_t} create_if_not_exist If not, whether to create . * * @return {tk_object_t*} Return the configuration object . */
tk_object_t* conf_ini_load(const char* url, bool_t create_if_not_exist);
- preservation
/** * @method conf_ini_save_as * take doc Object is saved to the specified URL. * @annotation ["static"] * * @param {tk_object_t*} obj doc object . * @param {const char*} url Saved in . * * @return {ret_t} return RET_OK It means success , Otherwise, it means failure */
ret_t conf_ini_save_as(tk_object_t* obj, const char* url);
/** * @method conf_ubjson_load * From specified URL load UBJSON object . * * @annotation ["constructor"] * * @param {const char*} url route ( Usually the file path ). * @param {bool_t} create_if_not_exist If not, whether to create . * * @return {tk_object_t*} Return the configuration object . */
tk_object_t* conf_ubjson_load(const char* url, bool_t create_if_not_exist);
* preservation
```c
/** * @method conf_ubjson_save_as * take doc Object is saved to the specified URL. * @annotation ["static"] * * @param {tk_object_t*} obj doc object . * @param {const char*} url Saved in . * * @return {ret_t} return RET_OK It means success , Otherwise, it means failure */
ret_t conf_ubjson_save_as(tk_object_t* obj, const char* url);
Complete example
#include "conf_io/conf_json.h"
void demo_conf_json(c) {
tk_object_t* doc = conf_json_load("./test.json", TRUE);
tk_object_set_prop_str(doc, "name", "awtk");
tk_object_set_prop_int(doc, "age", 100);
conf_json_save_as(doc, "./test.json");
TK_OBJECT_UNREF(doc);
doc = conf_json_load("./test.json", TRUE);
log_debug("name:%s\n", tk_object_get_prop_str(doc, "name"));
log_debug("age:%d\n", tk_object_get_prop_int(doc, "age", 0));
TK_OBJECT_UNREF(doc);
}
The corresponding data file is :
{
"name" : "awtk",
"age" : 100
}
边栏推荐
- MB10M-ASEMI整流桥MB10M
- PFC232-SOP8/14/16应广一级可带烧录程序编带
- Edgenext hit a mixed punch: a lightweight architecture integrating CNN and transformer
- ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
- Chapter 15 string localization and message Dictionary (1)
- 详解Kubernetes网络模型
- [how is the network connected] Chapter 4 explores access networks and network operators
- Laravel文档阅读笔记-Custom Authentication Login And Registration Using Laravel 8
- MySQL --- 数据库的基本操作
- PMS132B单片机TWS数码管蓝牙充电仓方案开发
猜你喜欢

freemarker+poi实现动态生成excel文件及解析excel文件

Modbus协议通信异常
![[nonlinear control theory]7_ High gain and High Frequency](/img/e5/6c5ca4a89c97d9613cddccb281b35b.png)
[nonlinear control theory]7_ High gain and High Frequency

Troubleshooting ideas that can solve 80% of faults

Rk1126 platform project summary
![[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now](/img/16/574eee53720fd609027950b9bf8154.png)
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now

【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)

wait_for_gap -- 从主库归档备库恢复归档

ORA-19838 -- 恢复控制文件到备库
![[nonlinear control theory]8_ Comparison of three robust controllers](/img/a8/03ed363659a0a067c2f1934457c106.png)
[nonlinear control theory]8_ Comparison of three robust controllers
随机推荐
[comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)
Outsourcing for five years, abandoned
读写 XML/JSON/INI 和 UBJSON 等格式的数据文件的统一接口
辉芒微IO单片机FT60F010A-URT
Longest non repeating subarray
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
Taiwan Feiling fm8pb513b MCU provides MCU program development product design
能解决 80% 故障的排查思路
阿里云子账户 - 权限策略 - 授权给某个账户某个 OSS Bucket 的完全控制权限
Easyswoole3.2 restart failed
Keras深度学习实战——基于VGG19模型实现性别分类
外包干了五年,废了...
Daily question - xiaolele changes the number
win10 kms activator
嵌入式开发板 ~ 说明
应广单片机(MCU单片机科普)
easyAI笔记——深度学习
第十五章 字符串本地化和消息字典(一)
[target tracking] | data set summary