|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +/*! |
| 20 | + * \file tvm/ffi/json/json.h |
| 21 | + * \brief Minimal lightweight JSON parsing and serialization utilities |
| 22 | + */ |
| 23 | +#ifndef TVM_FFI_EXTRA_JSON_H_ |
| 24 | +#define TVM_FFI_EXTRA_JSON_H_ |
| 25 | + |
| 26 | +#include <tvm/ffi/any.h> |
| 27 | +#include <tvm/ffi/container/array.h> |
| 28 | +#include <tvm/ffi/container/map.h> |
| 29 | +#include <tvm/ffi/extra/base.h> |
| 30 | + |
| 31 | +namespace tvm { |
| 32 | +namespace ffi { |
| 33 | +namespace json { |
| 34 | + |
| 35 | +/*! |
| 36 | + * \brief alias Any as json Value. |
| 37 | + * |
| 38 | + * To keep things lightweight, we simply reuse the ffi::Any system. |
| 39 | + */ |
| 40 | +using Value = Any; |
| 41 | + |
| 42 | +/*! |
| 43 | + * \brief alias Map<Any, Any> as json Object. |
| 44 | + * \note We use Map<Any, Any> instead of Map<String, Any> to avoid |
| 45 | + * the overhead of key checking when doing as conversion, |
| 46 | + * the check will be performed at runtime when we read each key |
| 47 | + */ |
| 48 | +using Object = ffi::Map<Any, Any>; |
| 49 | + |
| 50 | +/*! \brief alias Array<Any> as json Array. */ |
| 51 | +using Array = ffi::Array<Any>; |
| 52 | + |
| 53 | +/*! |
| 54 | + * \brief Parse a JSON string into an Any value. |
| 55 | + * |
| 56 | + * Besides the standard JSON syntax, this function also supports: |
| 57 | + * - Infinity/NaN as javascript syntax |
| 58 | + * - int64 integer value |
| 59 | + * |
| 60 | + * If error_msg is not nullptr, the error message will be written to it |
| 61 | + * and no exception will be thrown when parsing fails. |
| 62 | + * |
| 63 | + * \param json_str The JSON string to parse. |
| 64 | + * \param error_msg The output error message, can be nullptr. |
| 65 | + * |
| 66 | + * \return The parsed Any value. |
| 67 | + */ |
| 68 | +TVM_FFI_EXTRA_CXX_API json::Value Parse(const String& json_str, String* error_msg = nullptr); |
| 69 | + |
| 70 | +/*! |
| 71 | + * \brief Serialize an Any value into a JSON string. |
| 72 | + * |
| 73 | + * \param value The Any value to serialize. |
| 74 | + * \param indent The number of spaces to indent the output. |
| 75 | + * If not specified, the output will be compact. |
| 76 | + * \return The output JSON string. |
| 77 | + */ |
| 78 | +TVM_FFI_EXTRA_CXX_API String Stringify(const json::Value& value, |
| 79 | + Optional<int> indent = std::nullopt); |
| 80 | + |
| 81 | +} // namespace json |
| 82 | +} // namespace ffi |
| 83 | +} // namespace tvm |
| 84 | +#endif // TVM_FFI_EXTRA_JSON_H_ |
0 commit comments