Skip to content

Commit

Permalink
Merge pull request #277 from PJK/cjson2cbor
Browse files Browse the repository at this point in the history
Add a CBOR to cJSON example
  • Loading branch information
PJK committed Feb 19, 2023
2 parents 6a96c70 + da0ac96 commit a0d55a5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ commands:
steps:
- run: sudo apt-get update
- run: sudo apt-get install -y cmake ${TOOLCHAIN_PACKAGES}
- run: sudo apt install libcmocka-dev
- run: sudo apt install libcmocka-dev libcjson-dev
build:
steps:
- run: >
cmake -DWITH_TESTS=ON \
-DWITH_EXAMPLES=ON \
-DCMAKE_BUILD_TYPE=Debug \
-DSANITIZE=OFF \
-DCOVERAGE="${CMAKE_COVERAGE:='OFF'}" \
Expand Down
4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ if(CJSON_FOUND)
add_executable(cjson2cbor cjson2cbor.c)
target_include_directories(cjson2cbor PUBLIC ${CJSON_INCLUDE_DIRS})
target_link_libraries(cjson2cbor cbor ${CJSON_LIBRARY})

add_executable(cbor2cjson cbor2cjson.c)
target_include_directories(cbor2cjson PUBLIC ${CJSON_INCLUDE_DIRS})
target_link_libraries(cbor2cjson cbor ${CJSON_LIBRARY})
endif()

file(COPY data DESTINATION .)
Expand Down
123 changes: 123 additions & 0 deletions examples/cbor2cjson.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
*
* libcbor is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/

#include <cjson/cJSON.h>
#include <stdio.h>
#include <string.h>

#include "cbor.h"

void usage(void) {
printf("Usage: cbor2cjson [input file]\n");
exit(1);
}

cJSON* cbor_to_cjson(cbor_item_t* item) {
switch (cbor_typeof(item)) {
case CBOR_TYPE_UINT:
return cJSON_CreateNumber(cbor_get_int(item));
case CBOR_TYPE_NEGINT:
return cJSON_CreateNumber(-1 - cbor_get_int(item));
case CBOR_TYPE_BYTESTRING:
// cJSON only handles null-terminated string -- binary data would have to
// be escaped
return cJSON_CreateString("Unsupported CBOR item: Bytestring");
case CBOR_TYPE_STRING:
if (cbor_string_is_definite(item)) {
// cJSON only handles null-terminated string
char* null_terminated_string = malloc(cbor_string_length(item) + 1);
memcpy(null_terminated_string, cbor_string_handle(item),
cbor_string_length(item));
null_terminated_string[cbor_string_length(item)] = 0;
cJSON* result = cJSON_CreateString(null_terminated_string);
free(null_terminated_string);
return result;
}
return cJSON_CreateString("Unsupported CBOR item: Chunked string");
case CBOR_TYPE_ARRAY: {
cJSON* result = cJSON_CreateArray();
for (size_t i = 0; i < cbor_array_size(item); i++) {
cJSON_AddItemToArray(result, cbor_to_cjson(cbor_array_get(item, i)));
}
return result;
}
case CBOR_TYPE_MAP: {
cJSON* result = cJSON_CreateObject();
for (size_t i = 0; i < cbor_map_size(item); i++) {
char* key = malloc(128);
snprintf(key, 128, "Surrogate key %zu", i);
// JSON only support string keys
if (cbor_isa_string(cbor_map_handle(item)[i].key) &&
cbor_string_is_definite(cbor_map_handle(item)[i].key)) {
size_t key_length = cbor_string_length(cbor_map_handle(item)[i].key);
if (key_length > 127) key_length = 127;
// Null-terminated madness
memcpy(key, cbor_string_handle(cbor_map_handle(item)[i].key),
key_length);
key[key_length] = 0;
}

cJSON_AddItemToObject(result, key,
cbor_to_cjson(cbor_map_handle(item)[i].value));
free(key);
}
return result;
}
case CBOR_TYPE_TAG:
return cJSON_CreateString("Unsupported CBOR item: Tag");
case CBOR_TYPE_FLOAT_CTRL:
if (cbor_float_ctrl_is_ctrl(item)) {
if (cbor_is_bool(item)) return cJSON_CreateBool(cbor_get_bool(item));
if (cbor_is_null(item)) return cJSON_CreateNull();
return cJSON_CreateString("Unsupported CBOR item: Control value");
}
return cJSON_CreateNumber(cbor_float_get_float(item));
}

return cJSON_CreateNull();
}

/*
* Reads CBOR data from a file and outputs JSON using cJSON
* $ ./examples/cbor2cjson examples/data/nested_array.cbor
*/

int main(int argc, char* argv[]) {
if (argc != 2) usage();
FILE* f = fopen(argv[1], "rb");
if (f == NULL) usage();
fseek(f, 0, SEEK_END);
size_t length = (size_t)ftell(f);
fseek(f, 0, SEEK_SET);
unsigned char* buffer = malloc(length);
fread(buffer, length, 1, f);

/* Assuming `buffer` contains `length` bytes of input data */
struct cbor_load_result result;
cbor_item_t* item = cbor_load(buffer, length, &result);
free(buffer);

if (result.error.code != CBOR_ERR_NONE) {
printf(
"There was an error while reading the input near byte %zu (read %zu "
"bytes in total): ",
result.error.position, result.read);
exit(1);
}

cJSON* cjson_item = cbor_to_cjson(item);
char* json_string = cJSON_Print(cjson_item);
printf("%s\n", json_string);
free(json_string);
fflush(stdout);

/* Deallocate the result */
cbor_decref(&item);
cJSON_Delete(cjson_item);

fclose(f);
}
2 changes: 1 addition & 1 deletion examples/cjson2cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void cjson_cbor_stream_decode(cJSON *source,
}

void usage(void) {
printf("Usage: cjson [input JSON file]\n");
printf("Usage: cjson2cbor [input JSON file]\n");
exit(1);
}

Expand Down
Binary file added examples/data/all_types.cbor
Binary file not shown.
2 changes: 1 addition & 1 deletion src/cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
case CBOR_TYPE_NEGINT: {
fprintf(out, "%*s[CBOR_TYPE_NEGINT] ", indent, " ");
fprintf(out, "Width: %dB, ", _pow(2, cbor_int_get_width(item)));
fprintf(out, "Value: -%" PRIu64 " -1\n", cbor_get_int(item));
fprintf(out, "Value: -%" PRIu64 " - 1\n", cbor_get_int(item));
break;
}
case CBOR_TYPE_BYTESTRING: {
Expand Down

0 comments on commit a0d55a5

Please sign in to comment.