JSON.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #pragma once
  6. #include <wchar.h>
  7. namespace JSON
  8. {
  9. class JSONStack;
  10. class JSONParser;
  11. class EntryInfo
  12. {
  13. public:
  14. static Js::FunctionInfo Stringify;
  15. static Js::FunctionInfo Parse;
  16. };
  17. Js::Var Stringify(Js::RecyclableObject* function, Js::CallInfo callInfo, ...);
  18. Js::Var Parse(Js::RecyclableObject* function, Js::CallInfo callInfo, ...);
  19. class StringifySession
  20. {
  21. public:
  22. enum ReplacerType
  23. {
  24. ReplacerNone,
  25. ReplacerFunction,
  26. ReplacerArray
  27. };
  28. struct StringTable
  29. {
  30. Js::JavascriptString * propName;
  31. Js::PropertyRecord const * propRecord;
  32. };
  33. StringifySession(Js::ScriptContext* sc)
  34. : scriptContext(sc),
  35. replacerType(ReplacerNone),
  36. gap(NULL),
  37. indent(0),
  38. propertySeparator(NULL)
  39. {
  40. replacer.propertyList.propertyNames = NULL;
  41. replacer.propertyList.length = 0;
  42. };
  43. Js::Var Stringify(){};
  44. // Init operation is split in three functions
  45. void InitReplacer(Js::RecyclableObject* f)
  46. {
  47. replacerType = ReplacerFunction;
  48. replacer.ReplacerFunction = f;
  49. }
  50. void InitReplacer(StringTable *nameTable, uint len)
  51. {
  52. replacerType = ReplacerArray;
  53. replacer.propertyList.propertyNames = nameTable;
  54. replacer.propertyList.length = len;
  55. }
  56. void CompleteInit(Js::Var space, ArenaAllocator* alloc);
  57. Js::Var Str(Js::JavascriptString* key, Js::PropertyId keyId, Js::Var holder);
  58. Js::Var Str(uint32 index, Js::Var holder);
  59. private:
  60. Js::JavascriptString* Quote(Js::JavascriptString* value);
  61. Js::Var StringifyObject(Js::Var value);
  62. Js::Var StringifyArray(Js::Var value);
  63. Js::JavascriptString* GetArrayElementString(uint32 index, Js::Var arrayVar);
  64. Js::JavascriptString* GetPropertySeparator();
  65. Js::JavascriptString* GetIndentString(uint count);
  66. Js::JavascriptString* GetMemberSeparator(Js::JavascriptString* indentString);
  67. void StringifyMemberObject( Js::JavascriptString* propertyName, Js::PropertyId id, Js::Var value, Js::ConcatStringBuilder* result,
  68. Js::JavascriptString* &indentString, Js::JavascriptString* &memberSeparator, bool &isFirstMember, bool &isEmpty );
  69. uint32 GetPropertyCount(Js::RecyclableObject* object, Js::JavascriptEnumerator* enumerator);
  70. uint32 GetPropertyCount(Js::RecyclableObject* object, Js::JavascriptEnumerator* enumerator, bool* isPresise);
  71. JSONStack *objectStack;
  72. Js::ScriptContext* scriptContext;
  73. ReplacerType replacerType;
  74. union
  75. {
  76. Js::RecyclableObject* ReplacerFunction;
  77. struct PropertyList
  78. {
  79. StringTable *propertyNames;
  80. uint length;
  81. } propertyList;
  82. } replacer;
  83. Js::JavascriptString* gap;
  84. uint indent;
  85. Js::JavascriptString* propertySeparator; // colon or colon+space
  86. Js::Var StringifySession::StrHelper(Js::JavascriptString* key, Js::Var value, Js::Var holder);
  87. };
  88. } // namespace JSON