Giter Club home page Giter Club logo

json-builder's Introduction

The serializing counterpart to json-parser.

As with json-parser: BSD licensed, almost ANSI C89 apart from a single use of snprintf.

Usage

Quick example (docs coming soon):

json_value * arr = json_array_new(0);
json_array_push(arr, json_string_new("Hello world!"));
json_array_push(arr, json_integer_new(128));

char * buf = malloc(json_measure(arr));
json_serialize(buf, arr);

printf("%s\n", buf);

[ "Hello world!", 128 ]

json-builder is fully interoperable with json-parser:

char json[] = "[ 1, 2, 3 ]";

json_settings settings = {};
settings.value_extra = json_builder_extra;  /* space for json-builder state */

char error[128];
json_value * arr = json_parse_ex(&settings, json, strlen(json), error);

/* Now serialize it again.
 */
char * buf = malloc(json_measure(arr));
json_serialize(buf, arr);

printf("%s\n", buf);

[ 1, 2, 3 ]

Note that values created by or modified by json-builder must be freed with json_builder_free instead of json_value_free, otherwise the memory of the builder state will be leaked.

Modes

  • json_serialize_mode_multiline — Generate multi-line JSON, for example:
[
  1,
  2,
  3
]
  • json_serialize_mode_single_line — Generate JSON on a single line, for example:
[ 1, 2, 3 ]
  • json_serialize_mode_packed — Generate JSON as tightly packed as possible, for example:
[1,2,3]

Options

  • json_serialize_opt_CRLF — use CR/LF (Windows) line endings

  • json_serialize_opt_pack_brackets — do not leave spaces around brackets (e.g. [ 1, 2 ] becomes [1, 2])

  • json_serialize_opt_no_space_after_comma — do not leave spaces after commas

  • json_serialize_opt_no_space_after_colon — do not leave spaces after colons (inside objects)

  • json_serialize_opt_use_tabs — indent using tabs instead of spaces when in multi-line mode

  • indent_size — the number of tabs or spaces to indent with in multi-line mode

json-builder's People

Contributors

amorph avatar dewitt-garmin avatar marcelmeulemans avatar

Watchers

 avatar

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.