Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
I'm working on a project in java which is replacing an old C# program. In the old C# program, users could save out the data they were working on to a GZip file, written by Serializing a wrapper object which contained all the data, and writing it using a GZipStream.
I would like to offer users the option to load in data saved with the old system. Opening the GZip file using Java's GZipInputStream is easy enough, but how do I deserialize the object?
I did a little research and found this question: Can a serialized simple java object be deserialized by C#?
The answer there said that there's no library to do this, but there is documentation on how Java serializes objects, and it is possible to write your own code to deserialize them.
Is there a known way to do this from C# to Java? Or should I write my own converter from scratch?
You have to serialize everything in a JSON or XML file. No binary data should be stored in the file at all. Then you can de-serialize it within Java by using an arbitrary library.
You might serialize it with JSON.net and deserialize with Jackson.
UPDATE: In fact you might in fact write a SaveData class, compile it into a .dll and make it available for Com interop. Then, there are some java third party libraries which can make use of the SaveData class inside the dll so that it can interoperate in the java application. Then, serialize this SaveData into a file (binary format is ok). However I'm not sure how serialization is going to work out on byte-level. Are you really going to get the same object when you deserialize it on the java side? This solution might work but it is ugly as hell and at least as "painful" as re-writing the c# app to write to json
You could embed a tiny .NET application inside your jar and invoke the .NET application using this SO question
The .NET application would deserialize the file into .NET objects and then convert the .NET objects to xml or json. The xml/json could then be deserialized into equivalent java objects in the JVM
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm currently on a school project, we made a FuzzyLogicFramework in C++.
Now, I try to use this framework in Java in order to do an android app using the framework, I have a factory called FuzzyFactory in the c++ project.
the prototype for its methods are like this :
Expression<T>* newAnd(Expression<T>*, Expression<T>*);
I don't necesserly want to use all the avaiable types that is why I created a class like this :
template<> class FuzzyFactory<float>;
I generated a DLL library with this class.
My questions are the following:
I want to use this library in a Java file : what technology could I use?
I heard about JNI and was able to run basic functions with it, but would I be able to manage objects instanciation ? How would I be able to give pointers in my functions arguments in Java ? I was able to run my c++ project main in Java using JNI, but it seems using a Factory class with it is on another level.
I also heard about wrappers and JNA
I just need somebody to told me a technology to use, It's my first time trying to do cross language implementation so I'm a bit overwhelmed.
if you want to see a bit more about the current state of the project : https://gitlab.com/MelvinC/languageframeworkproject-ensisa-2020
Should my Java project and c++ project be in the same git repository ?
Sorry for my english, if it is not good enough.
Thank you in advance for reading and helping me.
SWIG has some support for C++ templates and can generate Java binding code from C++ headers. That will give you a very low-level, but hopefully usable interface.
Alternatively, you might want to consider some sort of external representation of your logic program. You can then use plain Java code to construct the program, then serialize it into the external representation and pass it to the C++ side.
For inspiration, the Z3 SMT solver accepts a representation called SMT-LIB.
Alternatively, you can create your own format using S-expressions, XML, or even just nested JSON.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am basically interested in analyzing the object form that we need to send to client over network in client-server model.
i need to know what are the criteria that we need to consider when we choose xml and java serialization respectively. which is the best approach to choose for object transform over network.
why serialization came into picture when we have already XML,JSON transformation already
Edit :
I wanted to know the why serialization is being used when we have XML/JSON already being used before its invention
If XML and JSON works for you I would stick with that. It is much easier to log and check it is doing what you believe it should be.
Java Serialization has many advantages however unless you need these they are unlikely to be the best solution.
it is built in, no extra JAR is required.
it is integrated with existing remote APIs
it supports all serializable object types and graphs. (XML and JSON only support trees of data, not typed objects)
it supports data types and you only write each reference once.
However Java Serialization is not
very efficient
easy to read/validate as a human.
flexible for schema changes esp changes in package or class names.
portable outside Java.
Personally, my preference is YAML. This is because
it was designed to be human readable where as XML is a subset of SGML and JSON a sub-set of Javascript.
it supports graphs and data types.
it is almost as efficient as JSON (it's a bit slower as it does more)
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Recently,I read code of HBase.I find client use protobuf to communicate with server in HBase's code.
Java has "Serializable". why not use it?
Efficiency: protocol buffers are generally much more efficient at transmitting the same amount of data than Java binary serialization
Portability: Java binary serialization is not widely implemented outside Java, as far as I'm aware (unsurprisingly)
Robustness in the face of unrelated changes: unless you manually specify the serializable UUID, you can end up making breaking changes without touching data at all in Java. Ick.
Backward and forward compatiblity: old code can read data written by new code. New code can read data written by old code. (You still need to be careful, and the implications of changes are slightly different between proto2 and proto3, but basically protobuf makes this a lot easier to reason about than Java.)
It's a lot easier to accidentally introduce non-serializable members into Java binary serialization, as proto descriptor files are all about serialization... you can't refer to an arbitrary class, etc.
I've worked on projects using protocol buffers, and I've worked on projects using Java binary serialization - and I would be very reluctant to use the latter again...
Protocol Buffers is an open serialization protocol. You could write a client in C++ or C# and still be able to communicate with the server if both ends are using the same Protocol Buffer schema. Java Serializable is Java only
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
For instance, when I know how all representations should look like, which path has more advantages.
Creating/Annotating Java classes and then document them in (lets say) XML documentations.
Write XML representation and then generate Java classes.
From what I may think of:
First approach would increase development speed. Since documentation is the last step (when it's feasible).
Second approach may let me feel as I am the client (for my own App) and will lead me to consider, how practical is my representation from a user standpoint.
But I have no other ideas, while I assume that this topic might be important in some cases, that I just don't know yet.
My answer is "it depends".
I've written quite a lot of REST services and I stick by very simple rules:
If this is a public facing API and by public I mean that you do not control all the clients consuming it, or once they're released they're in the wild then I will write the API first and make my Java model fit into that
If it's purely internal and you have the ability to change at will then go for the Java model first and just use whatever representation gets spit out.
The latter is the faster development model but it relies on you not really caring about what the actual representation looks like.
Java POJO's can easily be serialized to xml so I would generate the xml from existing java POJOS (although I agree with the commenter json is usually better)
I would go for POJOs like ghostbust555. The reason for that is XML is just one of many serialization formats. You could JSON or even your proprietary text format (or binary format).
If you take plain POJOs you can then annotate them for XML marshalling / unmarshalling via JAXB for instance. You could also use them for binary protocols such as Apache Thrift.
Lastly you could actually start from a UML class diagram and use that to generate your POJOs. That helps you document your model better.
creating POJOs is better then XML documents.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Is there a Java equivalent of Python's "construct" library? I want to write "structs" like so:
message = Struct("message",
UBInt8("protocol"),
UBInt16("length"),
MetaField("data", lambda ctx: ctx["length"])
)
It doesn't have to specifically be a library with some sort of abstraction using the Java language. I mean, it could be a "portable" format, with an API for parsing the documents. I guess this could work out with XML, but it would be be a lot more ugly.
I realize I could just inter-operate with Python, but I don't want to do that.
I've looked a lot around and all I could find was Ragel (www.complang.org/ragel), that can also produce Java code.
It looked too complex for me so I've started some work to port Construct to Java.
I suspect it would be easier to make something like that in Scala, Groovy or JavaScript.
Construct on GitHub: https://github.com/MostAwesomeDude/construct
java construct: https://github.com/ZiglioNZ/construct
I've spent a couple of days on it, mostly looking for equivalents of python's expressive classes.
The most useful java classes I've found are: java.util.Scanner, java.util.Formatter and java.nio.ByteBuffer.
It's a big task so I want to focus on something small like creating simple parsers and formatters for ByteBuffers.
[Update]
I've ported enough code to parse and build some of the protocols that come with Python Construct, such as ethernet, arp and ipv4. Check it out at https://github.com/ZiglioNZ/construct
[Update: new Release]
Java Construct 1.1.2 is now available, see release notes.
You can use DataInput/DataOutput (and their implementations) to convert any set of values from/to a set of bytes. This doesn't give you an object where you can use names to access the individual fields, though - you would have to create such yourself.
It depends a bit on what you want to do - do you have a fixed data format to send/receive on wire, or does this vary from time to time?