Adam
Adam

Reputation: 735

Convert Object to JSON string only Serialized fields

JavaScriptSerializer serializer = new JavaScriptSerializer();
string sJSON =  serializer.Serialize(pt);

This works fine except that it also includes fields that are set as [NonSerialized] Is there a way to exclude those fields?

Upvotes: 4

Views: 423

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

I think you are looking for the ScriptIgnoreAttribute:

public class Data {

    [ScriptIgnore]
    public string Ignore;

    public string DoNotIgnore;
}

Upvotes: 3

saj
saj

Reputation: 4796

[ScriptIgnore()] is what you want

the [NonSerialized()] tag works only for binary serilaization, your example is one of Java script serialization

Upvotes: 3

Related Questions