My C (de)serialization library

I've been working on a little project called SOL (Spelis's Object Language). It's a lightweight data serialization library written in C, with a syntax loosely inspired by JSON.

The idea behind SOL is relatively simple: provide a straightforward way to represent, manipulate and serialize structured data without depending on a massive external library.

Why SOL?

I wanted something that was relatively simple to integrate into C projects, while still providing the convenience of working with structured data.

JSON is great, but I wanted to experiment with designing my own format and implementing the entire thing myself.

SOL supports values such as null, booleans, integers, floating-point numbers, strings, characters, arrays and objects.

A different take on JSON

SOL borrows quite a bit from JSON, but its syntax is deliberately more relaxed.

For example, objects and arrays don't require commas between entries, and assignments don't necessarily require an = or :.

This makes the format a little less strict than JSON while retaining a familiar structure, which is nice when you inevitably forget a comma somewhere.

(I've seen people do that, and it's a very valid mistake.)

Written in C

The core library is written in C, with a relatively small API for parsing, creating, manipulating, and freeing values.

The library includes macros only possible for C11, but if you are compiling for a C standard lower than that, those macros get disabled and compatibility goes down to C99.

Beyond C

Although the library is written in C, I've started experimenting with bindings for other languages.

Currently, this includes a C++ wrapper and LuaJIT FFI bindings.

The goal is to make SOL usable from different environments without having to maintain entirely separate implementations.

What's next?

SOL is still mostly a work in progress. There's not much more I want to do with the parser/language itself, but I would enjoy making more bindings, notably for Python, Nim, Java, and maybe JavaScript.

One more thing I would like to add is maybe not making the stringifier/formatter throw away your precious comments, it's not very nice of it to do.

One more thing

I hope you'll enjoy my library if you decide to use it and I should probably show you an example of how it's used, there are some examples in the repo under examples/.
This is the examples/hello_arr.c example with some extra comments added.

C
#include "sol.h"
#include <stdio.h>

int main(void) {
  // ["Hello" ", " "World", '! '\n]
  const char source[] = "[\"Hello\" \", \" \"World\", '! '\\n]";

  // define value root value pointers and parse source
  sol_value *v;
  sol_error *e = NULL;
  bool success = sol_parse(source, &v, e);

  // check if it was successful
  if (!success) {
    sol_print_error(*e);
    sol_free_error(*e);
    return 1;
  }

  // check if our root value is an array
  // in this case it always is.
  if (sol_value_type(v) == SOL_ARRAY) {
    // get the underlying array
    sol_array *arr = sol_get_value(sol_array *, v);

    // we can replace "World" with "SOL"
    sol_array_set(arr, 2, sol_new_value(char *, "SOL"));

    // loop over and print
    for (size_t i = 0; i < sol_array_size(arr); i++) {
      const sol_value *val = sol_array_get(arr, i);

      if (sol_value_type(val) == SOL_STRING) {
        sol_string s = sol_get_value(char *, val);
        printf("%.*s", (int)s.length, s.data);
      } else if (sol_value_type(val) == SOL_CHARACTER) {
        printf("%c", sol_get_value(char, val));
      }
    }
  }

  // completely optional, but check how much memory the object uses.
  printf("mem: %zu bytes\n", sol_value_memory_usage(v));

  // free your value to avoid memory leaks.
  sol_free(v);

  return 0;
}

You can find the source code repository here:

Link to the repository


:/ no more posts >