void test_encode (void) { int rlen; char *r; char p[] = "abcdefghijklmnop"; char q[] = ""; bool eof; char *json = zio_json_encode (p, strlen (p), false); if (!json) BAIL_OUT ("zio_json_encode failed"); ok (json != NULL, "zio_json_encode works"); diag ("%s", json); r = NULL; eof = true; rlen = zio_json_decode (json, (void **)&r, &eof); ok (rlen == strlen (p) && r != NULL && strcmp (r, p) == 0 && eof == false, "zio_json_decode worked"); free (r); eof = true; rlen = zio_json_decode (json, NULL, &eof); ok (rlen == 0 && eof == false, "zio_json_decode worked with NULL data return arg"); free (json); json = zio_json_encode (q, strlen (q), true); if (!json) BAIL_OUT ("zio_json_encode failed"); ok (json != NULL, "zio_json_encode works on zero length data"); diag ("%s", json); r = NULL; eof = false; rlen = zio_json_decode (json, (void **)&r, &eof); ok (rlen == strlen (q) && r != NULL && strcmp (r, q) == 0 && eof == true, "zio_json_decode worked"); free (r); free (json); json = zio_json_encode (NULL, 0, true); if (!json) BAIL_OUT ("zio_json_encode failed"); ok (json != NULL, "zio_json_encode works on NULL data"); diag ("%s", json); rlen = zio_json_decode (json, (void **)&r, NULL); ok (rlen == 0 && r != NULL && strcmp (r, q) == 0, "zio_json_decode returned empty string"); free (r); free (json); }
int kz_close (kz_t *kz) { int rc = -1; char *json_str = NULL; char *key = NULL; if ((kz->flags & KZ_FLAGS_WRITE)) { if (!(kz->flags & KZ_FLAGS_RAW)) { if (asprintf (&key, "%s.%.6d", kz->name, kz->seq++) < 0) oom (); if (!(json_str = zio_json_encode (NULL, 0, true))) { /* EOF */ errno = EPROTO; goto done; } if (kvs_put (kz->h, key, json_str) < 0) goto done; } if (!(kz->flags & KZ_FLAGS_NOCOMMIT_CLOSE)) { if (kvs_commit (kz->h) < 0) goto done; } if (kz->nprocs > 0 && kz->grpname) { if (kz_fence (kz) < 0) goto done; } } rc = 0; done: if (json_str) free (json_str); if (key) free (key); kz_destroy (kz); return rc; }
static char *zio_json_str_create (zio_t *zio, void *data, size_t len) { bool eof = false; if (zio_eof_pending (zio)) { eof = true; zio_debug (zio, "Setting EOF sent\n"); zio->flags |= ZIO_EOF_SENT; } return zio_json_encode (data, len, eof); }
int kz_put (kz_t *kz, char *data, int len) { char *json_str = NULL; int rc = -1; if (len == 0 || data == NULL || (kz->flags & KZ_FLAGS_RAW)) { errno = EINVAL; goto done; } if (!(json_str = zio_json_encode (data, len, false))) { errno = EPROTO; goto done; } if (putnext (kz, json_str) < 0) goto done; rc = len; done: if (json_str) free (json_str); return rc; }