3

I have a simple object:

[DataContract]
public class MyClass
{
    [DataMember(Name = "MyClassNo")]
    public int MyClassNo { get; set; }
    [DataMember(Name = "MyName")]
    public string MyName { get; set; }
 }

When I serialize it over my web service I get

[
  {"MyClassNo": 1, "MyName": "Test1"},
  {"MyClassNo": 2, "MyName": "Test2"}
]

But what I want is the data without the property names included:

[
  {1, "Test1"},
  {2, "Test2"}
]

How do I achieve this?

*Edit - The Code I use to serialize is:

var myObj = MyOpenSQlConnection.Query<MyClass>(@"select MyClassNo, MyName from MyTable");
return myObj.ToList<MyClass>();

Note that I'm using Dapper-dot-net to map the sql results to my object

3
  • Do you have a code of serializer which service is using? It seems a JSON format
    – sll
    Jul 22, 2011 at 11:12
  • 1
    {1, "Test1"} this is invalid JSON
    – Andrey
    Jul 22, 2011 at 11:18
  • 2
    The choice of dapper is excellent (biased ;p), but shouldn't affect serialization at all in this case. Jul 22, 2011 at 11:45

3 Answers 3

0

It is impossible to do this without writing your own serializer that serializes to something other than a property hash. Hashes are sequences of key-value fields and therefore must have unique keys for each field.

If you tried blank values for each key, as in:

{"": "apple", "": "banana", "": "coconut"}

then what you really have is just:

{"": "coconut"}

since each key will overwrite the one before it.

A better question is probably, "why do you want to serialize without the property names?" Especially in web-based environments, you're going to want to know which value corresponds to which field.

1
  • It is looking like I need to write my own serializer, I was hoping there would be one out of the box! In answer to your second question: I really don't want to start a bun fight about this but I want to transmit as few bytes as possible while not compressing the data.
    – Mattl
    Jul 22, 2011 at 14:54
0

you can specify to all members this attibute: [DataMember(Name = "REMOVEME")] and then replace it by empty string :)

1
  • Unfortunately that does not work, When I do that I get a Error 324 (net::ERR_EMPTY_RESPONSE): : The server closed the connection without sending any data.
    – Mattl
    Jul 22, 2011 at 11:21
0

If you can guarentee that MyClassNo is always distinct then you could return a Dictionary<int, string> instead if a List<MyClass> which may be more along the lines of what you are after.

1
  • OK my example was simplified, some of my object have 60+ properties so I'm not sure I could use a dictionary.
    – Mattl
    Jul 22, 2011 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.