Migrating from 1.3.0.2 to 2.1.0.0 (JSON objects)

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

hello there,

I'm just giving my feedback for LINQ 4 JavaScript. I had been using version 1.3.0.2 for awhile and now migrated to 2.1.0.0. 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)
Before:
var products = JSON.parse(E.From(ArrayOfProducts).Select("$").ToJSON());

After:

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

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

Great work guys!

Fabio

 

Coordinator
Jun 2, 2010 at 9: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 1.3.0.2'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) })
    .ToJSON();

// [{"str":"Test1","num":1},{"str":"Test2","num":2},{"str":"Test3","num":3}]
document.writeln(jsonString);

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

Thank you very much!