News for hackers. Stuff that matters.
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by jlarocco on Friday November 14, 2014 @02:38PM (#8609058)
This uses std::string all over the place, which allocates memory under the hood.

I'm not sure how any JSON parser could avoid memory allocation without a difficult to use interface. Numbers, arrays, strings, and objects are unbounded by the JSON spec, so a truly malloc free library would need to provide a kind of streaming interfaces where things are returned in fixed sized chunks.

JSON wouldn't be my first choice for data storage in situations where I needed to avoid dynamic memory.

Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by matt42 on Saturday November 15, 2014 @12:26AM (#8610930)
The parser does not extensively use std::string. to parse json keys, It uses an internal stringview class mirroring the json string (string_view is candidate to get into the next C++ standard). When I said malloc free, I meant that no intermediate memory is allocated: actually, If your json object only contains int or floats, the parser will not use malloc: it will just traverse the string and fill the scalars of your destination object (thanks to static introspection), a big difference with state of the art json parsers returning an intermediate dynamically allocated tree that you have to interpret after the parsing. Of course, if the destination object stores instances of std::string of std::vector, the iod library will allocated them and fill them with the json values.
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by Iftheshoefits on Friday November 14, 2014 @02:55PM (#8609142)
It also uses vector, which is almost certainly using malloc and/or placement new even if the SIO somehow really does reserve space for a single object. Its also using the heap unless there is some hidden Allocator object in the code that explicitly is using stack allocated space.
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by areop on Friday November 14, 2014 @03:14PM (#8609236)
I wrote a JSON-Parser in C with minimal validation in ~250 LOC that does not allocate memory, but leaves the type check and conversion to the user. https://github.com/vurtun/json
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by jeremie on Friday November 14, 2014 @11:35PM (#8610831)
It would be nice if the README mentioned it was originally based on my https://github.com/quartzjer/js0n :)
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by areop on Saturday November 15, 2014 @06:42AM (#8611484)
Added your project as a reference. Sorry for the missing reference I originally never planned to make the project known publicly and therefore never added the references.
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by klibertp on Friday November 14, 2014 @06:57PM (#8610267)
This is absolutely beautiful. I didn't know you could get an address of a label in C or to do a goto to an address from variable instead of a literal label. Is this new in C, or was it always there?
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by dietrichepp on Friday November 14, 2014 @07:01PM (#8610282)
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by techdragon on Friday November 14, 2014 @08:45PM (#8610545)
Is there an equivalent for Clang/LLVM?
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by cjcole on Friday November 14, 2014 @11:37PM (#8610836)
Yes.

http://blog.llvm.org/2010/01/address-of-label-and-indirect-b...

A venerable interpreter implementation trick.

Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by codehero on Friday November 14, 2014 @09:10PM (#8610619)
Unfortunately it won't port to IAR, which is a shame. I don't understand why pointers to labels never made it to the C spec; it's one of the (equivaling) features that actually makes assembler look elegant.
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by rxi on Friday November 14, 2014 @06:58PM (#8610274)
That test.c file looks familiar!
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by areop on Saturday November 15, 2014 @06:38AM (#8611479)
Yeah I was looking for unit test macros and found your project. I added your project as a reference in the README for clarification of the origin.
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by huhtenberg on Friday November 14, 2014 @04:59PM (#8609758)
That's a very nice code.
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by codehero on Friday November 14, 2014 @02:57PM (#8609151)
I wrote a JSON parser that avoids using dynamic memory but works a bit differently. It's a callback interface like SAX, but instead of pushing already "typed" data to the user, the user calls a function to interpret the data. In this fashion, if the user wants a float instead of an integer, they call the function for float. Likewise, if the user has some space allocated for string values, the library can convert the already inmemory JSON format string to UTF-8 in the user's buffer.

The C interface is purposely ugly but the C++ is much easier to work with.

https://codehero.github.io/benejson/

Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by jly on Friday November 14, 2014 @03:07PM (#8609213)
> JSON wouldn't be my first choice for data storage in situations where I needed to avoid dynamic memory.

I work a lot with very low-resource embedded systems and we like to use JSON host-side for structured data configuration, but convert to BSON (http://bsonspec.org) for target-side storage. This can be parsed and iterated with very low-overhead and no dynamic memory. Similar idea to protocol buffers but with JSON-like data.

Avoiding a build-time conversion would be nice, though, and this idea is interesting.

Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by chadaustin on Friday November 14, 2014 @03:24PM (#8609286)
sajson allocates once: it turns out you can structure your parse tree such that each byte of input only requires one machine word of output. http://chadaustin.me/2013/01/sajson-why-the-parse-tree-is-bi...

https://github.com/chadaustin/sajson

Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by gurkendoktor on Saturday November 15, 2014 @12:58AM (#8610981)
That's correct in the worst case, but in the best case, std::string does not allocate memory anymore (short string optimisation), e.g. libc++: http://stackoverflow.com/a/21710033

So it might be "malloc-free" for configuration files where the possible values are limited, etc. :)

Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by pjungwir on Friday November 14, 2014 @02:43PM (#8609081)
jsmn is a nice JSON tokenizer. You malloc a chunk of memory up front, but that's it. It gives you pointers to the tokens and you can do what you like with them. I am using it in an app that has a fixed-size data structure that is populated from a JSON file. So my code is malloc-free also.
Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by aktau on Friday November 14, 2014 @03:28PM (#8609299)
Link for those interested: https://bitbucket.org/zserge/jsmn/wiki/Home

jsmn is pretty awesome, and contrary to this (std::string, std::vector) actually puts you in charge of allocations. It's also in C.

In a similar vein, and also possibly already mentioned:

- yayl (C): https://lloyd.github.io/yajl/

- microjson (C): http://esr.ibiblio.org/?p=6262

Re:Show HN: A Fast, Malloc-Free C++14 Json Parser and Encoder by makomk on Friday November 14, 2014 @05:38PM (#8609939)
I've got a streaming JSON parser in GCC C around here somewhere that does basically that - numbers and keys are truncated, but everything else is chopped up into chunks and streamed. Someday I ought to clean it up, document it and release it. Runs quite nicely on an Arduino.