0

I am looking for the proper way to have a variable property name on serialization.

Example outputs:

{
    "header": {
        "total": 3,
        "page": 1,
        "pagesize": 30
    },
    "cats": [
    ..
    ]
}

and

{
    "header": {
        "total": 3,
        "page": 1,
        "pagesize": 30
    },
    "dogs": [
    ..
    ]
}

I would like to have the name of the array be variable and assign it somehow somewhere. If I am creating a page of cats I want to have the name "cats" on output, if I am creating a page of dogs I want to have the name "dogs" on output. I am using a single class the represents such a page with a list of things (dogs or cats). Currently I am duplicating that class with the only variant being the name of the array which seems a bit not ok.

T.I.A. a lot

  • Do you have any requirements about how you serialize? Are you serializing to save it to a file or are you serializing for network? I am not entirely certain what the question is. What do you mean by "on output"? – selkathguy Feb 25 '14 at 15:16
  • sorry for any unclarities, this is about using json.net as serializer in a web api context – user3351749 Feb 25 '14 at 15:18

2 Answers2

0

Edit 2:

If you have 2 pages why not just make the array generic and call is something like "data" and bind the Cat and Dog page to the data property.

Header.cs

public class Header
{
 public int Total { get; set; }
 public int Page { get; set; }
 public int PageSize { get; set; }

}

Page.cs

public class Page
{
 public Header Header { get; set; }
 public object[] Data { get; set; }
}
Oliver
  • 35,233
  • 12
  • 66
  • 78
  • sorry to disappoint you but no, it is about extracting the variant Dogs Cats (say I have a list of objects and it is about the name of the array for that list) – user3351749 Feb 25 '14 at 15:21
  • The name of the property is the name that it will be serialized too. – Oliver Feb 25 '14 at 15:23
  • See my edit: [JsonProperty(PropertyName = "FooBar")] – Oliver Feb 25 '14 at 15:25
  • it is there that I have been trying to make the value of the PropertyName conform to the content (so say "Dogs" in the case of dogs and "Cats" in the case of cats) but failed to find the way – user3351749 Feb 25 '14 at 15:31
  • so I am trying to get rid of the repetition of classes and have just one, so CatPage and DogPage would be PageOfThings and it would produce "cats" or "dogs" based on what the things are in the list – user3351749 Feb 25 '14 at 15:34
  • I could assign to PropertyName using reflection but the value assigned did not make it to the final output, there still was what was specified like [JsonProperty(PropertyName = "FooBar")], "FooBar" acted like const – user3351749 Feb 25 '14 at 15:38
  • it makes sense that PropertyName cannot be changed at run-time so forward to another way of achieving the same – user3351749 Feb 25 '14 at 16:06
  • I have edited my answer to use one generic CS class and how you could used them on the Dog/Cat page. – Oliver Feb 26 '14 at 06:52
0

This is how we extracted the variant and fixed it:

public class ListView<T> : Dictionary<string, object> where T : IDomainObjectBase
{
    public ListView(DTOList<T> dto, ICallContext context, string name, IEnumerable<object> items)
    {
        this.Add("header", new ViewListHeader(dto.Total, dto.Page, dto.PageSize, context.LocalPath));
        this.Add(name, items.ToList());
    }
}

so we are using a dictionary now, sweet!