Git Product home page Git Product logo

libjson's Introduction

libjson - simple and efficient json parser and printer in C

Introduction

libjson is a simple library without any dependancies to parse and pretty print the JSON format (RFC 4627). The JSON format is a concise and structured data format.

Features

  • interruptible parser: append data to the state how you want it.
  • No object model integrated
  • Small codebase: handcoded parser and efficient factorisation make the code smalls and perfect for embedding.
  • Fast: use efficient code and small parsing tables for maximum efficiency.
  • Full JSON support.
  • UTF8 validation of the input.
  • No number conversion: user convert data the way they want.
  • Secure: optional limits on nesting level, and on data size.
  • Optional comments: in YAML/python style and C style.
  • Optional user defined allocation functions.

libjson parser is an interruptible handcoded state parse. the parser takes character or string as input. Since it's interruptible, it's up to the user to feed the stream to the parser, which permits complete flexibility as to whether the data is coming from a pipe, a network socket, a file on disk, a serial line, or crafted by the user.

The parser doesn't create an object tree for you, but each time it comes up with an element in this data, it just callback to the user with the type found and for some type, the data associated with it. It can be compared to the SAX way of XML, hence it's called SAJ (Simple API for JSon).

The parser doesn't convert number to any native C format, but instead callback with a string that is a valid JSon number. JSon number can be of any size, so that's up to user to decide whetever or not, the number can map to native C type int32_t, int64_t, or a complex integer type. As well the user has a choice to refuse the integer at the callback stage if the length is not appropriate.

The parser optionally allows YAML and/or C comments to be ignored if the config structure is set accordingly, otherwise a JSON_ERROR_COMMENT_NOT_ALLOWED is returned.

Embedding & Build system

The primary use case of this pieces of code is providing JSON capability to your program or library, without adding an extra build dependency. You can add it to your project directly, and integrate it without any fuss.

The "build system" available with the library is just a way to test that everything in the library conforms to specifications and features. It's not necessarily intended as a way to build portable dynamic library (.so or .dll). It should works in simple case of building on Linux and BSDs though.

For others use (eg. windows, OS X, obscure unixes), it is much simpler to integrate the library in your program or library directly.

Simple build fixes to build on more platforms will be accepted though.

Contributing

Open a pull request with your new feature, simple code fix, or documentation fixes. Please conform to coding style, and to the spirit of the library: policy is not imposed by the library.

The Parser API

The parser API is really simple, totaling only 5 API calls:

  • json_parser_init
  • json_parser_char
  • json_parser_string
  • json_parser_is_done
  • json_parser_free

json_parser_init initializes a new parser context from a parser config and takes a callback + userdata. This callback function is used everything the parser need to communicate a type and value to the client side of the library.

json_parser_char take one character and inject it in the parser. on parsing success it will return a 0 value, but on parsing error it returns a parsing error that represents the type of the error encounters. see JSON_ERROR_* for the full set of return values.

json_parser_string is similar to json_parser_char except that it takes a string and a length. it also returns the number of character processed, which is useful when an parser error happened in the stream to pinpoint where.

json_parser_is_done permits to test whetever or not the parser is in a terminated state. it involves not beeing into any structure.

json_parser_free is the opposite of init, it just free the allocated structure.

The Printer API

the printer API is simple too:

  • json_printer_init
  • json_printer_free
  • json_printer_pretty
  • json_printer_raw

json_printer_init initialise a printing context and takes a callback + userdata that will be called for every character that the printer wants to output. the caller can have the printer callback redirect to anything it wants.

json_printer_free is the opposite of init

json_printer_raw takes a json type and an optional data and length value depending on the type. it's up to the caller to verify that the order of type are JSON-compliant, otherwise the generated document won't be able to be parsed again.

json_printer_pretty works like json_printer_raw but is targetted for human reading by appending newlines and spaces

Jsonlint utility program

jsonlint utility provided with the library to verify, or reformat json stream. also useful as example on how to use the library.

libjson's People

Contributors

bos avatar cebtenzzre avatar dat avatar dsvensson avatar iainb avatar maghoff avatar nicolasdp avatar rehsack avatar vincenthz avatar wdauchy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

libjson's Issues

License not defined for this project

Hi, I want to reuse your likely open-source json parser in the my closed-souce program. I wanted to check whether your license allows me to do that but suddenly found out there's no any license at all. Please indicate which license is applicable. If you are not aware baout details of the open-sorce licenses, you probably need to read here more - https://opensource.org/licenses
I recommed new BSD 3-clause license or MIT licsnse - this allows code to be compiled into program statically, on the constast LGPL allows only dynamic linking of the closed-source and open-souce code.
Please indicate your choise of license by addin file with license text to repository. Thank you in advance...

Segmentation fault

Hi there

I fuzzed libjson with American Fuzzy Lop (http://lcamtuf.coredump.cx/afl/ , for no real reason at all, I was just looking for libraries to test AFL). I was running jsonlint with:

jsonlint --tree inputfile

The inputfile with the following content will crash jsonlint (segmentation fault):

[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[][[[[[[[[[[[[[[

Best regards,
floyd

Mac OS support

Hi.

libjjson does not build on Mac OS. Problem is that in mac os gcc there are no soname option.

unknown option (-soname) when compiling on OS X

When compiling (make) on OS X, the option -soname doesn't exist:

ld: unknown option: -soname

Changing -soname to -install_name in the Makefile fixes it, but I'm not sure how best to account for that case without breaking other platforms.

Designated array initializers are not supported in MSVC and causes the build to fail

(Continuing the discussion from issue #3)

which part of the actions_map table MSVC is complaining about ? is it the function pointer or the array initilizer that it thinks is non-constant ?

I'm not entirely sure that I've got the terminology exactly right, but MSVC does not implement what I think might be called designated array initializers:

int a[3] = {
    [1] = 5,
    [0] = 7
};

The [1] =-part throws MSVC completely off. This seems incredible, since this syntax is a part of C99, but MSVC does not claim to implement C99 :(

Currently, they are trying to implement C++0x, and anything from C99 that's included in C++0x will be ported to the C-compiler. Unfortunately this syntax has not been included and is not on the roadmap for MSVC.


I'd be happy to help getting libjson working on MSVC, but I'm not entirely sure what might be the best solution for this exact problem. Any thoughts?

more easy and API for json-dom.(zhaozg fork)

HI,
Small, clever, good performace, event driven, and I like it.
json string to struct is good, so i do some work to enhance c struct to json string.

I fork at https://github.com/zhaozg/libjson, add jsondom.c and jsondom.h code, some hack on json-dom api.
Hope this will make you think about a more easy way for construct json-dom from c api.
Thanks.

Free memory of custom dom in jsonlint.c

I found that the jsonlint sample did not free the memory for the DOM tree. The following is my implementation. This may be needed to show proper use of the API in real application.

static void tree_free(json_val_t* v) {
    switch (v->type) {
    case JSON_OBJECT_BEGIN:
        for (int i = 0; i < v->length; i++) {
            tree_free(v->u.object[i]->val);
            free(v->u.object[i]->key);
            free(v->u.object[i]);
        }
        free(v->u.object);
        break;

    case JSON_ARRAY_BEGIN:
        for (int i = 0; i < v->length; i++)
            tree_free(v->u.array[i]);
        free(v->u.array);
        break;

    default:
        free(v->u.data);
        break;
    }
    free(v);
}

int main(int argc, char **argv)
{
    // ...
        if (use_tree) {
            json_val_t *root_structure;
            ret = do_tree(&config, argv[i], &root_structure);
            if (ret)
                exit(ret);
            if (!verify)
                print_tree(root_structure, output);
            tree_free(root_structure);  // <--
        }
    // ...
} 

-Wsign-compare warnings

Hi,

I get the following warnings when compiling with -Wsign-compare:

json.c: In function ‘json_print_args’:
json.c:965:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (length == -1)
               ^~
jsonlint.c: In function ‘printchannel’:
jsonlint.c:48:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if (ret != length)
          ^~
jsonlint.c: In function ‘process_file’:
jsonlint.c:96:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (i = 0; i < processed; i++) {
                 ^

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.