0

I successfully got an schema object created by jsonix. Now I want to convert json strings to xml. This is my code:

var Jsonix= require('jsonix');
var context= new Jsonix.Context([myschemaobj]);

here I get the error that Jsonix.Context is not a function. What ist wrong with my code?

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
saab
  • 129
  • 1
  • 3
  • 16

1 Answers1

1

Jsonix node.js module exports an object named `Jsonix``. So you have to require Jsonix and your mappings as follows:

var Jsonix = require('jsonix').Jsonix;
var PO = require('./PO/Mappings').PO;

And then:

var context = new Jsonix.Context([ PO ]);
var unmarshaller = context.createUnmarshaller();
var result = unmarshaller.unmarshalString('<comment>test</comment>');
test.equal('comment', result.name.localPart);
test.equal('test', result.value);

Disclaimer: I'm the author of Jsonix.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • Thank you lexicore. I followed your advice. I got a new error message: Cannot read property 'apply' of undefined .... node_modules/jsonix/jsonix.js:44:18 I have to mention that I had created the "myschemaobj" under windows, and then copied it to my project folder in ubuntu (vb). The reason is I have no java runtime env in my ubuntu virtualbox environment. Is that possibly an error source? – saab Jul 10 '16 at 13:41
  • No, Jsonix runtime has no dependency on Java, it's pur JavaScript. Please provide your source code and the full error stack trace. – lexicore Jul 10 '16 at 15:32
  • My mistake :) I had forgotten to instantiate the Jsonix-class(new-keyword forgotten: var context= Jsonix.Context([myschemaobj]); ) – saab Jul 11 '16 at 09:26