[node-ffi] how is the proper way pass json object to ffi library function?

1,297 views
Skip to first unread message

Muhammad S Maromi

unread,
Jul 1, 2014, 11:18:56 PM7/1/14
to nod...@googlegroups.com
I tried the "straightforward" way, but it produces this error:

$ node learn.js 

/Users/msmaromi/learnnode/trenupp/node_modules/ffi/lib/_foreign_function.js:55

      throw e

            ^

TypeError: error setting argument 0 - writePointer: Buffer instance expected as third argument

    at Object.writePointer (/Users/msmaromi/learnnode/trenupp/node_modules/ffi/node_modules/ref/lib/ref.js:740:11)

    at Object.set (/Users/msmaromi/learnnode/trenupp/node_modules/ffi/node_modules/ref/lib/ref.js:482:13)

    at Object.alloc (/Users/msmaromi/learnnode/trenupp/node_modules/ffi/node_modules/ref/lib/ref.js:514:13)

    at Object.ForeignFunction.proxy [as tesJson] (/Users/msmaromi/learnnode/trenupp/node_modules/ffi/lib/_foreign_function.js:50:22)

    at Object.<anonymous> (/Users/msmaromi/learnnode/trenupp/learn.js:18:9)

    at Module._compile (module.js:456:26)

    at Object.Module._extensions..js (module.js:474:10)

    at Module.load (module.js:356:32)

    at Function.Module._load (module.js:312:12)

    at Function.Module.runMain (module.js:497:10)


Nathan Rajlich

unread,
Jul 2, 2014, 5:52:56 PM7/2/14
to nodejs
Can you show us some code? Hard to guess what's happening here.


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/a955d096-44f2-4acf-b1d3-c0f50ab97f89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ryan Schmidt

unread,
Jul 2, 2014, 5:52:56 PM7/2/14
to nod...@googlegroups.com
What do you mean exactly when you say "json object"? Do you mean a JavaScript object like this:


var o = {foo: 5};


Or do you mean a string representation of that object in JavaScript object notation (JSON) like this:


var j = '{foo: 5}';


For the latter, I would think you could pass the buffer representation of the string j to an ffi function. That function would need to know how to decode JSON-formatted strings.

For the former, you would need to convert o to JSON first (e.g. using JSON.stringify(o)).

Muhammad S Maromi

unread,
Jul 3, 2014, 4:36:01 AM7/3/14
to nod...@googlegroups.com
Hi Nathan,  I want to do is, let's say I have object:

var obj = {foo:1, bar: 2}

then pass that object to ffi function. 
What is the parameter of function (in C module) should be?


Thanks ryan, let me check what'll it goes.

Muhammad S Maromi

unread,
Jul 3, 2014, 4:51:24 AM7/3/14
to nod...@googlegroups.com
Sorry, this is my code:

// utility.c
void tesJson(char *text) {
  json_t *root;
  size_t i;
  json_error_t error;

  root = json_loads(text, 0, &error);
  free(text);

  for(i = 0; i < json_array_size(root); i++) {
    json_t *data;

    data = json_array_get(root, i);

    printf("%s\n", json_string_value(data));
  }
}

//learn.js
var utility = ffi.Library('utility', {
 'tesJson': ['void', ['pointer']]
});
var obj = {'1': 'one', '2': 'two'};
utility.tesJson(JSON.stringify(obj));


On Thursday, July 3, 2014 4:52:56 AM UTC+7, ryandesign wrote:

Nathan Rajlich

unread,
Jul 3, 2014, 7:21:27 PM7/3/14
to nodejs
Muhammad, your code looks very close to me. Note that I haven't tried this code, but try something like this: https://gist.github.com/TooTallNate/d1f8ee799e9764c0ab18

I've only changed:

 * The "pointer" argument type to "string"
 * Don't call `free()` on the passed in char *, V8/node will take care of that for us and we don't want a double-free

Let me know if that works for you. Cheers!


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.

Muhammad S Maromi

unread,
Jul 3, 2014, 8:11:20 PM7/3/14
to nod...@googlegroups.com
Yeaay, it works. Thanks so much!!

Anw do you have any idea how to print some variable in C to console? I tried "printf", but I think node doesn't accept that.
Reply all
Reply to author
Forward
0 new messages