I have the following thrift file:
union D{ 1: string s; }
struct B{ 1: required D d; }
struct C{ 1: required D d; }
union A{ 1: B b; 2: C c; }
service Test { void store(1: A a) }
And I have the following JSON object, which was obtained by parsing a string.
var data_json = {
'b': {
'd': {
's': "hello"
}
}
};
I'm trying to write a thrift client in Nodejs which calls the store
method with data_json
as its argument, but I get the following error when I do so:
/home/aakash/Documents/thrift0/gen-nodejs/test_types.js:225
this.b.write(output);
^
TypeError: Object #<Object> has no method 'write'
at Object.A.write (/home/aakash/Documents/thrift0/gen-nodejs/test_types.js:225:12)
at Object.Test_store_args.write (/home/aakash/Documents/thrift0/gen-nodejs/Test.js:57:12)
at Object.TestClient.send_store (/home/aakash/Documents/thrift0/gen-nodejs/Test.js:113:8)
at Object.TestClient.store (/home/aakash/Documents/thrift0/gen-nodejs/Test.js:105:8)
at Object.<anonymous> (/home/aakash/Documents/thrift0/client.js:40:8)
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)
However, it works fine when I pass the following object as argument:
var data_thrift = new ttypes.A({
'b': new ttypes.B({
'd': new ttypes.D({
's': "hello"
})
})
});
Is there a way to pass data_json
directly to store
, or a way to convert data_json
to data_thrift
?
I'm trying to write a thrift client in Nodejs which calls the store method with data_json as its argument, but I get the following error when I do so:
/home/aakash/Documents/thrift0/gen-nodejs/test_types.js:225
this.b.write(output);
Is there a way to pass
data_json
directly tostore
, or a way to convertdata_json
todata_thrift
?
Sure. Just provide a write() method for each of your Object
s that implements what is generated by Thrift otherwise, or provide an alternative serialization mechanism for your data to produce the exact same output.
The code generated by Thrift and the Thrift library provide the necessary infrastructure for using Thrift and to ensure the interoperability of both Thrift RPC calls and serialized data. To achieve this, the serialized data follow a certain predefined structure, which is determined by the protocol you use. All the infrastructure of an RPC and serialization framework, including the generacted code for your data, is just there to provide the means to do this.
If your application holds data in another way internally (which is perfectly ok) you will have to convert them back and forth. The only way to store the data directly would be to generate the exact same JSON output that the Thrift infrastructure would produce for your data, othwerwise the other side will not be able to deserialize your JSON.
But technically, following that path you will end up reinventing the wheel.