jsonCache.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // Spaces in JSON strings are needed to ensure json cache kicks in
  6. // An example of showing path type divergence a->b, a->b, a->b, a->c, a->b
  7. function test1()
  8. {
  9. var jsonString = " [ {\"a\":1, \"b\":2}, {\"a\":1, \"b\":2}, {\"a\":1, \"b\":2}, {\"a\":1, \"c\":2}, {\"a\":1, \"b\":2} ]"
  10. var json = JSON.parse(jsonString);
  11. TraverseJSONObject("Path type divergence" , 1, json, true);
  12. }
  13. // An example of showing numerical property a->b->1->c, a->b->1->c
  14. function test2()
  15. {
  16. var jsonString = " [ {\"a\":1, \"b\":2, \"1\":3, \"c\":4 }, {\"a\":1, \"b\":2, \"1\":3, \"c\":4 }, {\"a\":1, \"b\":2, \"1\":3, \"c\":4 }, {\"a\":1, \"b\":2, \"1\":3, \"c\":4 }] "
  17. var json = JSON.parse(jsonString);
  18. TraverseJSONObject("Numerical properties" , 1, json, true);
  19. }
  20. // Tests having duplicate property names
  21. function test3()
  22. {
  23. var jsonString = " [ {\"a\":1, \"b\":2, \"c\":3, \"d\":4,\"a\":5, \"a\":6, \"b\":7 }, {\"a\":1, \"b\":2, \"c\":3, \"d\":4,\"a\":5, \"a\":6, \"b\":7 }, {\"a\":1, \"b\":2, \"c\":3, \"d\":4,\"a\":5, \"a\":6, \"b\":7 }, {\"a\":1, \"b\":2, \"c\":3, \"d\":4,\"a\":5, \"a\":6, \"b\":7 }] "
  24. var json = JSON.parse(jsonString);
  25. TraverseJSONObject("Duplicates" , 1, json, true);
  26. }
  27. function TraverseJSONObject(msg, level, o, doRecurse) {
  28. doRecurse = doRecurse || false;
  29. var sp = "";
  30. if(level == 1)
  31. {
  32. WScript.Echo(msg);
  33. }
  34. for(var i=1; i<level; i++) {
  35. sp += " ";
  36. }
  37. for(var l in o) {
  38. WScript.Echo(sp + l + ": " + o[l]);
  39. if (doRecurse) {
  40. TraverseJSONObject(msg, level+1, o[l]);
  41. }
  42. }
  43. }
  44. function RunAll()
  45. {
  46. WScript.Echo("Running test1...");
  47. test1();
  48. WScript.Echo("Running test2...");
  49. test2();
  50. WScript.Echo("Running test3...");
  51. test3();
  52. }
  53. RunAll();