Giter VIP home page Giter VIP logo

jansson's Introduction

Jansson README

tests appveyor

Jansson is a C library for encoding, decoding and manipulating JSON data. Its main features and design principles are:

  • Simple and intuitive API and data model
  • Comprehensive documentation
  • No dependencies on other libraries
  • Full Unicode support (UTF-8)
  • Extensive test suite

Jansson is licensed under the MIT license; see LICENSE in the source distribution for details.

Compilation and Installation

If you obtained a jansson-X.Y.tar.* tarball from GitHub Releases, just use the standard autotools commands:

$ ./configure
$ make
$ make install

To run the test suite, invoke:

$ make check

If the source has been checked out from a Git repository, the configure script has to be generated first. The easiest way is to use autoreconf:

$ autoreconf -i

Documentation

Documentation is available at http://jansson.readthedocs.io/en/latest/.

The documentation source is in the doc/ subdirectory. To generate HTML documentation, invoke:

$ make html

Then, point your browser to doc/_build/html/index.html. Sphinx 1.0 or newer is required to generate the documentation.

Community

jansson's People

Contributors

akheron avatar allenx2018 avatar alteous avatar cmeister2 avatar coreyfarrell avatar cryptobiote avatar dtgriscom avatar edgale avatar firepick1 avatar i-ky avatar isaachier avatar jjchoy avatar joakimsoderberg avatar kiyolee avatar mephistophiles avatar npmccallum avatar pasiopou avatar paulharris avatar phst avatar ploxiln avatar pnacht avatar rdpoor avatar rogerz avatar sannis avatar seanmiddleditch avatar thynix avatar tpgxyz avatar vincentbernat avatar vsoch avatar xry111 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  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

jansson's Issues

order of the items inside json object is not preserved

Given the code snippet below:

json_error_t lerror;
json_t *lobject;

std::string strObj = "{"firstName": "John","phoneNumbers": ["212 732-1234", "646 123-4567"]}";
lobject = json_loads(strObj.c_str(), 0, &lerror);

const char *key;
json_t *value;

void *iter = json_object_iter(lobject);
while(iter)
{
key = json_object_iter_key(iter);
value = json_object_iter_value(iter);
std::cout << "key=" << key << "; value=" << value->type << std::endl;

  iter = json_object_iter_next(lobject, iter);
  json_decref(value);

}
json_decref(lobject);

The result (using Jansson 2.0) is:
key=phoneNumbers; value=1
key=firstName; value=2

The correct result is:
key=firstName; value=2
key=phoneNumbers; value=1

Add json_object_update_{existing,missing}

The _existing variant would only update keys that already exist and not add new keys at all. The _missing variant would only add new keys and leave the already existing keys alone.

Sphinx build failures

With Sphinx v1.02b, the Jansson documentation receives numerous warnings when being compiled. With the sphinx-build -W option in the default build rules, this causes the build to fail. The warning it errors out on:

  • default role cfunc not found

For the Fedora packages I had to temporarily remove the -W option from the makefiles to get the package to build on Fedora 14+. I really have no idea what that error even means (Google tells me nothing useful) or what needs to be tweaked in the Jansson files to fix it.

Write more tutorials

The only tutorial was written for v1.1 and has not been updated since. There are new (and old) features to cover.

There's a starting point for a tutorial using pack/unpack functionality here: http://pastebin.com/YnTMNKv2 (by Jonathan Landis)

string creation functions to accept length

If you'd accept this feature I'll happily provide a patch. Currently I'm making unnecessary string copies. Close 2nd would be for you to take an already-malloc'd pointer and use it, rather than always calling strdup.

Feature request: format character 'a' to return arrays from json_unpack

Currently I'm writing lots of code that looks like this:
if (json_unpack(input, "{so!}", "some_array", &the_array) == -1 ||
!json_is_array(the_array)) {
/* error handling code */
}

I would like to be able to use 'a' in place of the 'o' and not have to check if the returned json_t is an array or not.

[PATCH] avoid set-but-not-used warning/error in a test

I cloned the latest and tried to build and run the tests on Fedora 15,
which has gcc-4.6. Here's the failure I saw:

test_load.c: In function 'main':
test_load.c:14:13: error: variable 'json' set but not used [-Werror=unused-but-set-variable]
cc1: all warnings being treated as errors
make[4]: *** [test_load.o] Error 1
make[4]: *** Waiting for unfinished jobs....

Here's one way to avoid the warning while improving that test at the
same time:
From 7443fb976063c48315ec65e1e1cc8bb7bf72b159 Mon Sep 17 00:00:00 2001
From: Jim Meyering [email protected]
Date: Tue, 5 Apr 2011 14:04:16 +0200
Subject: [PATCH] avoid set-but-not-used warning/error in a test


---
 test/suites/api/test_load.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/test/suites/api/test_load.c b/test/suites/api/test_load.c
index b1679a3..ecb46d8 100644
--- a/test/suites/api/test_load.c
+++ b/test/suites/api/test_load.c
@@ -15,6 +15,8 @@ int main()
     json_error_t error;

     json = json_load_file("/path/to/nonexistent/file.json", 0, &error);
+    if(json)
+        fail("json_load_file returned non-NULL for a nonexistent file");
     if(error.line != -1)
         fail("json_load_file returned an invalid line number");
     if(strcmp(error.text, "unable to open /path/to/nonexistent/file.json: No such file or directory") != 0)
--
1.7.5.rc0.351.gebf36

Allow zero bytes inside strings

This would require at least two new API functions: for creating a string value from buffer and length, and for querying the length of a string value. For example:

json_t *json_string_from_buffer(const char *buffer, size_t length);
json_t *json_string_from_buffer_nocheck(const char *buffer, size_t length);
size_t json_string_length(json_t *json);

The length of strings would have to be stored internally and never use strlen(). The internals would also always have to prepare to handle strings with embedded null bytes.

Feature request: unpack api to detect unhandled JSON object keys

It would be nice for the unpack api to have an ability to check if the JSON object has entries that were not unpacked. E.g. I'm planning to have my config files in JSON format, and unpack seems easiest way to populate it to internal structs; but it would be nice to print a warning for unrecognized entries.

This could be by having a function variant with explicit callback for unrecognized entries; or perhaps have per-each object a "num_parsed" field that could be compared against the hash table size to see if there's unhandled entries (and each hashtable pair would then need a flag if it was parsed or not).

Add CMake building system support

Hi Petri =))

CMake building system going to be more popular. It will be good to add this build system support.

I wrote working example code for this project, which should be place in "src" directory into file CMakeLists.txt

include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set(janson_SOURCES
dump.c
error.c
hashtable.c
hashtable.h
jansson_private.h
load.c
memory.c
pack_unpack.c
strbuffer.c
strbuffer.h
utf.c
utf.h
value.c)

add_library(janson STATIC ${janson_SOURCES})

This code builds jansson as a static libary.

Test fail with a Russian locale

ru_RU.utf8:

make check-TESTS
make[3]: Вход в каталог `/home/nakulov/sources/jansson-2.3/test'
Suite: api
......F......
=================================================================
api/test_load.c
=================================================================
test_load.c:file_not_found:23: json_load_file returned an invalid
error message

=================================================================
Suite: invalid
...............................................
Suite: invalid-unicode
..................
Suite: valid
.................................
1 of 4 test suites failed
FAIL: run-suites
================================
1 of 1 test failed
Please report to [email protected]
================================
make[3]: *** [check-TESTS] Ошибка 1
make[3]: Выход из каталога `/home/nakulov/sources/jansson-2.3/test'
make[2]: *** [check-am] Ошибка 2
make[2]: Выход из каталога `/home/nakulov/sources/jansson-2.3/test'
make[1]: *** [check-recursive] Ошибка 1
make[1]: Выход из каталога `/home/nakulov/sources/jansson-2.3/test'
make: *** [check-recursive] Ошибка 1

Implement json_dump_callback()

It's like the internal do_dump() interface, allowing the user to provide a callback that's called for each (variable length) chunk of output.

Stop after decoding a valid JSON array or object

Add a decoding flag that makes decoder stop after decoding a valid JSON array or object. This would make it possible to e.g. store many JSON values inside a file, or store JSON and other data, and still be able to decode it with Jansson.

Numeric status codes in json_error_t

I would like to be able to take different actions depending on the type of error I get from lson_loads().
Currently I detect incompletely received JSON data by checking error.position == received_len, but I would like to be able to do more than that.
For example it would be nice to be able to distinguish errors caused by the EOF check from errors triggered by JSON_REJECT_DUPLICATES from bad JSON.

Incremental/restartable parser

Implement an incremental/restartable parser that doesn’t require all the input available when the parsing begins, i.e. the user could feed the parser with input data as soon as it is available. This requires some major refactoring to the parser, the lexer and the UTF-8 input layer.

Detect unsupported input encodings

Unsupported encodings (UTF-16, UTF-32) should be detected and a proper error message generated for them. BOMs should also be supported and the UTF-8 BOM ignored.

large numbers create an error "too big integer near ..."

Hey there. If a mega-large integer is encountered in the json data, I get a 'too big integer near ...' error. Can we check to see if the integer is > maxint, and then store it as a string or something instead of failing to parse the json data?

I'm trying to parse the twitter stream using the lib and it's choking on this json string part:
... ,"id_str":"6572981622935552", ...

  • Steve Webb

Parser fails with "real number overflow" on mingw 4.4.0

While parsing correct JSON string, it fails with this error on double values. Debugger shows that double value was successfully returned by strtod() function, but next if (errno == ERANGE) comparison fails.

The problem is probably that errno value should be set to zero before calling strtod() function, which doesn't change it in the successful case. See line 531 in load.c

Preprocessor macro for easy iteration of objects

Hello @akheron,

First of all, I would like to thank you for taking the time to develop this library. It's incredibly well written and of immense value to a project I'm currently working on. The API is really consistent and pretty sane.

I would like to propose the addition of a new macro to the main header: json_object_foreach, with the purpose of reducing the amount of code required to iterate over an object. The macro definition would be something along the lines of:

#define json_object_foreach(k, v, x) \
    const char *k; \
    json_t *v; \
    for(void *__json_iterator__ = json_object_iter(x); \
        __json_iterator__ && \
        (k = json_object_iter_key(__json_iterator__), v = json_object_iter_value(__json_iterator__), 1); \
        __json_iterator__ = json_object_iter_next(x, __json_iterator__))

That allows the simplification of iteration code from:

const char *key;
json_t *value;
void *iter = json_object_iter(obj);
while(iter)
{
    key = json_object_iter_key(iter);
    value = json_object_iter_value(iter);
    /* use key and value ... */
    iter = json_object_iter_next(obj, iter);
}

To:

json_object_foreach(key, value, object) 
{
    /* use key and value ... */
}

I could not think of any drawbacks (performance of otherwise) of using this. If you like the idea, I can fork the repo and issue a pull request with the code.

Thanks.

Encoding real numbers is locale specific

With locales other than C or POSIX, the decimal separator may be something else than '.'. In this case, the encoders produces invalid JSON when it encodes real numbers.

Bug see with static analyser

Please find below 2 errors detected by static analyzer:

Pointer 'json_string_value(string1)' returned from call to function 'json_string_value' at line 730 may be NULL and will be dereferenced at line 730. : D:\temp\jansson-2.0.1.tar\jansson-2.0.1\jansson-2.0.1\src\value.c : 730 : Error : Analyze

Pointer 'json_string_value(string2)' returned from call to function 'json_string_value' at line 730 may be NULL and will be dereferenced at line 730. : D:\temp\jansson-2.0.1.tar\jansson-2.0.1\jansson-2.0.1\src\value.c : 730 : Error : Analyze

Nitpick: index.rst undefined link document "upgrading"

Running Sphinx v1.0.7
loading pickled environment... not yet created
building [html]: targets for 6 source files that are out of date
updating environment: 6 added, 0 changed, 0 removed
reading sources... [100%] tutorial
jansson-2.0/doc/index.rst:32: (WARNING/2) toctree contains reference to nonexisting document u'upgrading'

Test failures on MinGW

An extra zero is (sometimes?) preceded to the exponent of real numbers. Also, everything in the invalid-unicode suite fails even though the expected and actual outputs look similar.

Make it possible to disable UTF-8 support in favor of ASCII only

Add a ./configure option --with-ascii-only or such that would switch the library to an ASCII only mode. In this mode, only 7-bit ASCII would be accepted and all UTF-8 related code would be replaced with (probably smaller and more efficient) code that only handles ASCII.

One possible use case would be low-specification systems with very limited CPU or RAM.

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.