0

Let's say I got a class like this:

[Version(123)]
public class Test
{

}

How would I add a property "Version": 123 to the generated json ?

I've been playin with a custom ContractResolver but with no success..

protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    {
        var properties = base.CreateProperties(type, memberSerialization);

        var attributes = type.GetCustomAttributes(false);

        VersionAttribute attribute = (VersionAttribute)attributes.FirstOrDefault(att => att is VersionAttribute);
        if (attribute != null)
        {
            JsonProperty versionProperty = new JsonProperty();
            versionProperty.ValueProvider = new ValueProvider(attribute);
            versionProperty.PropertyType = typeof(int);
            versionProperty.PropertyName = "Version";
            versionProperty.DefaultValue = 0;
            properties.Add(versionProperty);
        }

        return properties;
    }
Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • 1
    What prevents you from making the version a property of the class? You have to modify the class anyway, so you might as well just make version a part of the class. – Robert Harvey Sep 04 '18 at 15:04
  • 1
    Seems very similar to [Json.net Add property to every class containing of a certain type](https://stackoverflow.com/q/46549680/3744182). – dbc Sep 04 '18 at 15:21
  • 1
    Does the answer to [Json.net Add property to every class containing of a certain type](https://stackoverflow.com/q/46549680/3744182) also answer your question or do you need more specific help? – dbc Sep 04 '18 at 16:14
  • @dbc this is sufficient ! thanks alo ! – Felix D. Sep 06 '18 at 07:26

0 Answers0