Skip to content

Lightweight single-function JSON lexer using C stdio streams

License

Notifications You must be signed in to change notification settings

osvein/json-stdio

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-stdio

Lightweight JSON lexer using C stdio streams

If you are going to use json-stdio as a git submodule, the bare branch has everything you need, without the examples etc.

Features

  • simple
    • ANSI C
    • 2 files (1 source + 1 header)
    • ~120 lines of code
    • single function
    • doesn't require initialization
  • operates on unbuffered and unseekable streams of JSON data
  • uses stdio streams for abstraction of
    • memory (buffers, shared/typed memory objects)
    • standard input
    • files
    • POSIX sockets
  • converts JSON numbers and strings to C data types
    • configurable number precision
    • handles escape sequences
    • works with multibyte characters
  • no dynamic heap allocation
  • non-buffering
  • stateless
  • permissive license (MIT)

SYNOPSIS

#include <stdio.h>
#include "json.h"

struct json_token json_gettoken(FILE *stream);

DESCRIPTION

The json.h header defines the JSON_NUMBER_TYPE and JSON_NUMBER_FORMAT macros if they are undefined.

The json.h header defines the json_number type through typedef as the expansion of the JSON_NUMBER_TYPE macro.

The json.h header defines the json_type enumeration with the following members:

json_type_eof      Indicates an end-of-file condition.
json_type_null     Indicates a JSON null value token.
json_type_true     Indicates a JSON true value token.
json_type_false    Indicates a JSON false value token.
json_type_number   Indicates a JSON number value token.
json_type_string   Indicates a JSON string value token.
json_type_array    Indicates a JSON array value opening token.
json_type_object   Indicates a JSON object value opening token.
json_typemask      Mask for excluding the typeflags.
json_typeflag_pair Indicates that the token is the right-hand side of a JSON
                   object member (and consequently that the previous token was
                   the left-hand side).
json_typeflag_end  Indicates that the token is a closing token (of a string,
                   array or object value).

The json.h header defines the json_token structure with the following members:

enum json_type type     The type of the token
json_number    data.num The value of a JSON number value token
char           data.c   The next character of a JSON string value token

json-stdio requires no explicit initialization. To start using it, simply pass your stdio stream to json_gettoken().

The json_gettoken() function attempts to get the next token in stream.

RETURN VALUE

The json_gettoken() function returns a json_token structure with the type member set to an enum json_type enumeral representing the type of the next token, or json_type_eof if the stream is at end-of-file or a read error occurs. If the token is a JSON number value token, it is converted to a json_number using the scanf format the JSON_NUMBER_FORMAT macro expands to, and stored in the data.num member. If the token is a JSON string value token, the next character of the string is stored in the data.c member. Any JSON character escape sequence is converted.

About

Lightweight single-function JSON lexer using C stdio streams

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published