Qjson handling an returned array of ojbects - c++

I'm using Qjson to parse a json object that is returned from a web service. I'm stuck on handling an array of complex ojects.
At the first level the web service returns a map consisting of "error", "id", and "return". If there are no errors I can get the first level value by using
nestedMap = m_jsonObject["result"].toMap();
group = new Group();
group->Caption = nestedMap["Caption"].toString();
group->CollectionCount = nestedMap["CollectionCount"].toInt();
I can even get a date item value that is at the second level using
group->ModifiedOn = nestedMap["ModifiedOn"].toMap()["Value"].toDateTime();
I have an object called "Elements" that consists of 29 key-value pairs. The web service is returning an array of these "Elements" and I am unable to find the right way to parse it. In the header file the container for the elements is defined as
QList<GroupElement> Elements;
The line
group->Elements = nestedMap["Elements"].toList();
causes the compiler to throw an error 'error: no match for 'operator=' in '((MyClass*)this)->MyClass::group->Group::Elements = QVariant::toMap() const()'
I would like to learn the correct syntax to put this element into the class.

Update: I wrote another function to convert the QVariantMap object to a
first:
The group-> Elements object was changed to a
class ParentClass{
QList<SharedDataPointer<Address> > Elements;
other class memmbers...
};
Second:
A method to convert the QMap object to an Address object was created
QSharedDataPointer<Address>
API_1_6::mapToAddress(QVariantMap o)
{
QSharedDataPointer<Address> address (new Address());
address-> FirstName = o["FirstName"].toString();
address->LastName = o["LastName"].toString();
address->CompanyName = o["CompanyName"].toString();
address->Street = o["Street"].toString();
address->Street2 = o["Street2"].toString();
address->City = o["City"].toString();
address->Zip = o["Zip"].toString();
address-> State = o["State"].toString();
address->Country = o["Country"].toString();
address->Phone = o["Phone"].toString();
address->Phone2 = o["Phone2"].toString();
address-> Fax = o["Fax"].toString();
address-> Url = o["Url"].toString();
address->Email = o["Email"].toString();
address->Other = o["Other"].toString();
return address;
}
third: In the code, foreach is used to walk through the list and create and store the new objects
// get the list of the elements
elementsList = nestedMap["Elements"].toList();
// Add the element, converted to the new type, to the Elements object of the'parent' class
foreach(QVariant qElement, elementsList){
group-> Elements.append(mapToAddress(qElement))
}

Related

Duplicate value in list getting saved multiple times (Single record)

I am trying to update SObject record depending on the crtieria, id it matches create a SObject variable and adding values and then finally adding this into a List.When a match is found the Sobject variable here'PRrecordupdate' gets created and values get assigend but as I add it in a list,the original value already present in the list is removed and this latest PRrecordupdate get saved multiple times.Please look into the code below and let me know what I am doing wrong.Thanks
//CODE
List<Domain_review__C> newPR = new List<Domain_review__C>();
List<Domain_review__C> PRCollectionupdate = new List<Domain_review__C>();
Domain_review__C PRrecordupdate = new Domain_review__C();
map<id,domain_review__C> PRmap = new map<id,domain_review__C>();
List<Domain_Review__c> PR = [Select id,name,Property_Name_text__c from Domain_Review__c Where Exchange_Quality_ID__c =: a0W56000002PgXfEAK];
For(domain_review__c a: PR)
{
For(Domain_review__c b: newPR)
{
if(a.Property_Name_text__c == b.property_text__c)
{
PRrecordupdate.Id = a.Id;
PRrecordupdate.Approval_Status__c = b.Approval_Status__c;
PRrecordupdate.Rejection_Reason__c = b.Rejection_Reason__c;
PRrecordupdate.seller_name__c = b.seller_name__c;
PRrecordupdate.Inventory_Relationship__c = b.Inventory_Relationship__c;
PRCollectionupdate.add(PRrecordupdate);
//?? here, it's removing the earlier record and updating with new record multiple times, means at the end in PRCollectionupdate list only one record is saved multiple times//
}
}
}
PRmap.putall(PRCollectionupdate);
if(PRmap.size()>0){
update PRmap.values();
}

Read a list of parameters from a LuaRef using LuaBridge

[RESOLVED]
I'm building a game engine that uses LuaBridge in order to read components for entities. In my engine, an entity file looks like this, where "Components" is a list of the components that my entity has and the rest of parameters are used to setup the values for each individual component:
-- myEntity.lua
Components = {"MeshRenderer", "Transform", "Rigidbody"}
MeshRenderer = {
Type = "Sphere",
Position = {0,300,0}
}
Transform = {
Position = {0,150,0},
Scale = {1,1,1},
Rotation = {0,0,0}
}
Rigidbody = {
Type = "Sphere",
Mass = 1
}
I'm currently using this function (in C++) in order to read the value from a parameter (given its name) inside a LuaRef.
template<class T>
T readParameter(LuaRef& table, const std::string& parameterName)
{
try {
return table.rawget(parameterName).cast<T>();
}
catch (std::exception e) {
// std::cout ...
return NULL;
}
}
For example, when calling readVariable<std::string>(myRigidbodyTable, "Type"), with myRigidbodyTable being a LuaRef with the values of Rigidbody, this function should return an std::string with the value "Sphere".
My problem is that when I finish reading and storing the values of my Transform component, when I want to read the values for "Ridigbody" and my engine reads the value "Type", an unhandled exception is thrown at Stack::push(lua_State* L, const std::string& str, std::error_code&).
I am pretty sure that this has to do with the fact that my component Transform stores a list of values for parameters like "Position", because I've had no problems while reading components that only had a single value for each parameter. What's the right way to do this, in case I am doing something wrong?
I'd also like to point out that I am new to LuaBridge, so this might be a beginner problem with a solution that I've been unable to find. Any help is appreciated :)
Found the problem, I wasn't reading the table properly. Instead of
LuaRef myTable = getGlobal(state, tableName.c_str());
I was using the following
LuaRef myTable = getGlobal(state, tableName.c_str()).getMetatable();

Univocity - parse each TSV file row to different Type of class object

I have a tsv file which has fixed rows but each row is mapped to different Java Class.
For example.
recordType recordValue1
recordType recordValue1 recordValue2
for First row I have follofing class:
public class FirstRow implements ItsvRecord {
#Parsed(index = 0)
private String recordType;
#Parsed(index = 1)
private String recordValue1;
public FirstRow() {
}
}
and for second row I have:
public class SecondRow implements ItsvRecord {
#Parsed(index = 0)
private String recordType;
#Parsed(index = 1)
private String recordValue1;
public SecondRow() {
}
}
I want to parse the TSV file directly to the respective objects but I am falling short of ideas.
Use an InputValueSwitch. This will match a value in a particular column of each row to determine what RowProcessor to use. Example:
Create two (or more) processors for each type of record you need to process:
final BeanListProcessor<FirstRow> firstProcessor = new BeanListProcessor<FirstRow>(FirstRow.class);
final BeanListProcessor<SecondRow> secondProcessor = new BeanListProcessor<SecondRow>(SecondRow.class);
Create an InputValueSwitch:
//0 means that the first column of each row has a value that
//identifies what is the type of record you are dealing with
InputValueSwitch valueSwitch = new InputValueSwitch(0);
//assigns the first processor to rows whose first column contain the 'firstRowType' value
valueSwitch.addSwitchForValue("firstRowType", firstProcessor);
//assigns the second processor to rows whose first column contain the 'secondRowType' value
valueSwitch.addSwitchForValue("secondRowType", secondProcessor);
Parse as usual:
TsvParserSettings settings = new TsvParserSettings(); //configure...
// your row processor is the switch
settings.setProcessor(valueSwitch);
TsvParser parser = new TsvParser(settings);
Reader input = new StringReader(""+
"firstRowType\trecordValue1\n" +
"secondRowType\trecordValue1\trecordValue2");
parser.parse(input);
Get the parsed objects from your processors:
List<FirstRow> firstTypeObjects = firstProcessor.getBeans();
List<SecondRow> secondTypeObjects = secondProcessor.getBeans();
The output will be*:
[FirstRow{recordType='firstRowType', recordValue1='recordValue1'}]
[SecondRow{recordType='secondRowType', recordValue1='recordValue1', recordValue2='recordValue2'}]
Assuming you have a sane toString() implemented in your classes
If you want to manage associations among the objects that are parsed:
If your FirstRow should contain the elements parsed for records of type SecondRow, simply override the rowProcessorSwitched method:
InputValueSwitch valueSwitch = new InputValueSwitch(0) {
#Override
public void rowProcessorSwitched(RowProcessor from, RowProcessor to) {
if (from == secondProcessor) {
List<FirstRow> firstRows = firstProcessor.getBeans();
FirstRow mostRecentRow = firstRows.get(firstRows.size() - 1);
mostRecentRow.addRowsOfOtherType(secondProcessor.getBeans());
secondProcessor.getBeans().clear();
}
}
};
The above assumes your FirstRow class has a addRowsOfOtherType method that takes a list of SecondRow as parameter.
And that's it!
You can even mix and match other types of RowProcessor. There's another example here that demonstrates this.
Hope this helps.

How to add a List to HBox in JavaFX?

Am trying to read some values from database and display it in a List.
list = new List();
list.setSize(30, 280);
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/project?"
+ "user=root&password=virus");
statement = connect.createStatement();
preparedStatement = connect
.prepareStatement("select subname from subject");
rs=preparedStatement.executeQuery();
while (rs.next()) {
subject = rs.getString("subname");
list.add(subject);
}
}
The import statement for List is-
import java.awt.List;
So I think List control is not available in JavaFX.
I tried to place the List in HBox -
hb.getChildren().addAll(addSubName, list, b2);
Then I got an error -
method addAll in interface ObservableList<E> cannot be applied to given types;
required: Node[]
found: TextField,List,Button
reason: varargs mismatch; List cannot be converted to Node
where E is a type-variable:
E extends Object declared in interface ObservableList
Is this error correctable ? If not, tell me about a control that can be used as a substitute for List in JavaFX.

access vbscript regular expression invalid procedure call or argument

I have an access table with a memo field where data is entered using a form and before the user enters new data, a date is added in the form of (Apr-01). Upon saving, the new data is added at the top. Then I have a function as follows that goes into the field and gets the newest data so it would be at the top:
Example data
Apr-01 - new data
Mar-09 - old data
etc
Function to get the latest:
Public Function GetLatest(text As String) As String
If (IsNull(text)) Then
GetLatest = ""
Else
Set objRegExpr = New regexp
objRegExpr.Pattern = "([A-Za-z]{3}-[0-9]{2})"
objRegExpr.Global = True
objRegExpr.IgnoreCase = True
Set colMatches = objRegExpr.Execute(text)
del = colMatches(1)
txt = Split(text, del)
GetLatest = txt(0)
End If
End Function
Running this function in the Immediate window, I get the expected results but when I run the same function in a query, I get invalid procedure or argument pointing at this line del = colMatches(1). What am I missing?
To extract what you capture you want the 1st item which is index 0 not 1;
del = colMatches.Item(0)
Or an alternative avoiding the regexp object;
if ucase$(text) like "[A-Z][A-Z][A-Z]-[0-9][0-9]*" then GetLatest = left$(text,6)