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/object_factory.h" 23#include "ecmascript/global_env.h" 24#include "ecmascript/mem/c_containers.h" 25 26namespace panda::ecmascript::base { 27class JsonStringifier { 28 using TransformType = base::JsonHelper::TransformType; 29public: 30 JsonStringifier() = default; 31 32 explicit JsonStringifier(JSThread *thread, TransformType transformType = TransformType::NORMAL) 33 : thread_(thread), transformType_(transformType) {} 34 35 ~JsonStringifier() = default; 36 NO_COPY_SEMANTIC(JsonStringifier); 37 NO_MOVE_SEMANTIC(JsonStringifier); 38 39 JSHandle<JSTaggedValue> Stringify(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer, 40 const JSHandle<JSTaggedValue> &gap); 41 42private: 43 void AddDeduplicateProp(const JSHandle<JSTaggedValue> &property); 44 45 JSTaggedValue SerializeJSONProperty(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 46 JSTaggedValue GetSerializeValue(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &key, 47 const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 48 void SerializeObjectKey(const JSHandle<JSTaggedValue> &key, bool hasContent); 49 50 bool SerializeJSONObject(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 51 52 bool SerializeJSArray(const JSHandle<JSTaggedValue> &value, const JSHandle<JSTaggedValue> &replacer); 53 bool SerializeJSProxy(const JSHandle<JSTaggedValue> &object, const JSHandle<JSTaggedValue> &replacer); 54 55 void SerializePrimitiveRef(const JSHandle<JSTaggedValue> &primitiveRef); 56 57 bool PushValue(const JSHandle<JSTaggedValue> &value); 58 59 void PopValue(); 60 61 bool CalculateNumberGap(JSTaggedValue gap); 62 63 bool CalculateStringGap(const JSHandle<EcmaString> &primString); 64 bool AppendJsonString(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 65 bool SerializeElements(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 66 bool SerializeKeys(const JSHandle<JSObject> &obj, const JSHandle<JSTaggedValue> &replacer, bool hasContent); 67 JSHandle<JSTaggedValue> SerializeHolder(const JSHandle<JSTaggedValue> &object, 68 const JSHandle<JSTaggedValue> &value); 69 bool CheckStackPushSameValue(JSHandle<JSTaggedValue> value); 70 CString gap_; 71 CString result_; 72 CString indent_; 73 JSThread *thread_ {nullptr}; 74 ObjectFactory *factory_ {nullptr}; 75 CVector<JSHandle<JSTaggedValue>> stack_; 76 CVector<JSHandle<JSTaggedValue>> propList_; 77 JSMutableHandle<JSTaggedValue> handleKey_ {}; 78 JSMutableHandle<JSTaggedValue> handleValue_ {}; 79 TransformType transformType_ {}; 80}; 81} // namespace panda::ecmascript::base 82#endif // ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H 83