Serialization with Json.NET - c#

I'm building a Silverlight wp7 app in C#. I have objects that I want to convert to and from JSON. I'm using JSON.NET.
Several properties of these objects require a bit of logic to initialize. Is there some way to use a custom converter method? (One such property is a List of strings. The data is given as a single String, and in the constructor the class splits it into a list.)
Also, I'd rather have the properties be read only, but they have to be read-write for conversation (right?). That's kind of a pain.
Or am I stuck doing the conversion manually?

Writing a JsonConverter allows you to manually serialize/deserialize a type. You could write one for a List that will split the string when reading and concatenate it when writing JSON.

If you are calling for the objects through a web service, you can change the encoding of the web service response to return JSON.
http://blog.davebouwman.com/posting-data-to-aspnet-json-services-with-doj

Related

Is it possible to generate C# class by deserialization some protocol?

I need to generate or define new class based on deserialization serialized class. So I want to transfer class definition from server to client to have access to it's properties later.
Is it possible and how?
Proper way to do it would be to either expose a schema definition for your service for clients to consume & generate strongly type class definitions from that or provide a DLL with your DTO contract definitions (class/interface definitions) to the client.
If you chose neither of those approaches (no schema & no dll with interfaces) but still
want to generate a class definition, you can in an improper way generate .cs class definitions, from a sample data of the service (call the services couple of times and intercept the responses or use some http client). However this approach does not guarantee that you will get an accurate or/and complete generation. Basically you can go from:
XML->XSD->C# cs class file (or even XML to C# cs file directly)or JSON->C# class file
And deserializing object to dynamic especially when you don't own both the server & client code is pretty much the worst thing you can do. And this way you didn't transfer you class definition to the client. Deserializng to dynamic objects is actually no desrialization at all as matter of fact, it gives you a dictionary of strings with syntactical sugar to access them as properties at runtime with not compile time support which can be equal to a disaster. In short don't do it unless you own all the code (not that it's a good idea then either but maybe you could get by somehow)
One portable way to transfer the property definitions and the data itself is to use the JSON serializer.
You can deserialize into a dynamic object using JSON.Net
Deserialize json object into dynamic object using Json.net

How can I serialize objects that inherit built-in c# classes?

I'm trying to create a function that will save the current state of my application to a file, and another function to load a saved file. Currently, all the information is contained within a single object, which in turn refers to other objects. I recently heard that C# has some built-in classes that help you serialize and deserialize your objects, so I did a little research and learned about DataContracts, mostly from this page: http://msdn.microsoft.com/en-us/library/ms731073.aspx
Most of it works, except for the classes that implement built-in classes. For example, I have an object that inherits System.Windows.DependencyObject, and when I try to serialize it, it complains that my class inherits a class that does not have the DataContract attribute.
It makes sense to me why that would be a problem. When an object is being deserialized, its constructor is not called. If it inherits something that is not serializable, that might leave it in an invalid state.
I was wondering if this was possible: can I somehow tell the deserializer to call the base class's default constructor before deserializing my object? And then I would have to tell the serializer not to freak out.
Can you create a data transer object that has all the properties you want to store, then populate that object with data from the framework object? Mark it as serialized, fire up the serialization class of your choice - and now you have all the info you need. You just need to re-populate the appropriate class after deserialization.
You may want to look into using a binary serializer or xml serializer instead of a data contract serializer for this one. If you're saving it to a file and don't need the file human-readable binary serialization nearly always works.
See Binary Serialization, and in particular the Basic Serialization topic.
Also take a look at the XmlSerializer Class which will sometimes work where a DataContractSerializer doesn't.

Is there a way to use Json.Net deserialization with immutable classes?

I'm working with an API that uses json. I have some classes that I've created to model the API. To make life easy, my models use public properties, which are in turn used by Json.Net when deserializing the json into objects.
I'd like to make my objects immutable, but I'm running into a problem because if I make my properties read only, I break the deserialization. Is there a way for me to have immutable objects, and use deserialization?
Provide a constructor with parameters that correspond to the properties. The casing of the first letters of the parameters and properties does not need to match.
I think you should be able to use JsonConstructorAttribute. See this question for an example.

Best way to parse JSON string without getting stuck with structure or class

Could you help me to find out the best way to parse JSON string (coming as web service parameter).
using either JavaScriptSerializer or DataContractJsonSerializer based on deserialization is useless for me since the client does't accept to share with me common data structure (class).
Regards
I've used JSON.NET various times in the past - it lets you parse JSON into something a bit like an XML DOM, rather than requiring "real" types. Look at the "LINQ to JSON" part of the documentation.

Possible to serialize a collection that is part of an object

Im trying to store my data in an XML file using the XML Serialization. Question is, if my object has a collection member, can i serialize the collection as well?
This is how my objects work.
class Project{
List<Iteration>
}
class Iteration{
List<Job>
}
class Job{
some other attributes
}
So each project has a list of iterations, and each iteration has a list of projects. So essentially, Each project can have many iterations and each iteration can have many jobs.
If serialization is not possible, can anyone suggest to me another method to store my data?
thanks in advance.
I have used such a type of serialization before. If you have a simple application you can sometimes serialize your whole set of data from one root object (might be an antipattern somehow, but I know I have done it before).
This should work with the default XML serializer in the XML.Serialization namespace, but you need to make sure your classes are marked with the serializable attribute. I'm not sure if it is available on the compact framework though.
Have you considered using Json serialization instead? If you use something like Json.Net (available for Windows Phone 7), you should be able to serialize the entire object graph.

Categories

Resources