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.
Related
I am collecting data from various web services, some return XML, some JSON. I've looked into JSON.Net as a parser for JSON, and it looks like it works well to extract the items I need, specifically using Linq.
Question is, should I go ahead and use JSON.Net's conversion methods, and just turn all the XML I receive into JSON before processing, meaning I only need to write parsing methods for JSON.
Can anyone think of an advantage of having separate XML parsing code (quering it with Linq probably). Are there disadvantages to using JSON if the data structures start to get complex?
The only real difference I can see for using XML would be validating one's parse to a schema, although I have no idea as to JSON.NET's support for JSON Schema (currently an IETF draft).
Both formats can represent object graphs effectively so I don't think data structure complexity is a problem.
The only disadvantage of JSON that I have found is that XML is navigable. You can walk through the structure of an XML serialized object graph without actually creating the corresponding object graph. With JSON, unless I have missed something, you will need to create the object graph to navigate the structure.
I am not 100% sure but it would seem that Linq would acutally require the graph to be created in memory anyway. If that is true then the disadvantage disappears as a factor.
What I have is a serialized object (given to me serialized from another language). I would like to generate c# code for this and use it in JSON.Net or similar. I have just started looking at JSON.Net capabilities. However, thought it may be interesting to ask it here in parallel.
I have found 2 great options:
json2csharp works well, which is a lightweight website giving .NET code that can be copy-pasted.
JsonCSharpClassGenerator is an executable that creates actual files in a subfolder of your choosing. So it's better for bulk .NET class generation from a large JSON string.
Based on the list here, there are several BSON implementations for C#:
http://bsonspec.org/#/implementation
One example is JSON.
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.
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.
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/