• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_HELPER_H
17 #define ECMASCRIPT_BASE_JSON_HELPER_H
18 
19 #include "ecmascript/js_handle.h"
20 #include "ecmascript/mem/c_string.h"
21 #include "ecmascript/property_attributes.h"
22 
23 namespace panda::ecmascript::base {
24 
25 class JsonHelper {
26 public:
27     enum class TransformType : uint32_t {
28         SENDABLE = 1,
29         NORMAL = 2,
30         BIGINT = 3
31     };
32 
33     enum class BigIntMode : uint8_t {
34         DEFAULT,
35         PARSE_AS_BIGINT,
36         ALWAYS_PARSE_AS_BIGINT
37     };
38 
39     enum class ParseReturnType : uint8_t {
40         OBJECT,
41         MAP
42     };
43 
44     struct ParseOptions {
45         BigIntMode bigIntMode = BigIntMode::DEFAULT;
46         ParseReturnType returnType = ParseReturnType::OBJECT;
47 
48         ParseOptions() = default;
ParseOptionsParseOptions49         ParseOptions(BigIntMode bigIntMode, ParseReturnType returnType)
50         {
51             this->bigIntMode = bigIntMode;
52             this->returnType = returnType;
53         }
54     };
55 
IsTypeSupportBigInt(TransformType type)56     static inline bool IsTypeSupportBigInt(TransformType type)
57     {
58         return type == TransformType::SENDABLE || type == TransformType::BIGINT;
59     }
60 
61     static CString ValueToQuotedString(CString str);
62 
63     static bool IsFastValueToQuotedString(const char *value);
64 
CompareKey(const std::pair<JSHandle<JSTaggedValue>,PropertyAttributes> & a,const std::pair<JSHandle<JSTaggedValue>,PropertyAttributes> & b)65     static inline bool CompareKey(const std::pair<JSHandle<JSTaggedValue>, PropertyAttributes> &a,
66                                   const std::pair<JSHandle<JSTaggedValue>, PropertyAttributes> &b)
67     {
68         return a.second.GetDictionaryOrder() < b.second.GetDictionaryOrder();
69     }
70 
CompareNumber(const JSHandle<JSTaggedValue> & a,const JSHandle<JSTaggedValue> & b)71     static inline bool CompareNumber(const JSHandle<JSTaggedValue> &a, const JSHandle<JSTaggedValue> &b)
72     {
73         return a->GetNumber() < b->GetNumber();
74     }
75 };
76 
77 }  // namespace panda::ecmascript::base
78 
79 #endif  // ECMASCRIPT_BASE_UTF_JSON_H