diff options
| author | Frank LENORMAND <lenormf@gmail.com> | 2019-11-13 09:54:17 +0100 |
|---|---|---|
| committer | Frank LENORMAND <lenormf@gmail.com> | 2019-11-17 09:27:46 +0100 |
| commit | 7cdbe1d3d24c1cc13bd7cbc3fe252f1e88747ffb (patch) | |
| tree | 3d1bc2cb81cb4c9623bd18a0dcc9fc607fa8db53 /src/json.hh | |
| parent | a7d3976a1002a80c4e5a17c989921025ded89450 (diff) | |
src: Move JSON parsing code to its own file
The `json_ui.cc` file contained both data-parsing and UI-related
code. This commit moves the JSON parsing code to its own `json.cc`
file, to separate concerns, make compilation faster when changes are
made to either UI or parsing code, and make the parsing code more
accessible to fuzzers.
The signature of the following function:
```
auto parse_json(StringView json);
```
was changed to:
```
JsonResult parse_json(StringView json);
```
to avoid `auto` deduction issues at compile-time.
Diffstat (limited to 'src/json.hh')
| -rw-r--r-- | src/json.hh | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/json.hh b/src/json.hh new file mode 100644 index 00000000..1f76739c --- /dev/null +++ b/src/json.hh @@ -0,0 +1,41 @@ +#ifndef json_hh_INCLUDED +#define json_hh_INCLUDED + +#include "hash_map.hh" +#include "string.hh" +#include "value.hh" + +namespace Kakoune +{ + +using JsonArray = Vector<Value>; +using JsonObject = HashMap<String, Value>; + +template<typename T> +String to_json(ArrayView<const T> array) +{ + return "[" + join(array | transform([](auto&& elem) { return to_json(elem); }), ", ") + "]"; +} + +template<typename T, MemoryDomain D> +String to_json(const Vector<T, D>& vec) { return to_json(ArrayView<const T>{vec}); } + +template<typename K, typename V, MemoryDomain D> +String to_json(const HashMap<K, V, D>& map) +{ + return "{" + join(map | transform([](auto&& i) { return format("{}: {}", to_json(i.key), to_json(i.value)); }), + ',', false) + "}"; +} + +String to_json(int i); +String to_json(bool b); +String to_json(StringView str); + +struct JsonResult { Value value; const char* new_pos; }; + +JsonResult parse_json(const char* pos, const char* end); +JsonResult parse_json(StringView json); + +} + +#endif // json_hh_INCLUDED |
