JSONStringifier.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. namespace Js
  7. {
  8. struct JSONObjectStack
  9. {
  10. RecyclableObject* object;
  11. JSONObjectStack* next;
  12. bool Has(_In_ RecyclableObject* object)
  13. {
  14. JSONObjectStack* stack = this;
  15. while (stack != nullptr)
  16. {
  17. if (stack->object == object)
  18. {
  19. return true;
  20. }
  21. stack = stack->next;
  22. }
  23. return false;
  24. }
  25. };
  26. class JSONStringifier
  27. {
  28. private:
  29. // Spec defined limit on the gap length
  30. static const charcount_t MaxGapLength;
  31. struct PropertyListElement
  32. {
  33. Field(PropertyRecord const*) propertyRecord;
  34. Field(JavascriptString*) propertyName;
  35. PropertyListElement() {}
  36. PropertyListElement(const PropertyListElement& other)
  37. : propertyRecord(other.propertyRecord), propertyName(other.propertyName)
  38. {}
  39. };
  40. typedef SList<PropertyListElement, Recycler> PropertyList;
  41. ScriptContext* scriptContext;
  42. RecyclableObject* replacerFunction;
  43. PropertyList* propertyList;
  44. charcount_t totalStringLength;
  45. charcount_t indentLength;
  46. charcount_t gapLength;
  47. char16* gap;
  48. Var TryConvertPrimitiveObject(_In_ RecyclableObject* value);
  49. Var ToJSON(_In_ JavascriptString* key, _In_ RecyclableObject* valueObject);
  50. Var CallReplacerFunction(_In_opt_ RecyclableObject* holder, _In_ JavascriptString* key, _In_ Var value);
  51. _Ret_notnull_ Var ReadValue(_In_ JavascriptString* key, _In_opt_ const PropertyRecord* propertyRecord, _In_ RecyclableObject* holder);
  52. uint32 ReadArrayLength(_In_ RecyclableObject* value);
  53. JSONArray* ReadArray(_In_ RecyclableObject* arr, _In_ JSONObjectStack* objectStack);
  54. void AppendObjectElement(
  55. _In_ JavascriptString* propertyName,
  56. _In_ JSONObject* jsonObject,
  57. _In_ JSONObjectProperty* prop);
  58. void ReadObjectElement(
  59. _In_ JavascriptString* propertyName,
  60. _In_ uint32 numericIndex,
  61. _In_ RecyclableObject* obj,
  62. _In_ JSONObject* jsonObject,
  63. _In_ JSONObjectStack* objectStack);
  64. void ReadObjectElement(
  65. _In_ JavascriptString* propertyName,
  66. _In_opt_ PropertyRecord const* propertyRecord,
  67. _In_ RecyclableObject* obj,
  68. _In_ JSONObject* jsonObject,
  69. _In_ JSONObjectStack* objectStack);
  70. void ReadArrayElement(
  71. uint32 index,
  72. _In_ RecyclableObject* arr,
  73. _Out_ JSONProperty* prop,
  74. _In_ JSONObjectStack* objectStack);
  75. void CalculateStringifiedLength(uint32 propertyCount, charcount_t stepbackLength);
  76. void ReadProxy(_In_ JavascriptProxy* proxyObject, _In_ JSONObject* jsonObject, _In_ JSONObjectStack* stack);
  77. JSONObject* ReadObject(_In_ RecyclableObject* obj, _In_ JSONObjectStack* objectStack);
  78. void SetNullProperty(_Out_ JSONProperty* prop);
  79. void SetNumericProperty(double value, _In_ Var valueVar, _Out_ JSONProperty* prop);
  80. static charcount_t CalculateStringElementLength(_In_ JavascriptString* str);
  81. void ReadData(_In_ RecyclableObject* valueObj, _Out_ JSONProperty* prop);
  82. void ReadData(_In_ Var value, _Out_ JSONProperty* prop);
  83. void SetStringGap(_In_ JavascriptString* spaceString);
  84. void SetNumericGap(charcount_t spaceCount);
  85. void AddToPropertyList(_In_ Var item, _Inout_ BVSparse<Recycler>* propertyBV);
  86. public:
  87. JSONStringifier(_In_ ScriptContext* scriptContext);
  88. void ReadSpace(_In_opt_ Var space);
  89. void ReadReplacer(_In_opt_ Var replacer);
  90. void ReadProperty(
  91. _In_ JavascriptString* key,
  92. _In_opt_ RecyclableObject* holder,
  93. _Out_ JSONProperty* prop,
  94. _In_ Var value,
  95. _In_ JSONObjectStack* objectStack);
  96. const char16* GetGap() const { return this->gap; };
  97. charcount_t GetGapLength() const { return this->gapLength; }
  98. bool HasReplacerFunction() const { return this->replacerFunction != nullptr; }
  99. bool HasComplexGap() const;
  100. static LazyJSONString* Stringify(_In_ ScriptContext* scriptContext, _In_ Var value, _In_opt_ Var replacer, _In_opt_ Var space);
  101. }; // class JSONStringifier
  102. } //namespace Js