-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Javascript objects defined in .NET don't Stringify correctly #37
Comments
Don't use Simple property methodpublic class MyJsObjectInstance : ObjectInstance
{
public MyJsObjectInstance(ObjectInstance prototype) : base(prototype)
{
// No need to call PopulateFunctions().
MyString = "This is my string";
}
public string MyString
{
get { return this["myString"]; }
set { this["myString"] = value; }
}
} Pros:
Cons:
Accessor property methodpublic class MyJsObjectInstance : ObjectInstance
{
private string myString;
public MyJsObjectInstance(ObjectInstance prototype) : base(prototype)
{
this.PopulateFunctions();
this.myString = "This is my string";
}
[JsProperty(Name = "myString", IsEnumerable = true)]
public string MyString
{
get { return this.myString; }
set { this.myString = value; }
}
} Pros:
Cons:
|
By the way, the behaviour with |
Hi Paul, thanks for the response. I'm afraid that I was only able to get the first method working (simple property method) and showing up in the output of JSON.stringify - the second method does not seem to work despite the IsEnumerable parameter being set. Performing propertyIsEnumerable() on an instance of the object returns true for the first method, but false for the second - so I think the IsEnumerable flag is being ignored for some reason. public class MyJsObjectInstance : ObjectInstance
{
private string mySecondString;
public MyJsObjectInstance(ObjectInstance prototype)
: base(prototype)
{
this.PopulateFunctions();
this.PopulateFields();
}
public MyJsObjectInstance(ObjectInstance prototype, int i)
: base(prototype)
{
this.MyFirstString = "my first string";
this.MySecondString = "my second string";
}
// Works fine
public string MyFirstString
{
get { return (string)this["myFirstString"]; }
set { this["myFirstString"] = value; }
}
// Does not show up in JSON.stringify()
[JSProperty(Name = "mySecondString", IsEnumerable = true)]
public string MySecondString
{
get { return this.mySecondString; }
set { this.mySecondString = value; }
}
} |
Oh, that's because JSON.stringify doesn't serialize properties in the prototype. mySecondString is in the prototype but myFirstString is not; it's set directly on the object. Try this:
I checked, and according to the spec this behaviour (not serializing properties in the prototype) is correct. |
If I define a Javascript object in C# code using the pattern shown here: Building an instance class, and I call JSON.stringify on an instance of that object, none of the expected fields are shown in the output. However, I am still able to access those fields in the same Javascript code (i.e I can use their values, etc). If I set an 'invisible' field from Javascript code and then perform the stringify, it appears in the output as I would expect.
C# class
Javascript
var myJsObject = new MyJsObject(); console.log(JSON.stringify(myJsObject)); myJsObject.myString = "Updated the string"; console.log(JSON.stringify(myJsObject));
Javascript console output
This is my string {} {"myString":"Updated the string"}
The text was updated successfully, but these errors were encountered: