Serializing a hierarchy of plain C# objects to a JSON array
I have a class structure in C#, similar to the following:
[DataContract]
class Data
{
[DataMember] public List<Hotel> Hotels { get; set; }
// etc...
}
[DataContract]
class Hotel
{
[DataMember] public int HotelID { get; set; }
[DataMember] public string HotelName { get; set; }
// etc...
}
I've been serializing this to JSON, using the 'DataContractJsonSerializer'.
However, because JSON includes all the property names, this makes for a lot of redundant text.
(You might say gZip alleviates this, but I believe gZip compresses only a portion of the output, like the first n kilobytes, so in this case, it's not really going to help.)
So what I'd prefer to do is spit out the data in the format of a Javascript array, like this:
[[1, "firstHotel"], [2, "secondHotel"], [3, "thirdHotel"], ...]
Is there any way of customizing the JSON serializati开发者_如何学Con to do it this way? Or should I just manually write my own serializer?
What I was attempting isn't necessary, since gzip compression will take care of the entire JSON feed, removing any redundancy.
It looks like protobuf-net can handle DataContract-annotated classes. It uses the binary Protocol Buffers format, which is smaller and faster to serialize/deserialize than JSON. You can still dump JSON in places where you need it to be human-readable (debugging, etc.)
Even with gzip compression, the Protocol Buffers format will be smaller than JSON, though the difference in size depends on what your data looks like.
精彩评论