我是javascript开发人员,是c ++的新手。我在js中编写了一个代码,并希望使用rapidjson在c ++中实现。该代码的目的是比较两个json和作为参考的第三个json。
import React, { Component } from 'react';
import { render } from 'react-dom';
import store from './store';
import './style.css';
class App extends Component {
constructor() {
super();
this.state = {
name: 'React'
};
}
testHasAllProperties(dataset) {
let mandatoryProperties = [
"input1",
"input2",
"template",
"isequal",
]
let res = true;
mandatoryProperties.map(v => {
if (!dataset.hasOwnProperty(v)) {
console.log('Missing Property ' + v)
res = false
}
})
return res
}
getInputByPath(obj, path) {
obj = JSON.parse(JSON.stringify(obj))
for (var i = 0, path = path.split('.'), len = path.length; i < len; i++) {
obj = obj[path[i]];
};
return obj;
};
/*
template to iterate
current path to extract value from input
*/
iterateTemplate(template, path, output) {
for (let index in template) {
let outputarr = []
let datatype = Object.prototype.toString.call(template[index])
//this.line(path + index + ' - ' + datatype + ' - ' +
template[index] + ' - ' +datatypeval
outputarr.push(path + index)
outputarr.push(datatype)
let val = this.getInputByPath(store, path + index)
switch (datatype) {
case "[object Undefined]":
console.error(template, index, template[index], typeof template[index])
break
case "[object Array]":
datatype += "(" + val.length + ")"
template[index] = [template[index][0]]
while (template[index].length < val.length) {
template[index].push(template[index][0])
}
case "[object Object]":
template[index] = this.iterateTemplate(template[index], path + index + ".", output)
break
default:
template[index] = val
outputarr.push(template[index])
let datatypeval = Object.prototype.toString.call(val)
outputarr.push(datatypeval)
output.push(this.line(outputarr.join(" - ")))
break
}
}
return template;
}
line(value) {
return <div>{value}</div>
}
showAllDatasets() {
let datasetCount = store.length;
let output = { left: [], right: [] }
for (var i = 0; i < datasetCount; i++) {
let testdataset = JSON.parse(JSON.stringify(store[i]));
let template = JSON.stringify(testdataset.template)
if (this.testHasAllProperties(testdataset) === false)
continue;
output.left.push(this.line(i))
output.right.push(this.line(i))
let template1 = testdataset.template
this.iterateTemplate(template1, i + ".input1.", output.left)
let template2 = JSON.parse(template)
this.iterateTemplate(template2, i + ".input2.", output.right)
let isequal = (JSON.stringify(template1) === JSON.stringify(template2))
output.left.push(this.line("is equal = " + isequal))
output.right.push(this.line(" expected is " + testdataset.isequal))
}
return output;
}
render() {
let output = this.showAllDatasets()
return (
<div>
<div style={{ float: "left", width: "50%" }}>{output.left}</div>
<div style={{ float: "left", width: "50%" }}>{output.right}</div>
</div>
);
}
}
render(<App />, document.getElementById('root'));
我正在努力在c ++中找到实现逻辑,但由于我是c ++的新手,我需要一些帮助来完成它。任何帮助将不胜感激。我不需要Html,css部分只需要在c ++中实现和执行逻辑来实现它。虽然应用逻辑我无法在c ++中找到某些功能。
直到现在我能够检索密钥和模板的路径 这是我的代码:
#include <iostream>
#include "include/rapidjson/document.h"
#include "include/rapidjson/pointer.h"
#include "include/rapidjson/stringbuffer.h"
using namespace std;
using namespace rapidjson;
class json {
public:
static void parse(const string &item1,const string &item2,const
string &temp) {
Document doc;
doc.Parse(temp.c_str());
Document json1;
json1.Parse(item1.c_str());
Document json2;
json2.Parse(item2.c_str());
iterate(json1,json2,doc);
Pointer root;
getPath(doc, root);
}
// to get all the keys in the temp
static void iterate(const Value &json1, const Value &json2, const Value &json) {
for (Value::ConstMemberIterator iterator = json.MemberBegin(); iterator != json.MemberEnd(); iterator++) {
cout << iterator->name.GetString() << endl;
if (iterator->value.GetType() == kObjectType) {
for (Value::ConstMemberIterator itr1 = iterator->value.MemberBegin();
itr1 != iterator->value.MemberEnd(); itr1++) {
if (itr1->value.GetType() == kObjectType) {
iterate(json1,json2,itr1->value);
} else if (itr1->value.GetType() == kArrayType) {
for (auto itr2 = itr1->value.Begin(); itr2 != itr1->value.End(); itr2++) {
if (itr2->GetType() == kArrayType || itr2->GetType() == kObjectType) {
iterate(json1,json2,*(itr2));
}
}
}
}
} else if (iterator->value.GetType() == kArrayType) {
for (Value::ConstValueIterator itr1 = iterator->value.Begin(); itr1 != iterator->value.End(); itr1++) {
// cout << itr->name.GetString() << endl;
if (itr1->GetType() == kArrayType || itr1->GetType() == kObjectType) {
// cout << itr->name.GetString() << endl;
iterate(json1,json2,*(itr1));
}
}
}
}
}
//to get the path of the keys in the temp
static void getPath(const Value& value, const Pointer& pointer) {
if (value.IsObject())
for (Value::ConstMemberIterator itr = value.MemberBegin(); itr != value.MemberEnd(); ++itr) {
getPath(itr->value, pointer.Append(itr->name.GetString(), itr->name.GetStringLength()));
}
else if (value.IsArray()) {
for (SizeType i = 0; i < value.Size(); i++) {
getPath(value[i], pointer.Append(i));
}
}else {
StringBuffer sb;
pointer.Stringify(sb);
std::cout << sb.GetString() << std::endl;
}
}
};
int main() {
const char *temp = "{ \"product\": \"string\", \"version\":\"float\", \"releaseDate\": \"string\", \"demo\": \"bool\", \"person\": { \"id\": \"double\", \"name\": \"string\", \"phones\": { \"home\": \"string\", \"mobile\": \"string\" }, \"email\": [ null ], \"dateOfBirth\": \"string\", \"registered\": \"bool\", \"emergencyContacts\": [ { \"name\": \"string\", \"phone\": \"string\", \"relationship\": \"string\", \"alternativeContacts\": [\"name\":\"string\", null ] } ] } } }";
const char *item1 = "{ \"product\": \"Live JSON generator\", \"version\": 3.1, \"releaseDate\": \"2014-06-25T00:00:00.000Z\", \"demo\": true, \"person\": { \"id\": 12345, \"name\": \"John Doe\", \"phones\": { \"home\": \"800-123-4567\", \"mobile\": \"877-123-1234\" }, \"email\": [ \"jd@example.com\", \"jd@example.org\", \"name\": \"John Doe\", \"demo\": true ], \"dateOfBirth\": \"1980-01-02T00:00:00.000Z\", \"registered\": true, \"emergencyContacts\": [ { \"name\": \"Jane Doe\", \"phone\": \"888-555-1212\", \"relationship\": \"spouse\", \"alternativeContacts\": [ \"test\", \"123\", \"some\\\"char\" ] }, { \"name\": \"Justin Doe\", \"phone\": \"877-123-1212\", \"relationship\": \"parent\", \"alternativeContacts\": [\"name\": \"John Doe\", \"demo\": true, 3, null ] } ] } }";
const char *item2 = "{ \"product\": \"Live JSON generator\", \"version\": 3.1, \"releaseDate\": \"2014-06-25T00:00:00.000Z\", \"demo\": true, \"person\": { \"id\": 12345, \"name\": \"John Doe\", \"phones\": { \"home\": \"800-123-4567\", \"mobile\": \"877-123-1234\" }, \"email\": [ \"demo\": true, \"name\": \"John Doe\", \"jd@example.com\", \"jd@example.org\" ], \"dateOfBirth\": \"1980-01-02T00:00:00.000Z\", \"registered\": true, \"emergencyContacts\": [ { \"name\": \"Jane Doe\", \"phone\": \"888-555-1212\", \"relationship\": \"spouse\", \"alternativeContacts\": [ \"test\", \"123\", \"some\\\"char\" ] }, { \"name\": \"Justin Doe\", \"phone\": \"8774-123-1212\", \"relationship\": \"parent\", \"alternativeContacts\": [\"name\": \"John Doe\", \"demo\": true, 4, null ] } ] } }";
json::parse(item1,item2,temp);
}
将不胜感激。