JSON to C-struct

2,779 views
Skip to first unread message

AJ ONeal

unread,
Sep 13, 2010, 5:07:43 PM9/13/10
to node.js mailing list
I've started working on an app which, given a JSON meta-document, will produce the C code (.h and .c stub) necessary to parse the corresponding JSON document and assign it to the correct structs using JSON-C.

Does anyone else already have something like this?

Or would it be easier to write a plugin for node that packs the data in a particular format for C?

AJ ONeal

Raffaele Sena

unread,
Sep 13, 2010, 5:19:27 PM9/13/10
to nod...@googlegroups.com
Don't know if anything like this already exists, but one option would be to convert the JSON document to a protocol buffer (http://code.google.com/p/protobuf/). Then it would be easy to generate the code that parses the protocol buffer in pretty much any language.

If you are thinking about making a JSON structure available to a node.js plugin, this either works already out of the box (what happen when I pass a JS object to a native method ?)  or all you have to do is convert the JSON structure into a nested dictionary / hashtable.

-- Raffaele

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

AJ ONeal

unread,
Sep 13, 2010, 5:29:31 PM9/13/10
to nod...@googlegroups.com
I'll definitely keep protobuf in mind for the future and refer to it for hints as to how to write my meta-coder. I'm using strict C, not C++, nor a node plugin.

Perhaps I'll be able to use the same binary format. It's interesting.

AJ ONeal

AJ ONeal

unread,
Sep 13, 2010, 5:32:13 PM9/13/10
to nod...@googlegroups.com

AJ ONeal

unread,
Sep 13, 2010, 5:34:18 PM9/13/10
to nod...@googlegroups.com
I'm looking forward to hearing from others / seeing examples, but thank you very much Raffaele - you definitely got me on the right track!

http://code.google.com/p/protobuf-js/

AJ ONeal

AJ ONeal

unread,
Sep 13, 2010, 5:39:49 PM9/13/10
to nod...@googlegroups.com

fuzzy spoon

unread,
Sep 13, 2010, 5:44:36 PM9/13/10
to nod...@googlegroups.com
Another interesting note, If you are a c++ user looking for easing the
work of JSON style code,

Check this blog post out:
http://parrottdev.blogspot.com/2010/01/simple-almost-elegant-generic.html

It basically gives you a "property container", using boost (its also
header only).

The fun part is using boost::function and boost::bind, adapters and
things like v8 handles,
you can basically emulate a javascript object quite easily.
It uses boost::any, so reading in via JSON (i use json_spirit) it
comes in handy for
just assigning all values to an "object".

:)

Alan Gutierrez

unread,
Sep 14, 2010, 8:24:08 PM9/14/10
to nod...@googlegroups.com

I'm writing such a plugin and would love to have a first user.

Look at Structure API (Synchronous)

http://github.com/bigeasy/node-packet/wiki

But, I'm actually finishing up that part of the API this evening. It can
be ready for use tomorrow. Find me on IRC as prettyrobots.

Alan Gutierrez

--
Alan Gutierrez - al...@blogometer.com - http://twitter.com/bigeasy

Reply all
Reply to author
Forward
0 new messages