Migrating from to (JSON objects)

Jun 1, 2010 at 5:55 PM
Edited Jun 1, 2010 at 6:02 PM

hello there,

I'm just giving my feedback for LINQ 4 JavaScript. I had been using version for awhile and now migrated to I decided to share the changes in code between both versions when dealing with JSON objects, just a simple example:

var ArrayOfProducts= JSON.parser(data); //getting json data from ajax call, using JSON.parser from json2.js (downloadable at www.json.org)
var products = JSON.parse(E.From(ArrayOfProducts).Select("$").ToJSON());


var products = Enumerable.From(ArrayOfProducts).Select("$").ToArray();

I wonder if there's any change of performance here.

Great work guys!



Jun 2, 2010 at 10:40 AM

Thank you for using!

The reason why deleted ToJSON from ver.2 is my json serialize implement is not good.
so, if serialize Enumeble to JSON then recommend use json2.js.

By the way, is ArrayOfProducts already JavaScript Array?
I think your sample's before is deep copy, after is shallow copy.

my guess

// Deep Copy:
var products = JSON.parse(JSON.stringify(ArrayOfProducts));

// Shallow Copy:
var products = Enumerable.From(ArrayOfProducts).ToArray();

.Select(function(x){return x})
= .Select("x=>x").ToArray()
= .Select("$").ToArray()
= .Select("").ToArray()
= .ToArray()

my json example
// convert Enumerable to JsonArrayString (like's ToJSON)
Enumerable.prototype.ToJSON = function ()
    return JSON.stringify(this.ToArray());

// example

function Cls(str, num)
    this.str = str;
    this.num = num;

var jsonString = Enumerable.Range(1, 3)
    .Select(function (i) { return new Cls("Test" + i, i) })

// [{"str":"Test1","num":1},{"str":"Test2","num":2},{"str":"Test3","num":3}]

That reminds me, IE8, Firefox, Chrome etc... support Native JSON.
I'll add this "Enumerable.prototype.ToJSON" to next release.

Thank you very much!