rapidjson实现将得到的Value中的值变为string

rapidjson实现将得到的Value中的值变为string,主要应用如下场景

"{\"item_1\":

    {    \"sub_item_1\":\"value_1\",

          \"sub_item_2\":\"value_2\",

           \"sub_item_3\":\"value_3\"

    },

    \"item_2\":\"value_2\"

}"

拿到item_1的值

头文件

#include "rapidjson/document.h"

#include "rapidjson/writer.h"

#include "rapidjson/stringbuffer.h"
string strJsonTest = "{\"item_1\":{\"sub_item_1\":\"value_1\",\"sub_item_2\":\"value_2\",\"sub_item_3\":\"value_3\"},\"item_2\":\"value_2\"}";

Document docTest;
docTest.Parse<0>(strJsonTest.c_str());
if (!docTest.HasParseError())
{
    if (docTest.HasMember("item_1"))
    {
	rapidjson::Value& valObj = docTest["item_1"];
	rapidjson::StringBuffer sbBuf;
	rapidjson::Writer<rapidjson::StringBuffer> jWriter(sbBuf);
	valObj.Accept(jWriter);
	std::string strTemp = std::string(sbBuf.GetString());
	printf( "%s\n" ,strTemp );
	//strTemp的内容为{\"sub_item_1\":\"value_1\",\"sub_item_2\":\"value_2\",\"sub_item_3\":\"value_3\"}
    }
}

封装接口为

std::string JsonToString(const rapidjson::Value& valObj)
{
	rapidjson::StringBuffer sbBuf;
	rapidjson::Writer<rapidjson::StringBuffer> jWriter(sbBuf);
	valObj.Accept(jWriter);
	return std::string(sbBuf.GetString());
}



#include "qt_pch.h" #include "ChannelsAdjustWidget.h" #include <QCheckBox> #include <QLabel> #include <QFile> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QDebug> CChannelCurveWidget::CChannelCurveWidget(ChannelCurveINFO* value, ChannelCurveINFO* backup, QWidget* parent) : QWidget(parent) { if (nullptr != backup) { backupValue_.name_ = backup->name_; for (int i = 0; i < backup->curve_.count(); ++i) { backupValue_.curve_.push_back(backup->curve_.at(i)); } } curve_panel_ = new ColorCurvePanel(this); curve_panel_->setMinimumSize(350, 350); QGridLayout* gridLayout = new QGridLayout; gridLayout->setContentsMargins(0, 0, 0, 0); gridLayout->setHorizontalSpacing(25); int interval = 10; int st = 6; for (int i = 0; i < 25; ++i) { int realIndex = st + i * interval; auto* chkBox = new QCheckBox(QString("%1%").arg(realIndex * 100 / 256), this); chkBox->setProperty("boxIndex", realIndex); chkBox->setProperty(qss_property_dialog_check, true); if (nullptr == value) { chkBox->setEnabled(false); } else { chkBox->setEnabled(true); chkBox->setChecked(true); } listCheckBoxs_ << chkBox; auto* boxValue = new QDoubleSpinBox(this); boxValue->setMinimumSize(70, 35); boxValue->setRange(0.00, 255.00); boxValue->setDecimals(2); boxValue->setSingleStep(0.01); boxValue->setAlignment(Qt::AlignCenter); boxValue->setProperty("boxIndex", realIndex); boxValue->setProperty(qss_property_dialog_spin_box, true); boxValue->setEnabled(chkBox->isEnabled() && chkBox->isChecked()); if (nullptr != value) { boxValue->setValue(value->curve_.at(realIndex) * 100); curve_panel_->setPoint(realIndex, realIndex / 255.0f, value->curve_.at(realIndex)); } listBoxs_ << boxValue; auto* layout = new QHBoxLayout; layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(5); layout->addWidget(chkBox); layout->addWidget(boxValue); auto* widget = new QWidget(this); widget->setLayout(layout); gridLayout->addWidget(widget, i / 5, i % 5); } QHBoxLayout* topLayout = new QHBoxLayout; topLayout->setContentsMargins(0, 0, 0, 0); topLayout->setSpacing(10); topLayout->addWidget(curve_panel_, 1, Qt::AlignCenter); topLayout->addLayout(gridLayout); setLayout(topLayout); if (nullptr != value) { for (int i = 0; i < listCheckBoxs_.count(); ++i) { connect(listCheckBoxs_.at(i), &QCheckBox::clicked, this, [&](bool checked) { doCheckedChanged(checked, sender()); }); } for (int i = 0; i < listBoxs_.count(); ++i) { connect(listBoxs_.at(i), &QDoubleSpinBox::editingFinished, this, [&]() { double value = qobject_cast<QDoubleSpinBox*>(sender())->value(); doValueChanged(value, sender()); }); } } } void CChannelCurveWidget::setCurveColor(const QColor& clr) { curve_panel_->setCurveLinePen(QPen(clr, 2)); curve_panel_->setSlectedPointBursh(QBrush(clr)); curve_panel_->setUnslectedPointBursh(QBrush(clr)); } void CChannelCurveWidget::resetCurve() { if (backupValue_.curve_.isEmpty()) return; valueChanging_ = true; for (int i = 0; i < listCheckBoxs_.count(); ++i) { auto* chkValue = listCheckBoxs_.at(i); chkValue->setChecked(true); chkValue->setEnabled(true); } for (int i = 0; i < listBoxs_.count(); ++i) { auto* boxValue = listBoxs_.at(i); int index = boxValue->property("boxIndex").toInt(); boxValue->setValue(backupValue_.curve_.at(index) * 100); curve_panel_->setPoint(index, index / 255.0f, backupValue_.curve_.at(index)); } curve_panel_->update(); valueChanging_ = false; } ChannelCurveINFO CChannelCurveWidget::curveValue() { ChannelCurveINFO curveInfo; curveInfo.name_ = backupValue_.name_; double* lut = new double[256]; curve_panel_->getPercentLookUpTable(lut, 256); for (int i = 0; i < 256; ++i) { curveInfo.curve_.push_back(lut[i]); } delete[] lut; return curveInfo; } void CChannelCurveWidget::doCheckedChanged(bool checked, QObject* sender) { QDoubleSpinBox* boxValue = nullptr; int index = sender->property("boxIndex").toInt(); for (int i = 0; i < listBoxs_.count(); ++i) { auto* box = listBoxs_.at(i); if (index == box->property("boxIndex").toInt()) { boxValue = box; break; } } if (nullptr == boxValue) return; boxValue->setEnabled(checked); valueChanging_ = true; if (checked) { curve_panel_->setPoint(index, index / 255.0, boxValue->value() / 100.0); } else { curve_panel_->deletePoint(index); } curve_panel_->update(); adjustBoxValues(); valueChanging_ = false; } void CChannelCurveWidget::doValueChanged(double value, QObject* sender) { if (valueChanging_) return; valueChanging_ = true; auto* boxValue = (QDoubleSpinBox*)sender; int index = boxValue->property("boxIndex").toInt(); curve_panel_->setPoint(index, index / 255.0f, value / 100.0); curve_panel_->update(); adjustBoxValues(); valueChanging_ = false; } void CChannelCurveWidget::adjustBoxValues() { double* lut = new double[256]; curve_panel_->getPercentLookUpTable(lut, 256); for (int i = 0; i < listBoxs_.count(); ++i) { auto* boxValue = listBoxs_.at(i); int index = boxValue->property("boxIndex").toInt(); boxValue->setValue(lut[index] * 100.0); } delete[] lut; } CChannelsAdjustWidget::CChannelsAdjustWidget(const QString& jsonPath, QWidget* parent) : QWidget(parent) { ui.setupUi(this); initStyleSheets(); ColorJSonINFO jsonInfo; loadJSonPath(jsonPath, jsonInfo); QList<QColor> curve_colors = { QColor(0, 255, 255), // c QColor(255, 0, 255), // m QColor(255, 255, 0), // y QColor(0, 0, 0) // k }; QList<QColor> mask_colors = { QColor(255, 0, 0), QColor(0, 255, 0), QColor(0, 0, 255), QColor(255, 255, 255) }; for (int i = 0; i < jsonInfo.fileData_.channelCurves_.count(); ++i) { auto* value = (ChannelCurveINFO*)&(jsonInfo.fileData_.channelCurves_.at(i)); ChannelCurveINFO* backup = nullptr; for (int b = 0; b < jsonInfo.fileData_.backupCurves_.count(); ++b) { if (value->name_ == jsonInfo.fileData_.backupCurves_.at(b).name_) { backup = (ChannelCurveINFO*)&(jsonInfo.fileData_.backupCurves_.at(b)); } } if (nullptr == backup) backup = value; auto* curve_widget = new CChannelCurveWidget(value, backup, ui.stackedWidget_); curve_widget->setCurveColor(curve_colors.at(i)); ui.stackedWidget_->addWidget(curve_widget); } auto* defWidget = new CChannelCurveWidget(nullptr, nullptr, this); ui.stackedWidget_->addWidget(defWidget); ui.stackedWidget_->setCurrentWidget(defWidget); QList<QPushButton*> btnChannels = { ui.btnChannel1_, ui.btnChannel2_, ui.btnChannel3_, ui.btnChannel4_ }; for (int i = 0; i < btnChannels.count(); ++i) { QString normal = "QPushButton{border:1px solid %1;background:%1;color:%2}"; QString hover = "QPushButton:hover{border:1px solid %1;}"; QString sheet = normal.arg(curve_colors.at(i).name()).arg(mask_colors.at(i).name()) + hover.arg(mask_colors.at(i).name()); btnChannels.at(i)->setStyleSheet(sheet); btnChannels.at(i)->setCursor(Qt::PointingHandCursor); } initConnections(); QList<QSlider*> sliderGammas = { ui.sliderGammaC_, ui.sliderGammaM_, ui.sliderGammaY_, ui.sliderGammaK_ }; if (jsonInfo.fileData_.singleGammas_.isEmpty()) { QList<QPushButton*> btnGammasAdd = { ui.btnAddGammaC_, ui.btnAddGammaM_, ui.btnAddGammaY_, ui.btnAddGammaK_ }; QList<QPushButton*> btnGammasSub = { ui.btnSubGammaC_, ui.btnSubGammaM_, ui.btnSubGammaY_, ui.btnSubGammaK_ }; QList<QSpinBox*> boxGammas = { ui.boxGammaC_, ui.boxGammaM_, ui.boxGammaY_, ui.boxGammaK_ }; for (int i = 0; i < sliderGammas.count(); ++i) { btnGammasAdd.at(i)->setEnabled(false); sliderGammas.at(i)->setEnabled(false); btnGammasSub.at(i)->setEnabled(false); boxGammas.at(i)->setEnabled(false); } } else { for (int i = 0; i < sliderGammas.count(); ++i) { sliderGammas.at(i)->setValue(jsonInfo.fileData_.singleGammas_.at(i)); } } QList<QSlider*> sliderMaxInks = { ui.sliderMaxInkC_, ui.sliderMaxInkM_, ui.sliderMaxInkY_, ui.sliderMaxInkK_ }; if (jsonInfo.fileData_.adjMaxInks_.isEmpty()) { QList<QPushButton*> btnMaxInksSub = { ui.btnSubMaxInkC_, ui.btnSubMaxInkM_, ui.btnSubMaxInkY_, ui.btnSubMaxInkK_ }; QList<QPushButton*> btnMaxInksAdd = { ui.btnAddMaxInkC_, ui.btnAddMaxInkM_, ui.btnAddMaxInkY_, ui.btnAddMaxInkK_ }; QList<QSpinBox*> boxMaxInks = { ui.boxMaxInkC_, ui.boxMaxInkM_, ui.boxMaxInkY_, ui.boxMaxInkK_ }; for (int i = 0; i < sliderMaxInks.count(); ++i) { btnMaxInksSub.at(i)->setEnabled(false); sliderMaxInks.at(i)->setEnabled(false); btnMaxInksAdd.at(i)->setEnabled(false); boxMaxInks.at(i)->setEnabled(false); } } else { for (int i = 0; i < sliderMaxInks.count(); ++i) { sliderMaxInks.at(i)->setValue(jsonInfo.fileData_.adjMaxInks_.at(i)); } } } CChannelsAdjustWidget::~CChannelsAdjustWidget() { } void CChannelsAdjustWidget::resetChannelCurve() { auto* curve_widget = (CChannelCurveWidget*)ui.stackedWidget_->currentWidget(); curve_widget->resetCurve(); } bool CChannelsAdjustWidget::acceptCurves() { QList<double> maxInks, conentrations; QList<ChannelCurveINFO> curveValues, backupValues; if (ui.boxGammaC_->isEnabled()) { QList<QSpinBox*> boxGammas = { ui.boxGammaC_, ui.boxGammaM_, ui.boxGammaY_, ui.boxGammaK_ }; for (int i = 0; i < boxGammas.count(); ++i) { conentrations.push_back((boxGammas.at(i)->value() + 10) / 10.0); } } if (ui.boxMaxInkC_->isEnabled()) { QList<QSpinBox*> boxMaxInks = { ui.boxMaxInkC_, ui.boxMaxInkM_, ui.boxMaxInkY_, ui.boxMaxInkK_ }; for (int i = 0; i < boxMaxInks.count(); ++i) { maxInks.push_back(boxMaxInks.at(i)->value() / 100.0); } } for (int i = 0; i < ui.stackedWidget_->count(); ++i) { auto* curve_widget = (CChannelCurveWidget*)ui.stackedWidget_->widget(i); curveValues.push_back(curve_widget->curveValue()); backupValues.push_back(curve_widget->backupValue()); } ColorJSonINFO jsonInfo; loadJSonPath(jsonPath_, jsonInfo); jsonInfo.fileData_.singleGammas_.swap(conentrations); jsonInfo.fileData_.adjMaxInks_.swap(maxInks); jsonInfo.fileData_.channelCurves_.swap(curveValues); jsonInfo.fileData_.backupCurves_.swap(backupValues); return saveJSonPath(jsonPath_, jsonInfo); // QDialog::accept(); } void CChannelsAdjustWidget::paintEvent(QPaintEvent* event) { Q_UNUSED(event); QStyleOption opt; opt.init(this); QPainter painter(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this); } void CChannelsAdjustWidget::initStyleSheets() { this->setProperty(qss_property_dialog_widget_content, true); ui.label->setProperty(qss_property_dialog_label, true); ui.label_2->setProperty(qss_property_dialog_label, true); QList<QSpinBox*> boxs = { ui.boxGammaC_, ui.boxGammaM_, ui.boxGammaY_, ui.boxGammaK_, ui.boxMaxInkC_, ui.boxMaxInkM_, ui.boxMaxInkY_, ui.boxMaxInkK_ }; for (int i = 0; i < boxs.count(); ++i) { boxs.at(i)->setProperty(qss_property_dialog_spin_box, true); } QList<QPushButton*> btnSubs = { ui.btnSubGammaC_, ui.btnSubGammaM_, ui.btnSubGammaY_, ui.btnSubGammaK_, ui.btnSubMaxInkC_, ui.btnSubMaxInkM_, ui.btnSubMaxInkY_, ui.btnSubMaxInkK_ }; for (int i = 0; i < btnSubs.count(); ++i) { btnSubs.at(i)->setProperty("attrib_button_sub", true); btnSubs.at(i)->setAutoRepeat(true); } QList<QPushButton*> btnAdds = { ui.btnAddGammaC_, ui.btnAddGammaM_, ui.btnAddGammaY_, ui.btnAddGammaK_, ui.btnAddMaxInkC_, ui.btnAddMaxInkM_, ui.btnAddMaxInkY_, ui.btnAddMaxInkK_ }; for (int i = 0; i < btnSubs.count(); ++i) { btnAdds.at(i)->setProperty("attrib_button_add", true); btnAdds.at(i)->setAutoRepeat(true); } } void CChannelsAdjustWidget::initConnections() { connect(ui.btnChannel1_, &QPushButton::clicked, this, [&]() { if (ui.stackedWidget_->count() <= 1) return; ui.stackedWidget_->setCurrentIndex(0); }); connect(ui.btnChannel2_, &QPushButton::clicked, this, [&]() { if (ui.stackedWidget_->count() <= 2) return; ui.stackedWidget_->setCurrentIndex(1); }); connect(ui.btnChannel3_, &QPushButton::clicked, this, [&]() { if (ui.stackedWidget_->count() <= 3) return; ui.stackedWidget_->setCurrentIndex(2); }); connect(ui.btnChannel4_, &QPushButton::clicked, this, [&]() { if (ui.stackedWidget_->count() <= 4) return; ui.stackedWidget_->setCurrentIndex(3); }); // Gamma - C do { connect(ui.btnSubGammaC_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxGammaC_->value(); if (value == ui.boxGammaC_->minimum()) return; emit ui.boxGammaC_->valueChanged(value - ui.boxGammaC_->singleStep()); }); connect(ui.btnAddGammaC_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxGammaC_->value(); if (value == ui.boxGammaC_->maximum()) return; emit ui.boxGammaC_->valueChanged(value + ui.boxGammaC_->singleStep()); }); connect(ui.sliderGammaC_, &QSlider::valueChanged, ui.boxGammaC_, &QSpinBox::setValue); connect(ui.boxGammaC_, QOverload<int>::of(&QSpinBox::valueChanged), ui.sliderGammaC_, &QSlider::setValue); connect(ui.sliderGammaC_, &QSlider::valueChanged, this, [&](int value) { ui.btnSubGammaC_->setEnabled(value != ui.sliderGammaC_->minimum()); ui.btnAddGammaC_->setEnabled(value != ui.sliderGammaC_->maximum()); }); } while (false); // Gamma - M do { connect(ui.btnSubGammaM_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxGammaM_->value(); if (value == ui.boxGammaM_->minimum()) return; emit ui.boxGammaM_->valueChanged(value - ui.boxGammaM_->singleStep()); }); connect(ui.btnAddGammaM_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxGammaM_->value(); if (value == ui.boxGammaM_->maximum()) return; emit ui.boxGammaM_->valueChanged(value + ui.boxGammaM_->singleStep()); }); connect(ui.sliderGammaM_, &QSlider::valueChanged, ui.boxGammaM_, &QSpinBox::setValue); connect(ui.boxGammaM_, QOverload<int>::of(&QSpinBox::valueChanged), ui.sliderGammaM_, &QSlider::setValue); connect(ui.sliderGammaM_, &QSlider::valueChanged, this, [&](int value) { ui.btnSubGammaM_->setEnabled(value != ui.sliderGammaM_->minimum()); ui.btnAddGammaM_->setEnabled(value != ui.sliderGammaM_->maximum()); }); } while (false); // Gamma - Y do { connect(ui.btnSubGammaY_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxGammaY_->value(); if (value == ui.boxGammaY_->minimum()) return; emit ui.boxGammaY_->valueChanged(value - ui.boxGammaY_->singleStep()); }); connect(ui.btnAddGammaY_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxGammaY_->value(); if (value == ui.boxGammaY_->maximum()) return; emit ui.boxGammaY_->valueChanged(value + ui.boxGammaY_->singleStep()); }); connect(ui.sliderGammaY_, &QSlider::valueChanged, ui.boxGammaY_, &QSpinBox::setValue); connect(ui.boxGammaY_, QOverload<int>::of(&QSpinBox::valueChanged), ui.sliderGammaY_, &QSlider::setValue); connect(ui.sliderGammaY_, &QSlider::valueChanged, this, [&](int value) { ui.btnSubGammaY_->setEnabled(value != ui.sliderGammaY_->minimum()); ui.btnAddGammaY_->setEnabled(value != ui.sliderGammaY_->maximum()); }); } while (false); // Gamma - K do { connect(ui.btnSubGammaK_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxGammaK_->value(); if (value == ui.boxGammaK_->minimum()) return; emit ui.boxGammaK_->valueChanged(value - ui.boxGammaK_->singleStep()); }); connect(ui.btnAddGammaK_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxGammaK_->value(); if (value == ui.boxGammaK_->maximum()) return; emit ui.boxGammaK_->valueChanged(value + ui.boxGammaK_->singleStep()); }); connect(ui.sliderGammaK_, &QSlider::valueChanged, ui.boxGammaK_, &QSpinBox::setValue); connect(ui.boxGammaK_, QOverload<int>::of(&QSpinBox::valueChanged), ui.sliderGammaK_, &QSlider::setValue); connect(ui.sliderGammaK_, &QSlider::valueChanged, this, [&](int value) { ui.btnSubGammaK_->setEnabled(value != ui.sliderGammaK_->minimum()); ui.btnAddGammaK_->setEnabled(value != ui.sliderGammaK_->maximum()); }); } while (false); // MaxInk - C do { connect(ui.btnSubMaxInkC_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxMaxInkC_->value(); if (value == ui.boxMaxInkC_->minimum()) return; emit ui.boxMaxInkC_->valueChanged(value - ui.boxMaxInkC_->singleStep()); }); connect(ui.btnAddMaxInkC_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxMaxInkC_->value(); if (value == ui.boxMaxInkC_->maximum()) return; emit ui.boxMaxInkC_->valueChanged(value + ui.boxMaxInkC_->singleStep()); }); connect(ui.sliderMaxInkC_, &QSlider::valueChanged, ui.boxMaxInkC_, &QSpinBox::setValue); connect(ui.boxMaxInkC_, QOverload<int>::of(&QSpinBox::valueChanged), ui.sliderMaxInkC_, &QSlider::setValue); connect(ui.sliderMaxInkC_, &QSlider::valueChanged, this, [&](int value) { ui.btnSubMaxInkC_->setEnabled(value != ui.sliderMaxInkC_->minimum()); ui.btnAddMaxInkC_->setEnabled(value != ui.sliderMaxInkC_->maximum()); }); } while (false); // MaxInk - M do { connect(ui.btnSubMaxInkM_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxMaxInkM_->value(); if (value == ui.boxMaxInkM_->minimum()) return; emit ui.boxMaxInkM_->valueChanged(value - ui.boxMaxInkM_->singleStep()); }); connect(ui.btnAddMaxInkM_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxMaxInkM_->value(); if (value == ui.boxMaxInkM_->maximum()) return; emit ui.boxMaxInkM_->valueChanged(value + ui.boxMaxInkM_->singleStep()); }); connect(ui.sliderMaxInkM_, &QSlider::valueChanged, ui.boxMaxInkM_, &QSpinBox::setValue); connect(ui.boxMaxInkM_, QOverload<int>::of(&QSpinBox::valueChanged), ui.sliderMaxInkM_, &QSlider::setValue); connect(ui.sliderMaxInkM_, &QSlider::valueChanged, this, [&](int value) { ui.btnSubMaxInkM_->setEnabled(value != ui.sliderMaxInkM_->minimum()); ui.btnAddMaxInkM_->setEnabled(value != ui.sliderMaxInkM_->maximum()); }); } while (false); // MaxInk - Y do { connect(ui.btnSubMaxInkY_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxMaxInkY_->value(); if (value == ui.boxMaxInkY_->minimum()) return; emit ui.boxMaxInkY_->valueChanged(value - ui.boxMaxInkY_->singleStep()); }); connect(ui.btnAddMaxInkY_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxMaxInkY_->value(); if (value == ui.boxMaxInkY_->maximum()) return; emit ui.boxMaxInkY_->valueChanged(value + ui.boxMaxInkY_->singleStep()); }); connect(ui.sliderMaxInkY_, &QSlider::valueChanged, ui.boxMaxInkY_, &QSpinBox::setValue); connect(ui.boxMaxInkY_, QOverload<int>::of(&QSpinBox::valueChanged), ui.sliderMaxInkY_, &QSlider::setValue); connect(ui.sliderMaxInkY_, &QSlider::valueChanged, this, [&](int value) { ui.btnSubMaxInkY_->setEnabled(value != ui.sliderMaxInkY_->minimum()); ui.btnAddMaxInkY_->setEnabled(value != ui.sliderMaxInkY_->maximum()); }); } while (false); // MaxInk - K do { connect(ui.btnSubMaxInkK_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxMaxInkK_->value(); if (value == ui.boxMaxInkK_->minimum()) return; emit ui.boxMaxInkK_->valueChanged(value - ui.boxMaxInkK_->singleStep()); }); connect(ui.btnAddMaxInkK_, &QPushButton::clicked, this, [&]() { qreal value = ui.boxMaxInkK_->value(); if (value == ui.boxMaxInkK_->maximum()) return; emit ui.boxMaxInkK_->valueChanged(value + ui.boxMaxInkK_->singleStep()); }); connect(ui.sliderMaxInkK_, &QSlider::valueChanged, ui.boxMaxInkK_, &QSpinBox::setValue); connect(ui.boxMaxInkK_, QOverload<int>::of(&QSpinBox::valueChanged), ui.sliderMaxInkK_, &QSlider::setValue); connect(ui.sliderMaxInkK_, &QSlider::valueChanged, this, [&](int value) { ui.btnSubMaxInkK_->setEnabled(value != ui.sliderMaxInkK_->minimum()); ui.btnAddMaxInkK_->setEnabled(value != ui.sliderMaxInkK_->maximum()); }); } while (false); } bool CChannelsAdjustWidget::loadJSonPath(const QString& jsonPath, ColorJSonINFO& jsonInfo) { jsonPath_ = jsonPath; QFile file(jsonPath); if (!file.open(QIODevice::ReadOnly)) return false; QByteArray data(file.readAll()); file.close(); QJsonParseError jError; //创建QJsonParseError对象 QJsonDocument jDoc = QJsonDocument::fromJson(data, &jError); if (jError.error != QJsonParseError::NoError) return false; QJsonObject obj = jDoc.object(); if (obj.contains("FileInfo")) { auto fileInfoObj = obj.value("FileInfo").toObject(); if (fileInfoObj.contains("FileType")) { jsonInfo.fileInfo_.fileType_ = fileInfoObj.value("FileType").toString(); } if (fileInfoObj.contains("Version")) { jsonInfo.fileInfo_.version_ = fileInfoObj.value("Version").toInt(); } } if (obj.contains("InkInfo")) { auto inkObj = obj.value("InkInfo").toObject(); if (inkObj.contains("ChannelNum")) { jsonInfo.inkInfo_.channelNum_ = inkObj.value("ChannelNum").toInt(); } if (inkObj.contains("LightInkNum")) { jsonInfo.inkInfo_.lightInkNum_ = inkObj.value("LightInkNum").toInt(); } if (inkObj.contains("SpotColorNames")) { auto nameObjs = inkObj.value("SpotColorNames").toArray(); for (int i = 0; i < nameObjs.count(); ++i) { jsonInfo.inkInfo_.spotColorNames_.push_back(nameObjs.at(i).toString()); } } } if (obj.contains("FileData")) { auto dataObj = obj.value("FileData").toObject(); if (dataObj.contains("Step")) { jsonInfo.fileData_.step_ = dataObj.value("Step").toInt(); } if (dataObj.contains("MaxInk")) { auto maxInkObj = dataObj.value("MaxInk").toArray(); for (int i = 0; i < maxInkObj.count(); ++i) { jsonInfo.fileData_.maxInks_.push_back(maxInkObj.at(i).toDouble()); } } if (dataObj.contains("Linear")) { auto linearObj = dataObj.value("Linear").toObject(); for (int i = 0; i < 4; ++i) { QString key = QString("4CLR_%1").arg(i + 1); if (linearObj.contains(key)) { ChannelCurveINFO curveInfo; curveInfo.name_ = key; auto valuesObj = linearObj.value(key).toArray(); for (int v = 0; v < valuesObj.count(); ++v) { curveInfo.curve_.push_back(valuesObj.at(v).toDouble()); } jsonInfo.fileData_.channelCurves_.push_back(curveInfo); } } } if (dataObj.contains("TotalInk")) { jsonInfo.fileData_.totalInk_ = dataObj.value("TotalInk").toDouble(); } if (dataObj.contains("BeginLimitInk")) { jsonInfo.fileData_.beginLimitInk_ = dataObj.value("BeginLimitInk").toDouble(); } if (dataObj.contains("LightFussing")) { auto lightObj = dataObj.value("LightFussing").toArray(); for (int i = 0; i < lightObj.count(); ++i) { jsonInfo.fileData_.lightFussings_.push_back(lightObj.at(i).toDouble()); } } if (dataObj.contains("TotalGamma")) { jsonInfo.fileData_.totalGamma_ = dataObj.value("TotalGamma").toDouble(); } if (dataObj.contains("SingleGamma")) { auto gammaObj = dataObj.value("SingleGamma").toArray(); for (int i = 0; i < gammaObj.count(); ++i) { jsonInfo.fileData_.singleGammas_.push_back(gammaObj.at(i).toDouble() * 10 - 10); } } else { for (int i = 0; i < jsonInfo.inkInfo_.channelNum_; ++i) { jsonInfo.fileData_.singleGammas_.push_back(0.0); } } if (dataObj.contains("AdjMaxInk")) { auto maxInkObj = dataObj.value("AdjMaxInk").toArray(); for (int i = 0; i < maxInkObj.count(); ++i) { jsonInfo.fileData_.adjMaxInks_.push_back(maxInkObj[i].toDouble() * 100); } } else { for (int i = 0; i < jsonInfo.inkInfo_.channelNum_; ++i) { jsonInfo.fileData_.adjMaxInks_.push_back(100.0); } } if (dataObj.contains("Backup")) { auto linearObj = dataObj.value("Backup").toObject(); for (int i = 0; i < 4; ++i) { QString key = QString("4CLR_%1").arg(i + 1); if (linearObj.contains(key)) { ChannelCurveINFO curveInfo; curveInfo.name_ = key; auto valuesObj = linearObj.value(key).toArray(); for (int v = 0; v < valuesObj.count(); ++v) { curveInfo.curve_.push_back(valuesObj.at(v).toDouble()); } jsonInfo.fileData_.backupCurves_.push_back(curveInfo); } } } } return true; } #include "document.h" #include "prettywriter.h" #include "stringbuffer.h" using DocumentUTF16 = rapidjson::GenericDocument<rapidjson::UTF16<> >; using ValueUTF16 = rapidjson::GenericValue<rapidjson::UTF16<> >; using StringBufferUTF16 = rapidjson::GenericStringBuffer<rapidjson::UTF16<> >; using WriterUTF16 = rapidjson::Writer<StringBufferUTF16, rapidjson::UTF16<>, rapidjson::UTF16<> >; using PrettyWriterUTF16 = rapidjson::PrettyWriter<StringBufferUTF16, rapidjson::UTF16<>, rapidjson::UTF16<> >; bool SaveJSonFile(const std::wstring& strJSonPath, const wchar_t* strJSonStringUtf8); bool CChannelsAdjustWidget::saveJSonPath(const QString& jsonPath, const ColorJSonINFO& jsonInfo) { using namespace rapidjson; DocumentUTF16 JSonDoc; DocumentUTF16::AllocatorType& allocator = JSonDoc.GetAllocator(); ValueUTF16 fileInfoObj(kObjectType); do { auto& fileInfo = jsonInfo.fileInfo_; fileInfoObj.AddMember(L"FileType", ValueUTF16(fileInfo.fileType_.toStdWString().c_str(), allocator), allocator); fileInfoObj.AddMember(L"Version", fileInfo.version_, allocator); } while (false); ValueUTF16 inkInfoObj(kObjectType); do { auto& inkInfo = jsonInfo.inkInfo_; inkInfoObj.AddMember(L"ChannelNum", inkInfo.channelNum_, allocator); inkInfoObj.AddMember(L"LightInkNum", inkInfo.lightInkNum_, allocator); ValueUTF16 namesObj(kArrayType); for (int i = 0; i < jsonInfo.inkInfo_.spotColorNames_.count(); ++i) { namesObj.PushBack(ValueUTF16(inkInfo.spotColorNames_.at(i).toStdWString().c_str(), allocator), allocator); } inkInfoObj.AddMember(L"SpotColorNames", namesObj, allocator); } while (false); ValueUTF16 fileDataObj(kObjectType); do { auto& fileData = jsonInfo.fileData_; fileDataObj.AddMember(L"Step", fileData.step_, allocator); ValueUTF16 maxInkObjs(kArrayType); for (int i = 0; i < fileData.maxInks_.count(); ++i) { maxInkObjs.PushBack(fileData.maxInks_.at(i), allocator); } fileDataObj.AddMember(L"MaxInk", maxInkObjs, allocator); ValueUTF16 linearObj(kObjectType); for (int i = 0; i < fileData.channelCurves_.count(); ++i) { if (fileData.channelCurves_.at(i).name_.isEmpty()) continue; ValueUTF16 curveObj(kArrayType); for (int c = 0; c < fileData.channelCurves_.at(i).curve_.count(); ++c) { curveObj.PushBack(fileData.channelCurves_.at(i).curve_.at(c), allocator); } linearObj.AddMember(ValueUTF16(fileData.channelCurves_.at(i).name_.toStdWString().c_str(), allocator), curveObj, allocator); } fileDataObj.AddMember(L"Linear", linearObj, allocator); fileDataObj.AddMember(L"TotalInk", fileData.totalInk_, allocator); fileDataObj.AddMember(L"BeginLimitInk", fileData.beginLimitInk_, allocator); ValueUTF16 lightFussingObj(kArrayType); for (int i = 0; i < fileData.lightFussings_.count(); ++i) { lightFussingObj.PushBack(fileData.lightFussings_.at(i), allocator); } fileDataObj.AddMember(L"LightFussing", lightFussingObj, allocator); fileDataObj.AddMember(L"TotalGamma", fileData.totalGamma_, allocator); if (!fileData.singleGammas_.isEmpty()) { ValueUTF16 singleGammaObj(kArrayType); for (int i = 0; i < fileData.singleGammas_.count(); ++i) { singleGammaObj.PushBack(fileData.singleGammas_.at(i), allocator); } fileDataObj.AddMember(L"SingleGamma", singleGammaObj, allocator); } if (!fileData.adjMaxInks_.isEmpty()) { ValueUTF16 adjMaxInkObj(kArrayType); for (int i = 0; i < fileData.adjMaxInks_.count(); ++i) { adjMaxInkObj.PushBack(fileData.adjMaxInks_.at(i), allocator); } fileDataObj.AddMember(L"AdjMaxInk", adjMaxInkObj, allocator); } if (!fileData.backupCurves_.isEmpty()) { ValueUTF16 backupObj(kObjectType); for (int i = 0; i < fileData.backupCurves_.count(); ++i) { if (fileData.backupCurves_.at(i).name_.isEmpty()) continue; ValueUTF16 curveObj(kArrayType); for (int c = 0; c < fileData.backupCurves_.at(i).curve_.count(); ++c) { curveObj.PushBack(fileData.backupCurves_.at(i).curve_.at(c), allocator); } backupObj.AddMember(ValueUTF16(fileData.backupCurves_.at(i).name_.toStdWString().c_str(), allocator), curveObj, allocator); } fileDataObj.AddMember(L"Backup", backupObj, allocator); } } while (false); ValueUTF16 obj(kObjectType); obj.AddMember(L"FileInfo", fileInfoObj, allocator); obj.AddMember(L"InkInfo", inkInfoObj, allocator); obj.AddMember(L"FileData", fileDataObj, allocator); StringBufferUTF16 buf; PrettyWriterUTF16 writer(buf); obj.Accept(writer); SaveJSonFile(jsonPath_.toStdWString(), buf.GetString()); return true; } #include <Windows.h> inline std::string Unicode2Utf8(const wchar_t* unicode) { DWORD BufSize = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, nullptr, 0, nullptr, nullptr); char* pUtf8 = new char[BufSize]; memset(pUtf8, 0, BufSize); WideCharToMultiByte(CP_UTF8, 0, unicode, -1, pUtf8, BufSize, nullptr, nullptr); std::string strUtf8; strUtf8.assign(pUtf8); delete[] pUtf8; return strUtf8; } bool SaveJSonFile(const std::wstring& strJSonPath, const wchar_t* lpszJSonString) { std::string strBuf = Unicode2Utf8(lpszJSonString); FILE* pFile; errno_t code = _wfopen_s(&pFile, strJSonPath.c_str(), L"w+"); if (!code && pFile) { fwrite(strBuf.c_str(), 1, strBuf.size(), pFile); fclose(pFile); return true; } return false; } 当我改变25个spinbox的任意一个的时候,莫名其妙的我的boxGammaM_的spinbox的进行了改编
最新发布
12-03
RapidJSON 中,若需要将两个 `rapidjson::Value` 对象合并到一个新的 `rapidjson::Value` 中,通常的做法是创建一个目标对象,并逐个复制或添加成员。这要求确保分配器(`Allocator`)的正确使用,以避免内存管理问题。 假设每个 `rapidjson::Value` 都是一个 JSON 对象类型(即 `rapidjson::kObjectType`),可以通过以下方式实现合并: ### 合并示例代码 ```cpp #include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" void mergeJsonValues(rapidjson::Value& target, rapidjson::Value& source, rapidjson::Document::AllocatorType& allocator) { for (auto it = source.MemberBegin(); it != source.MemberEnd(); ++it) { // 如果目标中没有相同的 key,则直接添加 if (!target.HasMember(it->name)) { target.AddMember(it->name, it->value, allocator); } else { // 可选:处理重复 key 的情况,例如跳过、覆盖或合并 } } } int main() { rapidjson::Document doc; doc.SetObject(); rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); // 创建第一个 Value 对象 rapidjson::Value value1(rapidjson::kObjectType); value1.AddMember("name", "Alice", allocator); value1.AddMember("age", 25, allocator); // 创建第二个 Value 对象 rapidjson::Value value2(rapidjson::kObjectType); value2.AddMember("city", "Beijing", allocator); value2.AddMember("country", "China", allocator); // 创建目标 Value 对象 rapidjson::Value mergedValue(rapidjson::kObjectType); mergeJsonValues(mergedValue, value1, allocator); mergeJsonValues(mergedValue, value2, allocator); // 换为字符串输出 rapidjson::StringBuffer buffer; rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); mergedValue.Accept(writer); printf("%s\n", buffer.GetString()); return 0; } ``` ### 输出结果 运行上述代码后,控制台将输出合并后的 JSON 字符串: ``` {"name":"Alice","age":25,"city":"Beijing","country":"China"} ``` ### 注意事项 - **分配器一致性**:所有操作必须使用同一个 `Document` 提供的分配器,以保证内存管理的一致性。 - **处理重复键**:如果两个源对象包含相同的键,默认实现会跳过目标中已存在的键,但可以自定义逻辑进行覆盖或合并。 - **对象类型检查**:确保两个 `rapidjson::Value` 是对象类型(`rapidjson::kObjectType`),否则无法遍历其成员 [^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值