Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hey,

I'm trying to write a protocol parser in C# which would be simple to maintain.
My input is a Json payload, and I'm trying to convert it into a certain Request object in my inheritance-tree according to its identifier (which is obviously commonly named across all requests...).

I'm trying to come up with a way to parse this protocol without having a headache (if its possible).

I would be most grateful if somebody could offer me the lightest Json Deserialization Framework that will be able to support my needs.

My ultimate desires are rather simple.
I need to parse the identifier (obviously the whole object will be parsed already), and then, once I know the type I'm expecting, I could obtain the type (probably using enum attributes...), and reflect the rest of the object onto the type's format.

Are there even such frameworks that could help me accomplish my needs easily? Is there a better solution?

Thank you for reading, Your comments and suggestions are very welcome :)
Posted

1 solution

I would say, this one:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx[^].

If helps you to serialize not just the object, but some arbitrary object graph. Also, it's totally non-intrusive. You only develop data types, with relationships between them (like compositions). Then you just add [DataContract] and [DataMember] attributes to what you need to be parts of the contract. You also have good provisions for backup compatibility, when you data model is evolved in newer versions of software, but the users still use persistent artifacts created by older version of software. And finally, this approach is the easiest to use. You never need to deal with JSON or XML directly, you only care about your data model, a set of pure data types.

Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please see my past answers to similar or related questions:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^],
deseralize a json string array[^].

—SA
 
Share this answer
 
Comments
ShacharK 19-Oct-12 20:39pm    
I will definitely look at it, by the way, is there a way to verify the validity of the sent objects? For debugging purposes, If a field is not found, would I be notified? As it is very important that the entire data is complete...
Sergey Alexandrovich Kryukov 20-Oct-12 0:09am    
Normal mechanism is to set missing values to default, and this is what you should use in most cases, but you can perform your custom validation. You can write validation on each object handling events which will be invoked by the serializer before/after loading/storing.
--SA
Sergey Alexandrovich Kryukov 20-Oct-12 0:10am    
If you feel it's convincing or at least reasonable (of course it is :-), please consider accepting the answer formally (green button).
--SA
ShacharK 20-Oct-12 4:43am    
Well, I'm not totally convinced yet, but I do appreciate your efforts, Thank you very much :)
Sergey Alexandrovich Kryukov 20-Oct-12 22:27pm    
You are welcome. Naturally, it's matter of playing around of code and at least minor prototyping -- always recommended, to see what you really may want...
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900