R25 Json Parser Usage
-
Hello! I'm currently trying to transfer S24 plugin code into R25. As i see in changelog, parser_json.h is replaced with parser.h. But the usage is not clear. In the code, i was reading the string with
jsonParser.Read(result, maxon::JSONPARSERFLAGS::NONE, dd) iferr_return;
and jsonParser is
maxon::JsonParserRef jsonParser;
created like this
auto jsonCRes = maxon::JsonParserRef::Create(); if (jsonCRes == maxon::FAILED) MessageDialog("JSON parser create fail"_s); jsonParser = jsonCRes.GetValue();
I couldn't find anything similar to such creation in the new parser. Could you breafly explain how to create a valid ParserRef and call ReadString to convert string into dictionary?
Thanks! -
Hi,
the parser interface has been changed, you have more parser, and the process is the same for each parser. Create a reference and use it.
Parsers are declared in ParserClasses we have now json, jwt and csv parser.
//---------------------------------------------------------------------------------------- /// Parse a given JSON string and return the JSON data in a maxon::DataDictionary. /// @param[in] in_string The string containing a JSON representation. /// @return Error code if fail else the DataDictionary. static maxon::Result<maxon::DataDictionary> ImportJSONFromString(const maxon::String &in_string) { iferr_scope; // check passed argument if (in_string.IsEmpty()) return maxon::IllegalArgumentError(MAXON_SOURCE_LOCATION, "Passed string is empty."_s); // create a JSONParser reference const maxon::ParserRef jsonParser = maxon::ParserClasses::JsonParser().Create() iferr_return; // allocate a SingleValueReceiver to store the data read from the JSONParser; maxon::SingleValueReceiver<const maxon::DataDictionary&> readData; // parse the string and check it's containing a valid JSON structure iferr (jsonParser.ReadString(in_string, maxon::PARSERFLAGS::NONE, maxon::GetUtf8DefaultDecoder(), readData)) { // provided string is not containing a valid JSON structure DiagnosticOutput("BLW >> Invalid JSON string [@]"); iferr_throw(err); } // return the data return readData.Get().GetValue(); }
Cheers,
Manuel -
@m_magalhaes Thank you!