TorbenJ
TorbenJ

Reputation: 4582

Only serialize some specific properties of a class?

I ran into a problem with Json.Net.

My application is connected to a database and stores some data in it. Lets assume my application has three different classes: Entity, Group, Organisation

Short look into Entity class:

public class Entity
{
    [JsonIgnore]
    public int ID { get; private set; }
    [JsonProperty]
    public string UID { get; private set; }
    [JsonProperty]
    public Gender EntityGender { get; set; }
    [JsonProperty]
    public string Surname { get; set; }
    [JsonProperty]
    public string Forename { get; set; }
    [JsonProperty]
    public Group EntityGroup { get; set; }
    [JsonProperty]
    public Organisation EntityOrganisation { get; set; }

Organisation and Group also have an ID property.
When serializing an Entity I do not want to fully serialize EntityGroup and EntityOrganisation but only their IDs.

Some quick example how I would like it to look like:

(How it looks now)

{
  "UID": "6c5204356b3a1a33",
  "Surname": "Bar",
  "Forename": "Foo",
  "EntityGroup": {
    "Name": "XGroup",
    "GroupOrganisation": {
      "Name": "FooOrg"
    }
  },
  "EntityOrganisation": null,
  "EntityStation": null
}

(How it should look like)

{
  "UID": "6c5204356b3a1a33",
  "Surname": "Bar",
  "Forename": "Foo",
  "EntityGroup": {
    "ID": 1,
    "GroupOrganisation": {
      "ID": 1
    }
  },
  "EntityOrganisation": null,
  "EntityStation": null
}

Is it possible to setup a JsonParser that only serializes the ID property of Group and Organisation when serializing an Entity?

Upvotes: 1

Views: 3356

Answers (1)

nick_w
nick_w

Reputation: 14938

What about this:

In your Entity class, add the JsonConverter attribute to the EntityGroup class:

[JsonProperty]
[JsonConverter(typeof(EntityGroupConverter))]
public Group EntityGroup { get; set; }

This will use the EntityGroupConverter to serialize the Group. This class is as follows:

class EntityGroupConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(Group);
    }

    public override object ReadJson(JsonReader reader, Type objectType,
        JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override void WriteJson(JsonWriter writer, object value,
        JsonSerializer serializer)
    {
        Group group = value as Group;
        serializer.Serialize(writer, new { ID = group.ID,
            GroupOrganisation = new { ID = group.Organisation.ID } });
    }
}

The key here is using the serializer to write an anonymous object that includes only the desired properties.

Note that ReadJson is left unimplemented - I am assuming that deserialization is not required at this stage.

Edit

As Skadier pointed out, the signature of the ReadJson will look like this in more recent versions of Json.NET (the version used in this question is 3.5.0.0):

public override object ReadJson(JsonReader reader, Type objectType,
        object existingValue, JsonSerializer serializer)

Upvotes: 2

Related Questions