How to prevent property from being called a second time while using the same object in the "this" clause


Hakan

I have a class structure with several string properties, and I want to add another property to dynamically return the string value of the Jsonsame object .

My problem is that when I use it while thiscreating the Jsonresult , it recursively calls the object again and StackOverflowExceptioneventually crashes .

I tried changing the thisfield New Introducer() { Id = this.Id }but it resulted in the same error.

Although I can get around the problem by identifying the bool IsSerializingparameter and manually bypassing the field a second time , I'm looking for a more appropriate solution.

Is there a command or property to prevent the compiler from calling the property a Serializedsecond time ? Or am I calling the property the wrong way in the first place?

Here is my class:

public class Introducer
{
    public Introducer()
    {
        this.Id = 0;
        this.NameSurname = string.Empty;
        this.EmailAddress = string.Empty;
        this.UserCreated = new User();
    }

    public int Id { get; set; }
    private string _NameSurname;
    public string NameSurname { get { return _NameSurname; } set { _NameSurname = value.Trim(); } }
    private string _EmailAddress;
    public string EmailAddress { get { return _EmailAddress; } set { _EmailAddress = value.Trim(); } }
    public DateTime DateCreated { get; set; }
    public User UserCreated { get; set; }

    public string Serialized
    {
        get
        {
            return JsonConvert.SerializeObject(this, Formatting.Indented);
        }
    }
}
LJ

Here is a tryable example. The idea is to use the JsonIgnore attribute to instruct the serializer not to select that attribute for serialization.

class MyClass
{
    public int Id { get; set; }
    private string _NameSurname;
    public string NameSurname { get { return _NameSurname; } set { _NameSurname = value.Trim(); } }
    private string _EmailAddress;
    public string EmailAddress { get { return _EmailAddress; } set { _EmailAddress = value.Trim(); } }
    public DateTime DateCreated { get; set; }

    [JsonIgnore]
    public string Serialized
    {
        get
        {
            return JsonConvert.SerializeObject(this, Formatting.Indented);
        }
    }
}

Called as:

MyClass obj = new MyClass();
var serialized = obj.Serialized;

edit:

Good practice 1: As mentioned in other answers, a methodcan be used in place of propertythe following:

class MyClass
{
    public int Id { get; set; }
    private string _NameSurname;
    public string NameSurname { get { return _NameSurname; } set { _NameSurname = value.Trim(); } }
    private string _EmailAddress;
    public string EmailAddress { get { return _EmailAddress; } set { _EmailAddress = value.Trim(); } }
    public DateTime DateCreated { get; set; }

    public string GetJSON()
    {
        return JsonConvert.SerializeObject(this, Formatting.Indented);
    }
}

Good practice 2: Follow proper OOD patterns and serialize (or represent) objects in a separate format class.

Related


How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

How to prevent data from being loaded in NgRx a second time?

Irina I have a DOM block: <div (click)="toggle(block)"> <div *ngIf="block.opened"> <div *ngFor="let item of block.items"></div> </div> </div> On click I dispatch action to load data from server and display this data in block: toggle(block: Regist

Prevent the same class from being called on jQuery

winner I actually have a slider to show the menu, this menu slides right to left to the next level and left to right to go back, but when I click back, yes go back, but at the same time to the actual div, below is my code. <div class="total"> <div class="s

Prevent the same class from being called on jQuery

winner I actually have a slider to show the menu, this menu slides right to left to the next level and left to right to go back, but when I click back, yes go back, but at the same time to the actual div, below is my code. <div class="total"> <div class="s

Prevent property from being added to JavaScript object

George I'm basically just wondering if there is some standardized way to prevent methods/properties from being added to JavaScript objects? As a C++ programmer, trying to debug JavaScript code can seem like a headache when you allow dynamically adding properti

How to prevent constructor from being called temporarily

bullet magnet I have a class whose constructor takes some vectors and stores them. struct X { X(std::vector<int> const& ints, std::vector<float> const& floats, std::vector<std::string> const& strings) : ints_{ints} , floats_{floats} , strings_{

How to prevent the constructor from being called in this instance

Dylan Roberts I'm trying to implement a factory pattern where the method returns me the correct object. When calling the factory method, my private constructor is called, which has an effect on the outcome of that class. I put the print statement into the cons

How to prevent constructor from being called temporarily

bullet magnet I have a class whose constructor takes some vectors and stores them. struct X { X(std::vector<int> const& ints, std::vector<float> const& floats, std::vector<std::string> const& strings) : ints_{ints} , floats_{floats} , strings_{

How to prevent the constructor from being called in this instance

Dylan Roberts I'm trying to implement a factory pattern where the method returns me the correct object. When calling the factory method, my private constructor is called, which has an effect on the outcome of that class. I put the print statement into the cons

How to always prevent a method from being called

User 3271300 -(void) parseXML { [self performSelector:@selector(parseXML) withObject:self afterDelay:55.0 ]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://apikeygoeshere.com/data.xml"]]; NSData *response

How to prevent constructor from being called temporarily

bullet magnet I have a class whose constructor takes some vectors and stores them. struct X { X(std::vector<int> const& ints, std::vector<float> const& floats, std::vector<std::string> const& strings) : ints_{ints} , floats_{floats} , strings_{

How to always prevent a method from being called

User 3271300 -(void) parseXML { [self performSelector:@selector(parseXML) withObject:self afterDelay:55.0 ]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://apikeygoeshere.com/data.xml"]]; NSData *response