@@ -256,7 +256,7 @@ Array.tojson = function(a, indent, nolint, depth) {
256256 indent += "\t" ;
257257
258258 for ( var i = 0 ; i < a . length ; i ++ ) {
259- s += indent + tojson ( a [ i ] , indent , nolint , depth + 1 ) ;
259+ s += indent + tojson ( a [ i ] , indent , nolint , depth + 1 , false ) ;
260260 if ( i < a . length - 1 ) {
261261 s += "," + elementSeparator ;
262262 }
@@ -713,7 +713,7 @@ tojsononeline = function(x) {
713713 return tojson ( x , " " , true ) ;
714714} ;
715715
716- tojson = function ( x , indent , nolint , depth ) {
716+ tojson = function ( x , indent , nolint , depth , sortKeys ) {
717717 if ( x === null )
718718 return "null" ;
719719
@@ -734,7 +734,7 @@ tojson = function(x, indent, nolint, depth) {
734734 case "boolean" :
735735 return "" + x ;
736736 case "object" : {
737- var s = tojsonObject ( x , indent , nolint , depth ) ;
737+ var s = tojsonObject ( x , indent , nolint , depth , sortKeys ) ;
738738 if ( ( nolint == null || nolint == true ) && s . length < 80 &&
739739 ( indent == null || indent . length == 0 ) ) {
740740 s = s . replace ( / [ \t \r \n ] + / gm, " " ) ;
@@ -751,10 +751,13 @@ tojson = function(x, indent, nolint, depth) {
751751} ;
752752tojson . MAX_DEPTH = 100 ;
753753
754- tojsonObject = function ( x , indent , nolint , depth ) {
754+ tojsonObject = function ( x , indent , nolint , depth , sortKeys ) {
755755 if ( typeof depth !== 'number' ) {
756756 depth = 0 ;
757757 }
758+ if ( typeof sortKeys !== 'boolean' ) {
759+ sortKeys = false ;
760+ }
758761 var lineEnding = nolint ? " " : "\n" ;
759762 var tabSpace = nolint ? "" : "\t" ;
760763 if ( typeof x !== "object" ) {
@@ -765,12 +768,12 @@ tojsonObject = function(x, indent, nolint, depth) {
765768 indent = "" ;
766769
767770 if ( typeof ( x . tojson ) == "function" && x . tojson != tojson ) {
768- return x . tojson ( indent , nolint , depth ) ;
771+ return x . tojson ( indent , nolint , depth , sortKeys ) ;
769772 }
770773
771774 if ( x . constructor && typeof ( x . constructor . tojson ) == "function" &&
772775 x . constructor . tojson != tojson ) {
773- return x . constructor . tojson ( x , indent , nolint , depth ) ;
776+ return x . constructor . tojson ( x , indent , nolint , depth , sortKeys ) ;
774777 }
775778
776779 if ( x instanceof Error ) {
@@ -796,8 +799,14 @@ tojsonObject = function(x, indent, nolint, depth) {
796799 var keys = x ;
797800 if ( typeof ( x . _simpleKeys ) == "function" )
798801 keys = x . _simpleKeys ( ) ;
799- var fieldStrings = [ ] ;
802+ var keyNames = [ ] ;
800803 for ( var k in keys ) {
804+ keyNames . push ( k ) ;
805+ }
806+ if ( sortKeys ) keyNames . sort ( ) ;
807+
808+ var fieldStrings = [ ] ;
809+ for ( var k of keyNames ) {
801810 var val = x [ k ] ;
802811
803812 // skip internal DB types to avoid issues with interceptors
@@ -806,7 +815,7 @@ tojsonObject = function(x, indent, nolint, depth) {
806815 if ( typeof DBCollection != 'undefined' && val == DBCollection . prototype )
807816 continue ;
808817
809- fieldStrings . push ( indent + "\"" + k + "\" : " + tojson ( val , indent , nolint , depth + 1 ) ) ;
818+ fieldStrings . push ( indent + "\"" + k + "\" : " + tojson ( val , indent , nolint , depth + 1 , sortKeys ) ) ;
810819 }
811820
812821 if ( fieldStrings . length > 0 ) {
0 commit comments