@@ -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 }
@@ -719,7 +719,7 @@ tojsononeline = function(x) {
719719 return tojson ( x , " " , true ) ;
720720} ;
721721
722- tojson = function ( x , indent , nolint , depth ) {
722+ tojson = function ( x , indent , nolint , depth , sortKeys ) {
723723 if ( x === null )
724724 return "null" ;
725725
@@ -740,7 +740,7 @@ tojson = function(x, indent, nolint, depth) {
740740 case "boolean" :
741741 return "" + x ;
742742 case "object" : {
743- var s = tojsonObject ( x , indent , nolint , depth ) ;
743+ var s = tojsonObject ( x , indent , nolint , depth , sortKeys ) ;
744744 if ( ( nolint == null || nolint == true ) && s . length < 80 &&
745745 ( indent == null || indent . length == 0 ) ) {
746746 s = s . replace ( / [ \t \r \n ] + / gm, " " ) ;
@@ -757,10 +757,13 @@ tojson = function(x, indent, nolint, depth) {
757757} ;
758758tojson . MAX_DEPTH = 100 ;
759759
760- tojsonObject = function ( x , indent , nolint , depth ) {
760+ tojsonObject = function ( x , indent , nolint , depth , sortKeys ) {
761761 if ( typeof depth !== 'number' ) {
762762 depth = 0 ;
763763 }
764+ if ( typeof sortKeys !== 'boolean' ) {
765+ sortKeys = false ;
766+ }
764767 var lineEnding = nolint ? " " : "\n" ;
765768 var tabSpace = nolint ? "" : "\t" ;
766769 if ( typeof x !== "object" ) {
@@ -771,12 +774,12 @@ tojsonObject = function(x, indent, nolint, depth) {
771774 indent = "" ;
772775
773776 if ( typeof ( x . tojson ) == "function" && x . tojson != tojson ) {
774- return x . tojson ( indent , nolint , depth ) ;
777+ return x . tojson ( indent , nolint , depth , sortKeys ) ;
775778 }
776779
777780 if ( x . constructor && typeof ( x . constructor . tojson ) == "function" &&
778781 x . constructor . tojson != tojson ) {
779- return x . constructor . tojson ( x , indent , nolint , depth ) ;
782+ return x . constructor . tojson ( x , indent , nolint , depth , sortKeys ) ;
780783 }
781784
782785 if ( x instanceof Error ) {
@@ -802,8 +805,14 @@ tojsonObject = function(x, indent, nolint, depth) {
802805 var keys = x ;
803806 if ( typeof ( x . _simpleKeys ) == "function" )
804807 keys = x . _simpleKeys ( ) ;
805- var fieldStrings = [ ] ;
808+ var keyNames = [ ] ;
806809 for ( var k in keys ) {
810+ keyNames . push ( k ) ;
811+ }
812+ if ( sortKeys ) keyNames . sort ( ) ;
813+
814+ var fieldStrings = [ ] ;
815+ for ( var k of keyNames ) {
807816 var val = x [ k ] ;
808817
809818 // skip internal DB types to avoid issues with interceptors
@@ -812,7 +821,7 @@ tojsonObject = function(x, indent, nolint, depth) {
812821 if ( typeof DBCollection != 'undefined' && val == DBCollection . prototype )
813822 continue ;
814823
815- fieldStrings . push ( indent + "\"" + k + "\" : " + tojson ( val , indent , nolint , depth + 1 ) ) ;
824+ fieldStrings . push ( indent + "\"" + k + "\" : " + tojson ( val , indent , nolint , depth + 1 , sortKeys ) ) ;
816825 }
817826
818827 if ( fieldStrings . length > 0 ) {
0 commit comments