问题
My interface system provides a result from the Erlang world sending a string reppresentation of an erlang term, such as a list of tuples:
[ {"key1" , ["AAA", "BBB"] } , {"key2" , ["CCC", "DDD"] } ]
Once I receive this string in Java, I want to parse it (deserialize) using the OTP library ( com.ericsson.otp.OtpErlangList, etc).
I do not want to connect directly to Erlang OTP or use Json or Xml in my situation ( trying to handle it as native as possible, if possible ).
When I convert the string to a OtpErlangList, I get a list of integers, so overall I believe it simply converted to the character codes since a string in erlang is a list of codes. It appears there is no de-serializer in JInterface. If this is the case then I am forced to go for JSON.
There must be a way, since the OtpErlangObject ( and all other sub classes) have a toString method. Why would they not give the ability to reverse that ( fromString(String str) ) ?
Reiterating: How to convert an erlang term string representation into JInterface objects in Java right from a String ( without node connections, interfacing with JSON, etc).
回答1:
You said as native as possible and no json etc.. but I would use protocol-buffers or thrift if you make calls to erlang from java. There is also bert but I haven't experimented with it. I think these would be safer than dealing with string de/serialization and save you feature headaches.
回答2:
JInterface doesn't provide such functionality AFAIK. It is intended for communication with Erlang nodes via Erlang internal network protocol. In your case I would probably write a parser with something like antlr. Basing on a grammar, you can build any object structure you like.
回答3:
I don't understand how parsing the Erlang string is any more native than parsing JSON. However, if you really want to do this, you might be able to use the OtpInputStream class, and initialize it with they byte array of the String you want to parse. Then call read_any to get an OtpErlangObject. The documentation is here: OtpInputStream.
回答4:
Credits to the collaboration here is final answer:
The final answer is simply that there is no erlang term string representation deserializer for Java (or other language) as far as I know. But...
1) At the moment there is no Java Interface (JInterface included) that supports the deserialization of an erlang term from native string term representation
2) Jinterface supports the OtpInputStream, but it expects the Erlang External Term Format as described here
3) The above external term format is binary data. The transmission of the data to a Java interface must be done as such using the term_to_binary BIF.
4) Once the data is streamed back to an OtpErlangObject on the java side (using JInterface), it can be expanded towards its inner data, casting the OtpErlangObject to whatever was sent ( OtpErlangList, OtpErlangTuple, etc)
Erlang side:
ExternalFormatBinData = term_to_binary(MyTermToBeSent),
%transmit data using messaging or other media
...
The Java side of the code
OtpInputStream otpInputStream = new OtpInputStream(receivedBinaryDataArrayOfBytes)
OtpErlangObject erlangObject = otpInputStream.read_any();
OtpErlangList erlangList = (OtpErlangList) erlangObject // replace OtpErlangList to whatever is being sent
...
回答5:
https://github.com/metadave/etp will convert a String into a Java object model (without node connections, interfacing with JSON, etc). Simply build a JInterface model from this in memory representation.
The final answer is simply that there is no erlang term string representation deserializer for Java (or other language) as far as I know.
That's what the etp project is (above).
来源:https://stackoverflow.com/questions/15775554/how-to-deserialize-an-string-serialized-erlang-term-into-a-jinterface-object-in