1. 完成了语音识别的C++业务层封装,测试通过
2. 试着测试了一下LVGL_GIF渲染+音乐播放+语音识别的组合简单优化后,
发现lvgl渲染略显卡顿,语音识别有缓冲区空警告,不过无伤大雅,还需要进一步深度优化。
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
//
|
||||
// Created by misaki on 2025/9/22.
|
||||
//
|
||||
#include "cpp_json.h"
|
||||
@@ -0,0 +1,207 @@
|
||||
//
|
||||
// Created by misaki on 2025/9/22.
|
||||
//
|
||||
/**
|
||||
* CppJson.h
|
||||
* 对cJSON的Cpp封装
|
||||
* Code By Misaki
|
||||
* 2025.9.22
|
||||
*/
|
||||
|
||||
/**
|
||||
* 如何使用:
|
||||
std::string text = R"({"name":"Misaki","age":18,"skills":["C++","RISC-V"]})";
|
||||
auto j = cppjson::Json::parse(text);
|
||||
std::cout << "name = " << j["name"].asString() << '\n';
|
||||
j["skills"].append(cppjson::Json("Go"));
|
||||
std::cout << j.dumpPretty() << '\n';
|
||||
|
||||
or:
|
||||
Json j = Json::object();
|
||||
j.set("name", Json("misaki"))
|
||||
.set("age", Json(24))
|
||||
.set("vip", Json(true))
|
||||
.set("list", Json::array()
|
||||
.append(Json(1))
|
||||
.append(Json("hello")));
|
||||
|
||||
std::cout << j.dumpPretty() << "\n";
|
||||
|
||||
Json j2 = j["list"];
|
||||
for (auto& v : j2) std::cout << v.dump() << " ";
|
||||
std::cout << "\n";
|
||||
*/
|
||||
|
||||
// cpp_json.hpp
|
||||
#pragma once
|
||||
#include <cJSON.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace cppjson {
|
||||
|
||||
//前向声明 + 别名
|
||||
class Json;
|
||||
using Array = std::vector<Json>; // 这里只是别名,不会实例化
|
||||
using Object = std::map<std::string, Json>;
|
||||
|
||||
|
||||
class Json {
|
||||
public:
|
||||
enum Type { Null, Bool, Number, String, ArrayType, ObjectType };
|
||||
|
||||
/*-------- 构造 ------------------------------------------------*/
|
||||
Json() noexcept : ptr_(nullptr), owner_(false) {}
|
||||
explicit Json(std::nullptr_t) noexcept : Json() {}
|
||||
explicit Json(bool v) : ptr_(cJSON_CreateBool(v)), owner_(true) {}
|
||||
explicit Json(int v) : ptr_(cJSON_CreateNumber(v)), owner_(true) {}
|
||||
explicit Json(double v) : ptr_(cJSON_CreateNumber(v)), owner_(true) {}
|
||||
explicit Json(const char* v) : ptr_(cJSON_CreateString(v)), owner_(true) {}
|
||||
explicit Json(const std::string& v):ptr_(cJSON_CreateString(v.c_str())), owner_(true) {}
|
||||
explicit Json(const Array& arr); // 类外实现
|
||||
explicit Json(const Object& obj); // 类外实现
|
||||
/* 接管原始指针,owner=true 会负责 delete */
|
||||
explicit Json(cJSON* raw, bool owner = true) noexcept
|
||||
: ptr_(raw), owner_(owner) {}
|
||||
|
||||
/*-------- 拷贝/移动 -------------------------------------------*/
|
||||
Json(const Json& rhs)
|
||||
: ptr_(rhs.ptr_ ? cJSON_Duplicate(rhs.ptr_, 1) : nullptr), owner_(true) {}
|
||||
Json(Json&& rhs) noexcept
|
||||
: ptr_(rhs.ptr_), owner_(rhs.owner_) { rhs.ptr_ = nullptr; rhs.owner_ = false; }
|
||||
Json& operator=(Json rhs) noexcept { swap(rhs); return *this; }
|
||||
~Json() { reset(); }
|
||||
|
||||
/*-------- 工厂 ------------------------------------------------*/
|
||||
static Json parse(const std::string& text) {
|
||||
cJSON* p = cJSON_Parse(text.c_str());
|
||||
if (!p) throw std::runtime_error("cppjson::parse failed");
|
||||
return Json(p, true);
|
||||
}
|
||||
static Json array() { return Json(cJSON_CreateArray(), true); }
|
||||
static Json object() { return Json(cJSON_CreateObject(), true); }
|
||||
|
||||
/*-------- 序列化 ----------------------------------------------*/
|
||||
[[nodiscard]] std::string dump() const {
|
||||
if (!ptr_) return "null";
|
||||
char* s = cJSON_PrintUnformatted(ptr_);
|
||||
std::string ret(s ? s : "null");
|
||||
if (s) free(s);
|
||||
return ret;
|
||||
}
|
||||
[[nodiscard]] std::string dumpPretty() const {
|
||||
if (!ptr_) return "null";
|
||||
char* s = cJSON_Print(ptr_);
|
||||
std::string ret(s ? s : "null");
|
||||
if (s) free(s);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*-------- 类型查询 --------------------------------------------*/
|
||||
[[nodiscard]] Type type() const noexcept {
|
||||
if (!ptr_) return Null;
|
||||
switch (ptr_->type & 0xFF) {
|
||||
case cJSON_False:
|
||||
case cJSON_True: return Bool;
|
||||
case cJSON_Number:return Number;
|
||||
case cJSON_String:return String;
|
||||
case cJSON_Array: return ArrayType;
|
||||
case cJSON_Object:return ObjectType;
|
||||
default: return Null;
|
||||
}
|
||||
}
|
||||
[[nodiscard]] bool isNull() const noexcept { return type() == Null; }
|
||||
[[nodiscard]] bool isBool() const noexcept { return type() == Bool; }
|
||||
[[nodiscard]] bool isNumber() const noexcept { return type() == Number; }
|
||||
[[nodiscard]] bool isString() const noexcept { return type() == String; }
|
||||
[[nodiscard]] bool isArray() const noexcept { return type() == ArrayType; }
|
||||
[[nodiscard]] bool isObject() const noexcept { return type() == ObjectType; }
|
||||
|
||||
/*-------- 取值 ------------------------------------------------*/
|
||||
[[nodiscard]] bool asBool() const { if (!isBool()) throw std::runtime_error("not bool"); return cJSON_IsTrue(ptr_); }
|
||||
[[nodiscard]] int asInt() const { if (!isNumber()) throw std::runtime_error("not number"); return static_cast<int>(ptr_->valueint); }
|
||||
[[nodiscard]] double asDouble() const { if (!isNumber()) throw std::runtime_error("not number"); return ptr_->valuedouble; }
|
||||
[[nodiscard]] std::string asString() const { if (!isString()) throw std::runtime_error("not string"); return ptr_->valuestring; }
|
||||
|
||||
/*-------- 数组/对象接口 ---------------------------------------*/
|
||||
[[nodiscard]] size_t size() const {
|
||||
if (isArray() || isObject()) return cJSON_GetArraySize(ptr_);
|
||||
return 0;
|
||||
}
|
||||
Json operator[](size_t idx) const {
|
||||
if (!isArray()) throw std::runtime_error("not array");
|
||||
cJSON* it = cJSON_GetArrayItem(ptr_, static_cast<int>(idx));
|
||||
return it ? Json(it, false) : Json();
|
||||
}
|
||||
Json operator[](const std::string& key) const {
|
||||
if (!isObject()) throw std::runtime_error("not object");
|
||||
cJSON* it = cJSON_GetObjectItem(ptr_, key.c_str());
|
||||
return it ? Json(it, false) : Json();
|
||||
}
|
||||
Json& append(const Json& v) {
|
||||
if (!isArray()) throw std::runtime_error("not array");
|
||||
cJSON_AddItemToArray(ptr_, cJSON_Duplicate(v.ptr_, 1));
|
||||
return *this;
|
||||
}
|
||||
Json& set(const std::string& key, const Json& v) {
|
||||
if (!isObject()) throw std::runtime_error("not object");
|
||||
cJSON_AddItemToObject(ptr_, key.c_str(), cJSON_Duplicate(v.ptr_, 1));
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*-------- 迭代器(只读)---------------------------------------*/
|
||||
template <bool IsConst>
|
||||
struct iterator_impl {
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
using value_type = Json;
|
||||
using difference_type = std::ptrdiff_t;
|
||||
using pointer = typename std::conditional<IsConst, const Json, Json>::type*;
|
||||
using reference = typename std::conditional<IsConst, const Json, Json>::type&;
|
||||
|
||||
iterator_impl(cJSON* h, cJSON* c) : head_(h), cur_(c) {}
|
||||
reference operator*() {
|
||||
tmp = std::make_unique<Json>(cur_, false);
|
||||
return *tmp;
|
||||
}
|
||||
pointer operator->() { return &(operator*()); }
|
||||
iterator_impl& operator++() { cur_ = cur_ ? cur_->next : nullptr; return *this; }
|
||||
friend bool operator==(const iterator_impl& a, const iterator_impl& b) { return a.cur_ == b.cur_; }
|
||||
friend bool operator!=(const iterator_impl& a, const iterator_impl& b) { return !(a == b); }
|
||||
private:
|
||||
cJSON *head_, *cur_;
|
||||
std::unique_ptr<Json> tmp; // 改为智能指针
|
||||
};
|
||||
using iterator = iterator_impl<false>;
|
||||
using const_iterator = iterator_impl<true>;
|
||||
iterator begin() { return iterator(ptr_, ptr_ ? ptr_->child : nullptr); }
|
||||
iterator end() { return iterator(ptr_, nullptr); }
|
||||
[[nodiscard]] const_iterator begin() const { return cbegin(); }
|
||||
[[nodiscard]] const_iterator end() const { return cend(); }
|
||||
[[nodiscard]] const_iterator cbegin() const { return const_iterator(ptr_, ptr_ ? ptr_->child : nullptr); }
|
||||
[[nodiscard]] const_iterator cend() const { return const_iterator(ptr_, nullptr); }
|
||||
|
||||
/*-------- 工具 ------------------------------------------------*/
|
||||
void swap(Json& rhs) noexcept { std::swap(ptr_, rhs.ptr_); std::swap(owner_, rhs.owner_); }
|
||||
|
||||
private:
|
||||
cJSON* ptr_ = nullptr;
|
||||
bool owner_ = false;
|
||||
|
||||
void reset() { if (owner_ && ptr_) cJSON_Delete(ptr_); ptr_ = nullptr; owner_ = true; }
|
||||
};
|
||||
|
||||
|
||||
/*================ 类外实现:Array / Object 构造 =================*/
|
||||
inline Json::Json(const Array& arr)
|
||||
: ptr_(cJSON_CreateArray()), owner_(true) {
|
||||
for (const auto& v : arr) append(v);
|
||||
}
|
||||
inline Json::Json(const Object& obj)
|
||||
: ptr_(cJSON_CreateObject()), owner_(true) {
|
||||
for (const auto& kv : obj) set(kv.first, kv.second);
|
||||
}
|
||||
|
||||
} // namespace cppjson
|
||||
Reference in New Issue
Block a user