hey I have a array object something like this
[{
public: "public",
private: "private",
[{
properties: {...
},
instance: {.....
}
}, {...
}, {...
}]
}, {...
}, {....
}]
Here the outer most array contains object of class A, which has a some public props, some private porps and it also contains an array which contains object of class B, which also contains some public and private fields.
so basically this is my hierarchy
array = [A1,A2,A3,A4,....]//object of A
var A = function(){
var Const = function(){
this.public_prop;
this.private_prop;
this.list = [B1,B2,B3,B4]// objects of B
}
//.........
return Const;
}();
var B = function(){
var Const = function(){
this.public_prop;
this.private_prop;
}
//.........
return Const;
}();
Now while stringifying(serialzing) it I want to only include public prop and the arrays in the serialized string.
for example for the above JSON representation i want something like this
[{
public: "public",
[{
properties: {...
}
}, {...
}, {...
}]
}, {...
}, {....
}]
now i can create a function getState() in each class which will only return fields which needs to be stringified, but i cannot seem to find a way to make the native implementation of JSON.stringify call the method before serializing it. Is there some way of accomplishing this?
I refered Hide certain values in output from JSON.stringify(), but it only explains how to exclude simple numeric or string prop in single hierarchy, but how to exclude props in multiple hierarchy?
Note:All my classes follow module-pattern
hey I have a array object something like this
[{
public: "public",
private: "private",
[{
properties: {...
},
instance: {.....
}
}, {...
}, {...
}]
}, {...
}, {....
}]
Here the outer most array contains object of class A, which has a some public props, some private porps and it also contains an array which contains object of class B, which also contains some public and private fields.
so basically this is my hierarchy
array = [A1,A2,A3,A4,....]//object of A
var A = function(){
var Const = function(){
this.public_prop;
this.private_prop;
this.list = [B1,B2,B3,B4]// objects of B
}
//.........
return Const;
}();
var B = function(){
var Const = function(){
this.public_prop;
this.private_prop;
}
//.........
return Const;
}();
Now while stringifying(serialzing) it I want to only include public prop and the arrays in the serialized string.
for example for the above JSON representation i want something like this
[{
public: "public",
[{
properties: {...
}
}, {...
}, {...
}]
}, {...
}, {....
}]
now i can create a function getState() in each class which will only return fields which needs to be stringified, but i cannot seem to find a way to make the native implementation of JSON.stringify call the method before serializing it. Is there some way of accomplishing this?
I refered Hide certain values in output from JSON.stringify(), but it only explains how to exclude simple numeric or string prop in single hierarchy, but how to exclude props in multiple hierarchy?
Note:All my classes follow module-pattern
Share Improve this question edited May 23, 2017 at 11:43 CommunityBot 11 silver badge asked Sep 17, 2012 at 19:11 AnkurAnkur 12.8k7 gold badges39 silver badges68 bronze badges3 Answers
Reset to default 9var result = JSON.stringify(myjson, function(key, val) {
if (key !== "private")
return val;
});
Your object example at the top isn't valid syntax, but to exclude properties named "private"
, this should work.
Assuming that you're writing JavaScript as your question tags suggest (although your example code looks like it's nearly c#!): you need to override the "toJSON" method of the object you're serializing, not "stringify" nor "getState".
Therefore if you have an object "Message" that has public and "private" properties, you need to define a "toJSON" method that only returns the public property, as shown below:
var Message = function() {
this.myPrivateProperty = "Secret message";
this.myPublicProperty = "Message for the public";
this.toJSON = function() {
return {
"public": this.myPublicProperty
};
};
}
alert(JSON.stringify(new Message())); // {"public":"Message for the public"}
Maybe a little late but JSON.stringify
signature accepts a replacer
function/array. The array whitelists and function you can define however you want.
MDN Documentation