Serialization java to c# - c#

I have manged to serialize an arraylist in java using xstream, send it to my c# application via http and then write that to file (just for now).
The serialized data was an arraylist in java. I want to try and re form this arraylist in c# from the xml i have.
I have been looking at http://code.google.com/p/xstream-dot-net/ to do this. Does anyone know if that is the right way to go or is there a better way of reforming the serialized data in c#?

I have used JSON.NET (Newtonsoft) on the .NET side and Google's GSON on the java side and it seems to work for Dictionaries, Maps, Sets, Lists, Date, and primitive types with very little configuration involved. Refer to https://github.com/chandru9279/Spikes for it.

Related

Can a serialized simple java object be deserialized by C#?

Assuming all fields of a java class are java primitives, if such an object has been serialized, can it be successfully deserialized by C# into an instance of an "equivalent" C# class?
Is the reverse possible - C# to java?
I realise there are many language agnostic formats, such as XML that could be used to get the job done. I am more interested in whether using the native serialized data is feasible.
The formats of serialized streams are available. I think you can write a class easily to parse the byte stream and create the required class in C#.
An article that specifies the serialized format:
http://www.javaworld.com/community/node/2915
WOX will be helpful to achieve interoperable serialization.
it can serialize/deserialize Java/C# objects into/from standard XML(platform independent)
This is not possible, at least not using the native serialization libraries that both frameworks provide, as stated in this previous SO post.
If you want to achieve cross language serialization/deserialization, you could resort to XML (XSTream for Java, XStream-dot-net for C#) or WOX:
WOX is an XML serializer for Java and C# objects. In other words, WOX
is a library (woxSerializer.jar for Java, and woxSerializer.dll for
C#) to serialize Java and C# objects to XML and back again.
If you're OK with including another dependency, you might consider using an object database such as db4o for the job. I haven't tried this myself, but according the Wikipedia article,
db4o uses a custom feature called "generic reflector" to represent class information, when class definitions are not available, which allows to use it in a mixed Java-.NET environment, for example Java client - .NET server and vice versa.
You can find more information about the above-mentioned reflection API here and here.
In a nutshell, this would lead to a system where you store your Java/C# objects to an (embedded) database (i.e., without client/server architecture, but by loading a single file that contains the whole database) and retrieve C#/Java objects from the database afterwards.
I have used this document with a high amount of success to parse data stored in serialized format on a database:
http://www.jtech.ua.es/j2ee/2005-2006/modulos/rmi/recursos/serial-1.5.0.pdf
The most meaningful info for me was from page 63 to 68.
In my case I had the source code used to serialize the data which was useful to both identify fields and read the data when was written in a non standard way using the ISerializable.WriteObject/ReadObject calls.
I don't know the reason but my serialized data had not "handler" field on any object, it would take 0 bytes. Other than that, everything followed the docs but it gets kind of tricky if you have never done such kind of tasks before
As noted on some comment, this is a good base even if it's written in java:
https://github.com/smartplatf/a-utilities/blob/master/src/main/java/org/anon/utilities/serialize/srdr/SerialStreamReader.java

Sending XML format messages through TCP in C#

I have a C# TCP chat program. Currently, I have formatted the messages sent using strings i.e, a "login" message starts with a "3" then followed by a "U:" then the username etc.
I think this method is very crude in a way that it's not really readable and not standardized. In early research, I have read that I can format my messages using XML but I dont know where to start exactly. Do I just make a string builder and append it tags like .append("<Login>"+message)?
The most common approach for dealing with a problem like this is to use serialization. Serialization is the process of converting an in-memory object into a format that can be easily streamed "over the wire," and de-serialization is the reverse process of converting the serialized format back into an object. .NET has good support for XML and binary serialization out-of-the-box, but there are other ways to implement this. Here's a link to get you started:
http://msdn.microsoft.com/en-us/library/7ay27kt9(VS.71).aspx
You can send whatever you like over the connection - as long as it's just for your program it doesn't really matter what you choose. Xml might give you some benefits as it lends itself to some kind of more structured messages and there are many classes and tools and knowledge around on the net regarding XML. JSon format might be another option - it will make it potentially easier creating a JavaScript client for it in case you want to go web based.
Unless there is a reqirement that 3rd parties be able to read these messages then I would probably favour binary serialisation, as it has a more compact format.
That said, I'd probably just use WCF rather than uisng TCP directly.
If you want to know more about XML serialisation then the most commonly used methods are:
Generating a stronly typed C# object decorated with attributes to control XML serialisation using XSD.exe, and then using XmlSerializer to serialise and deserialise XML. (recommended)
Using the XmlDocument class
You can write our XML yourself as a string, but its better to use the serialisation methods made available in the .Net framework as it makes things considerably easier and reduces the chance that you will make a mistake and inadvertantly start working with invalid xml.

LINQ to JSON in .NET

Is there such a thing as a JSON file? That is, *.json?
Can JSON be used in C# code without any JavaScript stuff, sort of as a replacement for XML?
And is there any official LINQ to JSON stuff around for C#?
I did find one website for my last question, but it took me to a page to download JSON.NET, and that page doesn't seem to mention anything about LINQ.
Yes, there is such a thing as a *.json file. The MIME type is application/json (source). JSON is a text-based format though, so you could hypothetically store JSON-formatted data in a text file with whatever extension you choose.
JSON can absolutely be used independently of JavaScript. In some cases, it's probably better suited to representing your data than XML. JSON.org has a great comparison page between JSON and XML.
JSON.org lists several JSON libraries for C# (for example, JSON.NET which you have already discovered), and most (if not all) of the collections that these libraries use should support LINQ. JSON.NET definitely does offer support for it. See here or here.
Everyone tends to stick with the JavaScriptSerializer (from the System.Web.Extensions library) when working with JSON in .NET. The handy part about this is the ability to create a custom JavaScriptConverter that will take custom objects and serialize them the way you chose. Likewise, you can make a deserialization method to receive in custom JSON formatting.
Though this of course depends on your application. Given that it's a Windows Forms application, is there any particular reason you'd chose JSON over storing the information natively or just use the XML format? If your application communicates with webpages, the JavaScriptSerializer is probably the best bet, though if you're using it to store/retrieve settings I'd use XML. And, if it's necessary to synchronize your application with a web-based one, just serialize to JSON when the time is ready.
You can deserialize your JSON file into C# objects. After that, you can query with LINQ on these objects.

Does C# have any built in objects for turning JSON text into manageable objects or do I need a third party library?

I have all this JSON text that I want to deserialize (or something) into an object with variables so I can run through it and add/change some things and then serialize it back to text. Is there something built in from Microsoft for this?
WCF has the DataContractJsonSerializer but I haven't used it myself - I've always gone for Json.NET which I've found to be broadly excellent.
I'd expect the WCF serializer to be a good fit if you're using WCF, but if you're writing a standalone app, I'd go for Json.NET. It's a pretty straightforward dependency. In particular, I like the fact that I don't actually have to model the classes directly in order to use Json.NET - I tend to use the "LINQ" side of the library which is a little like LINQ to XML, but applied to JSON. I deal directly in JArray, JObject, JToken etc and let Json.NET just do the parsing/formatting.
If you were using MVC, there are JSON methods, for output, but it is also possible to use action filters to deserialise json that is passed in. There are some quirks to this but it does work well with json submitted via jQuery etc.
Check this out
The fancy new "AJAX" stuff come with JavaScriptSerializer (built into Framework 3.5 and later, at least) It's not quite as flexible as, say, the XML serializers but it does the job in many cases.
I believe there are some built in, but I don't think they are terribly powerful by themselves. We have always used this library. It's very powerful and easy to use.
It's fantastic for serializing objects and lists or deserializing JSON.
http://json.codeplex.com/

Using C# to serialize a Java deserializable object

I have two application that need to talk to each other. App1 needs to be able to serialize an object that App2 can then deserialize. Easily done, right? Here's the problem; App1 is C# based, App2 is Java based. So App1 needs to write out the file in the Java binary file format. How can this be done?
The way I see it, I have two options. The first is figure out some way to serialize a Java object in C#, so that App1 just creates the appropriate file. My other option would be to write a converter in Java that reads in a file and populates the object accordingly and serializes the newly populated object. That way the C# app would only have to write out some sort of formatted text file that the converter then interprets.
I can't make any changes to the Java application.
How should this be done?
Update:
The Java application is already in the hands of customers so changing the serialization scheme would cause the customers existing data to be incompatible. The Java App uses the native java serialization when dealing with this object. Modifications to the Java app can't happen.
The C# app uses protocol buffers to serialize its own data.
The best answer is option 3:
use a language-neutral serialization scheme.
I use JavaScript. Thrift is another option, protocol buffers I believe are more focused on RPC, but should be usable for serialization as well. XML or a custom binary format would be other options.
Edit:
Sorry, didn't notice that you can't make changes to the Java application. That said, the best way to do it would probably be to create your own well defined format, write a java app that can read that format, then output a serialized java object for the legacy app.
"IKVM" might be something you could use. This product allows you to convert compiled java bytecode (.jar, etc.) into a .NET DLL. It's super easy to use, and might give you the interop you need.
Other than this, the easiest way to accomplish this without a binary-level interop is to just use a plain text format, such as a CSV or XML.
Just use XML serialization. Both frameworks have good support, and the simplicity will make it easier to debug / maintain. Write a small program in Java that just imports the XML and writes the binary file.
Your best bet would be to write something that uses Java Native interface. Not fun, but it'll work.
You can do this directly using JNI (not fun but doable) or there may be some tools out there that will generate code for you -- take a look at SWIG: http://www.swig.org/
You would call Java from C# to do the persistence for you.

Categories