Polymorphic problem @class in first place SOLVED in JavaScript with angular

11 views
Skip to first unread message

Jorge Machado

unread,
Jun 21, 2017, 3:36:24 AM6/21/17
to Genson user group
Dear all I developed a Javascript Method to reorder the fields and let @class allays in first place
in nested JSON objects



var BacoJS = (function () {

    function orderKeys(obj, expected) {
        var keys = Object.keys(obj).sort(function keyOrder(k1, k2)
        {
            if(k1 == "@class")
                return -1;
            else if(k2 == "@class")
                return 1
            else
                return 0;
            //if (k1 < k2) return -1;
            //else if (k1 > k2) return +1;
            //else return 0;
        });

        var i, after = {};
        for (i = 0; i < keys.length; i++) {
            after[keys[i]] = obj[keys[i]];
            delete obj[keys[i]];
        }

        for (i = 0; i < keys.length; i++) {
            obj[keys[i]] = after[keys[i]];
        }
        return obj;
    }
    function orderKeysRecur(obj, expected)
    {
        if(typeof obj == "object")
            orderKeys(obj, expected);
        for (var k in obj)
        {
            if (typeof obj[k] == "object" && obj[k] !== null)
                orderKeysRecur(obj[k]);
        }
    }

    return {
        stringifyOrdered: function (jsonObj) {
            orderKeysRecur(jsonObj)
            //return JSON.stringify(jsonObj);  // OR
            return angular.toJson(jsonObj);
        }
    };
})();

//does not affect angular JS lib
//you can use-it with angular
var orderedExample = BacoJS.stringifyOrdered(exampleBacoJs);
console.log(orderedExample);


Eugen Cepoi

unread,
Jun 21, 2017, 11:49:15 PM6/21/17
to gen...@googlegroups.com
Hey, thanks for sharing this!

--
You received this message because you are subscribed to the Google Groups "Genson user group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to genson+unsubscribe@googlegroups.com.
To post to this group, send email to gen...@googlegroups.com.
Visit this group at https://groups.google.com/group/genson.
To view this discussion on the web visit https://groups.google.com/d/msgid/genson/6f6bd5d3-56d0-430b-b2ef-3b0f3b27e89b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jorge Machado

unread,
Jun 22, 2017, 2:52:26 AM6/22/17
to Genson user group
//WAS MISSING THE EXAMPLE

var exampleBacoJs =
{
    "xpto" : 1,
    "@class" : "pt.estgp.estgweb.domain.XptoSchool",
    "zgt" : {
        "zgt1" : 1,
        "zgt2" : 2,
        "@class" : "pt.estgp.estgweb.domain.ZgtDepartment"
    }

//does not affect angular JS lib
//you can use-it with angular
var orderedExample = BacoJS.stringifyOrdered(exampleBacoJs);
console.log(orderedExample);


RESULT:
>>>>>>>>CONSOLE>>>>>>>>
{
  "@class" : "pt.estgp.estgweb.domain.XptoSchool",  
  "xpto" : 1,
   "zgt" : {
        "@class" : "pt.estgp.estgweb.domain.ZgtDepartment"
        "zgt1" : 1,
        "zgt2" : 2,
    }
}  
Reply all
Reply to author
Forward
0 new messages