Git Product home page Git Product logo

jenny's Introduction

jenny is a JSON generator.

jenny is built first and foremost for embedded applications where the heap is limited and you generally want to avoid allocations. malloc() is not used here!

Here's how it works:

First, you define your JSON structure and what variables you want in it. For example, here is test.jenny:

{
    "int": "$this_is_an_int, int$",
    "string": "$this_is_a_string, string$"
}

(By the way, you can use a JSON validator to check that this is valid JSON. jenny does not check!)

Then, you use ./jenny to generate a .c file:

./jenny < test.jenny
{
    { .type = JENNY_CHUNK, .u.chunk = "{\n    \"int\": "},
    { .type = JENNY_VAR, .u.var = { .p = &this_is_an_int, .type = JENNY_INT_TYPE }},
    { .type = JENNY_CHUNK, .u.chunk = ",\n    \"string\": "},
    { .type = JENNY_VAR, .u.var = { .p = &this_is_a_string, .type = JENNY_STRING_TYPE }},
    { .type = JENNY_CHUNK, .u.chunk = "\n}\n"},
    { .type = JENNY_END }
}

Then, you #include the C file and use the jenny API to output the JSON to any write() -ish function that you want. In this case, I'm wrapping write itself to print to STDOUT:

#include <unistd.h>

#include "jenny.h"

int this_is_an_int = 20;
char *this_is_a_string = "\"abc\n\t\\123";
jenny_t test[] =
#include "test.jenny.c"

ssize_t write_wrapper(void *p_fd, const void *p, size_t len) {
    return write(*(int *)p_fd, p, len);
}

int main(void) {
    int fd = STDOUT_FILENO;
    jenny(test, write_wrapper, &fd);

    return 0;
}

jenny's People

Contributors

edmund-huber avatar

Stargazers

Jimmy Pedersen avatar wrmsr avatar

Watchers

 avatar James Cloos avatar

Forkers

sdsxpln

jenny's Issues

don't ever block caller of `jenny()` on a `write`

Right now, I'm using write_all to stuff everything into write (and potentially blocking the callers for however long it takes to do that).

Instead jenny ought to keep a progress structure, and update the structure when the plain write is successful. Then return. The semantics of calling jenny() should be that the caller expects one write call to not block, because the caller has used one of poll, select etc. So then jenny() would return (having partially written its output) and the caller gets to decide if it wants to just repeatedly call jenny() until it reports that it is done, or if it wants to go do other things until the fd/socket/whatever is ready for another nonblocking write.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.