1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H 17 #define ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H 18 19 #include "ecmascript/js_tagged_value.h" 20 #include "ecmascript/base/json_helper.h" 21 #include "ecmascript/js_handle.h" 22 #include "ecmascript/linked_hash_table.h" 23 #include "ecmascript/object_factory.h" 24 #include "ecmascript/global_env.h" 25 #include "ecmascript/mem/c_containers.h" 26 27 namespace panda::ecmascript::base { 28 class JsonStringifier { 29 using TransformType = base::JsonHelper::TransformType; 30 public: 31 JsonStringifier() = default; 32 33 explicit JsonStringifier(JSThread *thread, TransformType transformType = TransformType::NORMAL) thread_(thread)34 : thread_(thread), transformType_(transformType) {} 35 36 ~JsonStringifier() = default; 37 NO_COPY_SEMANTIC(JsonStringifier); 38 NO_MOVE_SEMANTIC(JsonStringifier); 39 40 JSHandle<JSTaggedValue> Stringify(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer, 41 const JSHandle<JSTaggedValue> &gap); 42 43 private: 44 void AddDeduplicateProp(const JSHandle<JSTaggedValue> &property); 45 46 JSTaggedValue SerializeJSONProperty(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 47 JSTaggedValue GetSerializeValue(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &key, 48 const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 49 void SerializeObjectKey(const JSHandle<JSTaggedValue> &key, bool hasContent); 50 51 bool SerializeJSONObject(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 52 53 bool SerializeJSONSharedMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 54 55 bool SerializeJSONSharedSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 56 57 bool SerializeJSONMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 58 59 bool SerializeJSONSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 60 61 bool SerializeJSONHashMap(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 62 63 bool SerializeJSONHashSet(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 64 65 bool SerializeLinkedHashMap(const JSHandle<LinkedHashMap> &hashMap, const JSHandle<JSTaggedValue> &replacer); 66 67 bool SerializeLinkedHashSet(const JSHandle<LinkedHashSet> &hashSet, const JSHandle<JSTaggedValue> &replacer); 68 69 bool SerializeJSArray(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 70 71 bool SerializeJSProxy(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &replacer); 72 73 void SerializePrimitiveRef(const JSHandle<JSTaggedValue> &primitiveRef); 74 75 bool PushValue(const JSHandle<JSTaggedValue> &value); 76 77 void PopValue(); 78 79 bool CalculateNumberGap(JSTaggedValue gap); 80 81 bool CalculateStringGap(const JSHandle<EcmaString> &primString); 82 bool AppendJsonString(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 83 bool SerializeElements(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 84 bool SerializeKeys(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 85 JSHandle<JSTaggedValue> SerializeHolder(const JSHandle<JSTaggedValue> &object, 86 const JSHandle<JSTaggedValue> &value); 87 bool CheckStackPushSameValue(JSHandle<JSTaggedValue> value); 88 CString gap_; 89 CString result_; 90 CString indent_; 91 JSThread *thread_ {nullptr}; 92 ObjectFactory *factory_ {nullptr}; 93 CVector<JSHandle<JSTaggedValue>> stack_; 94 CVector<JSHandle<JSTaggedValue>> propList_; 95 JSMutableHandle<JSTaggedValue> handleKey_ {}; 96 JSMutableHandle<JSTaggedValue> handleValue_ {}; 97 TransformType transformType_ {}; 98 }; 99 } // namespace panda::ecmascript::base 100 #endif // ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H 101