simple.stringify.js 12 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. var jsonBlobL1AsString = "{\"a\":1, \"b\":2, \"c\":\"\", \"d\":\"\\\"\"}"
  6. var jsonBlobL2AsString = "{\"a\":{\"aa\":10, \"ab\":11}, \"b\":{\"ba\":\"this is a test!\", \"bb\":\"a\"}}"
  7. var jsonLongTransformedBlobL2AsString = "{\"a\":{\"aa\":10, \"ab\":11}, \"b\":{\"ba\":\"this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!\\n\", \"bb\":\"a\"}}"
  8. var jsonLongDirectBlobL2AsString = "{\"a\":{\"a\\ta\":10, \"ab\":11}, \"b\":{\"ba\":\"this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!this is a very long string test!\", \"bb\":\"a\"}}"
  9. TraverseAndPrint("level 1:", jsonBlobL1AsString, false);
  10. TraverseAndPrint("level 1:", jsonBlobL2AsString, false);
  11. TraverseAndPrint("level 1+:", jsonLongDirectBlobL2AsString, true);
  12. TraverseAndPrint("level 1+:", jsonLongTransformedBlobL2AsString, true);
  13. TraverseAndPrint("level 1:", jsonBlobL2AsString, false);
  14. function TraverseAndPrint(msg, str, doRecurse) {
  15. WScript.Echo("---------------");
  16. WScript.Echo(msg);
  17. WScript.Echo(str);
  18. var json = JSON.parse(str);
  19. str = "foo";
  20. WScript.Echo("JSON.Parse result - " + json);
  21. TraverseJSONObject(msg, 1, json, doRecurse);
  22. WScript.Echo("---------------");
  23. WScript.Echo("stringify: " + JSON.stringify(json));
  24. WScript.Echo("---------------");
  25. }
  26. function TraverseJSONObject(msg, level, o, doRecurse) {
  27. doRecurse = doRecurse || false;
  28. var sp = "";
  29. for(var i=1; i<level; i++) {
  30. sp += " ";
  31. }
  32. for(var l in o) {
  33. WScript.Echo(msg + sp + l + ": " + o[l]);
  34. if (doRecurse) {
  35. TraverseJSONObject(msg, level+1, o[l]);
  36. }
  37. }
  38. }