parson
1.0.0
Parson은 C로 작성된 가벼운 JSON 라이브러리입니다.
달리다:
git clone https://github.com/kgabis/parson.git
Parson.h 및 Parson.c를 소스 코드 트리로 복사하십시오.
make test 하고 테스트를 실행하십시오.
다음은 Github 저장소에서 Basic Commit Info (날짜, SHA 및 저자)를 인쇄하는 함수입니다.
void print_commits_info ( const char * username , const char * repo ) {
JSON_Value * root_value ;
JSON_Array * commits ;
JSON_Object * commit ;
size_t i ;
char curl_command [ 512 ];
char cleanup_command [ 256 ];
char output_filename [] = "commits.json" ;
/* it ain't pretty, but it's not a libcurl tutorial */
sprintf ( curl_command ,
"curl -s "https://api.github.com/repos/%s/%s/commits" > %s" ,
username , repo , output_filename );
sprintf ( cleanup_command , "rm -f %s" , output_filename );
system ( curl_command );
/* parsing json and validating output */
root_value = json_parse_file ( output_filename );
if ( json_value_get_type ( root_value ) != JSONArray ) {
system ( cleanup_command );
return ;
}
/* getting array from root value and printing commit info */
commits = json_value_get_array ( root_value );
printf ( "%-10.10s %-10.10s %sn" , "Date" , "SHA" , "Author" );
for ( i = 0 ; i < json_array_get_count ( commits ); i ++ ) {
commit = json_array_get_object ( commits , i );
printf ( "%.10s %.10s %sn" ,
json_object_dotget_string ( commit , "commit.author.date" ),
json_object_get_string ( commit , "sha" ),
json_object_dotget_string ( commit , "commit.author.name" ));
}
/* cleanup code */
json_value_free ( root_value );
system ( cleanup_command );
} print_commits_info("torvalds", "linux"); 인쇄물:
Date SHA Author
2012-10-15 dd8e8c4a2c David Rientjes
2012-10-15 3ce9e53e78 Michal Marek
2012-10-14 29bb4cc5e0 Randy Dunlap
2012-10-15 325adeb55e Ralf Baechle
2012-10-14 68687c842c Russell King
2012-10-14 ddffeb8c4d Linus Torvalds
...
이 예에서는 Parson을 사용하여 사용자 정보를 파일에 저장 한 다음로드하고 나중에 검증합니다.
void persistence_example ( void ) {
JSON_Value * schema = json_parse_string ( "{"name":""}" );
JSON_Value * user_data = json_parse_file ( "user_data.json" );
char buf [ 256 ];
const char * name = NULL ;
if ( user_data == NULL || json_validate ( schema , user_data ) != JSONSuccess ) {
puts ( "Enter your name:" );
scanf ( "%s" , buf );
user_data = json_value_init_object ();
json_object_set_string ( json_object ( user_data ), "name" , buf );
json_serialize_to_file ( user_data , "user_data.json" );
}
name = json_object_get_string ( json_object ( user_data ), "name" );
printf ( "Hello, %s." , name );
json_value_free ( schema );
json_value_free ( user_data );
return ;
}DOT 표기법 덕분에 JSON 값을 만드는 것은 매우 간단합니다. 객체 계층 구조는 특정 필드를 해결할 때 자동으로 생성됩니다. 다음 예에서는 사람에 대한 기본 정보가 포함 된 간단한 JSON 값을 만듭니다.
void serialization_example ( void ) {
JSON_Value * root_value = json_value_init_object ();
JSON_Object * root_object = json_value_get_object ( root_value );
char * serialized_string = NULL ;
json_object_set_string ( root_object , "name" , "John Smith" );
json_object_set_number ( root_object , "age" , 25 );
json_object_dotset_string ( root_object , "address.city" , "Cupertino" );
json_object_dotset_value ( root_object , "contact.emails" , json_parse_string ( "["[email protected]","[email protected]"]" ));
serialized_string = json_serialize_to_string_pretty ( root_value );
puts ( serialized_string );
json_free_serialized_string ( serialized_string );
json_value_free ( root_value );
}산출:
{
"name": "John Smith",
"age": 25,
"address": {
"city": "Cupertino"
},
"contact": {
"emails": [
"[email protected]",
"[email protected]"
]
}
}
나는 항상 작동하는 버그 수정을 병합 할 것입니다. 그러나 API에 새로운 것을 추가하려면 GitHub에서 "문제"를 먼저 만들어서 구현을 시작하기 전에 라이브러리에서 끝나야하는지 논의 할 수 있습니다. Parson의 코드 스타일을 따르고 적절한 테스트를 작성하십시오.
MIT 라이센스 (MIT)