Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON in ANSI C [closed]

Tags:

json

c

I'd like to read JSON-encoded data into C structs. The structure of the json data is known in advance, relatively flat and mimicked by some C struct typedefs. An array at the third level or so contains an extremely lengthy list of JSON objects which have to be processed one at a time.

The code is intended to run on a very constrained system so the library should not dynamically allocate memory.

I know there is Crockford's List of JSON libraries, but I'm not quite sure which one is the best fit for the stated problem.

like image 915
wnrph Avatar asked May 20 '12 15:05

wnrph


People also ask

Is JSON parse blocking or not?

A function that does not accept a callback or return a promise blocks until it returns a value. So yes it JSON. parse blocks. Parsing JSON is a CPU intensive task, and JS is single threaded.

What is JSON parse () method?

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

Is JSON a security risk?

JSON alone is not much of a threat. After all, it's only a data-interchange format. The real security concerns with JSON arise in the way that it is used. If misused, JSON-based applications can become vulnerable to attacks such as JSON hijacking and JSON injection.

Are newlines valid in JSON?

JSON strings do not allow real newlines in its data; it can only have escaped newlines. Snowflake allows escaping the newline character by the use of an additional backslash character.


2 Answers

Try jsmn lib, I love that it can parse any json file with only two malloc's.

jsmn is a minimalistic library for parsing JSON data format. It can be easily used in small projects or can be integrated into embedded systems.

jsmn is a good choice, because:

  • it is compatible with C89 compiler version
  • it uses no dynamic memory allocation
  • it has the smallest possible overhead
  • it needs only one pass to parse JSON data
  • it has no dependencies, even libc
  • it is distributed under MIT license, so you can use it in your proprietary projects
like image 80
jimon Avatar answered Oct 21 '22 18:10

jimon


Try with json-c is one of the most common and it is open source and work also on Windows (Win32).

JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects.

like image 20
aleroot Avatar answered Oct 21 '22 19:10

aleroot