1

I want to ignore some properties from my Object during run time. The properties are decorated with data member attribute (Without data member attribute excludepropertyreferencces is working fine). Can you please provide some insight? Thanks

Question : HOW TO EXCLUDE PROPERTIES AT RUN TIME, IF THEY ARE DECORATE WITH DATAMEMBER ATTRIBUTE ? ServiceStack , ExcludePropertyReferences

    var model = new Model {
        FirstName = "First Name",
        LastName = "Last Name",
        Children = new List<ModelChild>{
            new ModelChild { ChildFirstName = "ChildFirstName 1", ChildLastName = "ChildLastName 1" },
            new ModelChild { ChildFirstName = "ChildFirstName 2", ChildLastName = "ChildLastName 2" }
        }
    };

    var model1 = new Model1 {
        FirstName = "First Name",
        LastName = "Last Name",
        Children = new List<Model1Child>{
            new Model1Child { ChildFirstName = "ChildFirstName 1", ChildLastName = "ChildLastName 1" },
            new Model1Child { ChildFirstName = "ChildFirstName 2", ChildLastName = "ChildLastName 2" }
        }
    };

    Console.WriteLine("Properties won't get ignored because the Model is decorated with Serialization Attributes");

    using(MemoryStream stream = new MemoryStream())
    using (var jsConfig = JsConfig.BeginScope()) {
        jsConfig.ExcludeTypeInfo = true;
        jsConfig.ExcludePropertyReferences = new [] { "Model.LastName", "ModelChild.ChildLastName" }.ToArray();
        JsonSerializer.SerializeToStream(model, model.GetType(), stream);
        LINQPad.Extensions.Dump(System.Text.Encoding.Default.GetString(stream.ToArray()));
    }

    Console.WriteLine();
    Console.WriteLine();

    Console.WriteLine("Properties will get ignored because the Model is not decorated with Serialization Attributes");
    using(MemoryStream stream = new MemoryStream())
    using (var jsConfig = JsConfig.BeginScope()) {
        jsConfig.ExcludeTypeInfo = true;
        jsConfig.ExcludePropertyReferences = new [] { "Model1.LastName", "Model1Child.ChildLastName" }.ToArray();
        JsonSerializer.SerializeToStream(model1, model1.GetType(), stream);
        LINQPad.Extensions.Dump(System.Text.Encoding.Default.GetString(stream.ToArray()));

    }




// Define other methods and classes here
[DataContract()]
public class Model {

    [DataMember(Name = "first_name",EmitDefaultValue = false )]   
    public string FirstName { get; set; }
    [DataMember(Name = "last_name")]
    public string LastName { get; set; }
    [DataMember(Name = "collections")]
    public List<ModelChild> Children { get; set; }
}

[DataContract()]
public class ModelChild {
    [DataMember(Name = "child_first_name")]
    public string ChildFirstName { get; set; }
    [DataMember(Name = "child_last_name")]
    public string ChildLastName { get; set; }
}

public class Model1 {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Model1Child> Children { get; set; }
}

public class Model1Child {
    public string ChildFirstName { get; set; }
    public string ChildLastName { get; set; }
}
  • Can you try adding IgnoreDataMemberAttribute? – Ufuk Hacıoğulları Oct 22 '14 at 15:00
  • Thanks for the response. My properties are decorated with DataMember attribute. I want to serialize the fields based on my query string. Hence I want to perform this at run time – Adhrit Talluri Oct 22 '14 at 15:08
  • If there is no datamember decoration jsConfig.ExcludePropertyReferences = new [] { property.Name} is working fine . Looking help how to exclude the property dynamically if they are decorated with datamember. Thanks n advance – Adhrit Talluri Oct 22 '14 at 15:10
  • What ServiceStack version are you using? Is this only about JSON serialization? – Ufuk Hacıoğulları Oct 22 '14 at 15:10
  • Thanks for your response . I am using Version 3.9.71.0 . Yes want to serialize only required fields based on query string . JsonSerializer.ExcludePropertyReferences = new string[] {}; JsonSerializer.SerializeToStream(objectValue, type, writeStream); – Adhrit Talluri Oct 22 '14 at 15:26
  • Is the required field info inside the type? Can you share some of the code so that we can get a better idea. – Ufuk Hacıoğulları Oct 22 '14 at 15:34
  • Is the required field info inside the type? Can you share some of the code so that we can get a better idea. – Yes the required fields are inside the code.. Please find the below code and add service stack reference . – Adhrit Talluri Oct 22 '14 at 15:40
  • I have updated original post with code – Adhrit Talluri Oct 22 '14 at 15:44
  • See this answer on different ways to ignore properties: http://stackoverflow.com/a/14859968/85785 – mythz Oct 22 '14 at 20:05
  • no i have seen this, it will not address as our properties are decorated with data member attribute and run time we need to specify properties to serialize based on the query string. – Adhrit Talluri Oct 23 '14 at 02:28

0 Answers0