/* * Copyright (c) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H #define ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H #include "ecmascript/js_tagged_value.h" #include "ecmascript/base/json_helper.h" #include "ecmascript/js_handle.h" #include "ecmascript/object_factory.h" #include "ecmascript/global_env.h" #include "ecmascript/mem/c_containers.h" namespace panda::ecmascript::base { class JsonStringifier { using TransformType = base::JsonHelper::TransformType; public: JsonStringifier() = default; explicit JsonStringifier(JSThread *thread, TransformType transformType = TransformType::NORMAL) : thread_(thread), transformType_(transformType) {} ~JsonStringifier() = default; NO_COPY_SEMANTIC(JsonStringifier); NO_MOVE_SEMANTIC(JsonStringifier); JSHandle Stringify(const JSHandle &value, const JSHandle &replacer, const JSHandle &gap); private: void AddDeduplicateProp(const JSHandle &property); JSTaggedValue SerializeJSONProperty(const JSHandle &value, const JSHandle &replacer); JSTaggedValue GetSerializeValue(const JSHandle &object, const JSHandle &key, const JSHandle &value, const JSHandle &replacer); void SerializeObjectKey(const JSHandle &key, bool hasContent); bool SerializeJSONObject(const JSHandle &value, const JSHandle &replacer); bool SerializeJSArray(const JSHandle &value, const JSHandle &replacer); bool SerializeJSProxy(const JSHandle &object, const JSHandle &replacer); void SerializePrimitiveRef(const JSHandle &primitiveRef); bool PushValue(const JSHandle &value); void PopValue(); bool CalculateNumberGap(JSTaggedValue gap); bool CalculateStringGap(const JSHandle &primString); bool AppendJsonString(const JSHandle &obj, const JSHandle &replacer, bool hasContent); bool SerializeElements(const JSHandle &obj, const JSHandle &replacer, bool hasContent); bool SerializeKeys(const JSHandle &obj, const JSHandle &replacer, bool hasContent); JSHandle SerializeHolder(const JSHandle &object, const JSHandle &value); bool CheckStackPushSameValue(JSHandle value); CString gap_; CString result_; CString indent_; JSThread *thread_ {nullptr}; ObjectFactory *factory_ {nullptr}; CVector> stack_; CVector> propList_; JSMutableHandle handleKey_ {}; JSMutableHandle handleValue_ {}; TransformType transformType_ {}; }; } // namespace panda::ecmascript::base #endif // ECMASCRIPT_BASE_JSON_STRINGIFY_INL_H