Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm just wondering about "stringify" vs "serialize". To me they're the same thing (though I could be wrong), but in my past experience (mostly with ) I use Serialize() and never use Stringify().

I know I can create a simple alias in Javascript,

// either
JSON.serialize = function(input) {
    return JSON.stringify(input);
};

// or
JSON.serialize = JSON.stringify;

http://jsfiddle.net/HKKUb/

but I'm just wondering about the difference between the two and why stringify was chosen.


for comparison purpose, here's how you serialize XML to a String in C#

public static string SerializeObject<T>(this T toSerialize)
{
    XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
    StringWriter textWriter = new StringWriter();

    xmlSerializer.Serialize(textWriter, toSerialize);
    return textWriter.ToString();
}
share|improve this question
6  
Actually can't you just do JSON.serialize = JSON.stringify? –  Daniel DiPaolo Sep 7 '12 at 15:41
    
why yes you can. –  Chase Florell Sep 7 '12 at 15:42
    
I suppose after re-reading my question, I'm seeing that in my C# example, I'm serializing the XML and THEN converting the serialized object ToString();. There inlies the rub. –  Chase Florell Sep 7 '12 at 16:22
    
I guess for continuity, it would be better (for me) to have a method that looks like this... JSON.serialize(obj).toString(); or jsonObject().toString();... this way it would look much like my C#... but now I'm over complicating it. –  Chase Florell Sep 7 '12 at 16:23

2 Answers 2

up vote 6 down vote accepted

Take a closer look at the two comments you've put onto the question:

I suppose after re-reading my question, I'm seeing that in my C# example, I'm serializing the XML and THEN converting the serialized object ToString();. There inlies the rub.

and

I guess for continuity, it would be better (for me) to have a method that looks like this... JSON.serialize(obj).toString(); or jsonObject().toString();... this way it would look much like my C#... but now I'm over complicating it.

Now remember that in Javascript, an object is a hash (rather, if using Prototype or another framework, it should be qualified as a "special kind of hash" - but the simple form works for this example):

var obj = {
   foo: 1,
   bar: function() { console.log(this.foo); }
}
obj.foo; // 1
obj.bar; // function reference
obj.bar(); // function call
obj['foo']; // 1
obj['bar']; // function reference
obj['bar'](); // function call

The only reason a serialize() might be necessary in Javascript is to cut out the functions, references to other objects, etc.

So, to go back to your C# example - we've just cut out .Serialize() as unnecessary. An object is a hash, it's already serialized, further "serialization" would have to be done manually in your own code anyway. All that leaves you is .ToString().

Does .stringify() make more sense now?

share|improve this answer
    
yes, stringify makes sense, however I'd still rather use .toString() –  Chase Florell Sep 8 '12 at 0:07
1  
toString could mean anything. JSON.stringify is clearly JSON. –  Brandon Apr 11 '13 at 12:08

This is because that JSON notation was specified in 1999 not after 2002 (asp.net is released at that year). so i guess they didn't know about the serialize.

Jokes apart,

Hearing the word serialization, first thing that comes to my mind is like converting the data to bytes, here JSON.stringify makes perfect sense as it converts the object to a string representation not a byte representation.

PS:

@Chase Florell, you can't just add JSON.serialize, as in strict mode, this code may actually fail in some browsers.

as JSON is not your average Object.

share|improve this answer
    
I guess in my (limited) experience, serializing something has always been converting it to some form of string for flat file storage. I get what you're saying though. –  Chase Florell Sep 7 '12 at 15:57
    
I thought it was 2001.. see wikipedia. The name was probably a personal preference of Douglas Crockford. 1999 is the year the 3rd ECMA standard came out, on which JSON is founded. –  Martijn Pieters Sep 7 '12 at 15:58
    
@ChaseFlorell serializing just literally refers to turning an in memory object into a serially contiguous data format so it may be atomically contained, I believe a more common method of doing it would be the bytewise representation, for sending across networks or other such. –  Jimmy Hoffa Sep 7 '12 at 16:13
    
@avinashr, re: your ps: edit... this fiddle doesn't seem to fail. can you explain how "JSON is not your average Object"? I'm not being snarky, just unclear on what you mean. –  Chase Florell Sep 7 '12 at 16:20
    
@ChaseFlorell is correct - with regards to Javascript, JSON is a subset of valid Javascript objects. See my answer for a simple example: All valid JSON is a valid Javascript object/hash, but not all Javascript object/hashes are valid JSON. It's even in the name - JSON stands for "JavaScript Object Notation". –  Izkata Sep 7 '12 at 21:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.