locked
problem with Json parsing in C++

    Question

  • Hello,

    i have such logic:

    // cpp

    public MyDelegate (Windows::Data::Json::JsonObject^);

    public void fireDelegate(MyDelegate^ del)

    {

    del(Windows::Data::Json::parse("[ { 'a' : '3' } ]");

    }

    // js

    inside js i'm invoking fireDelegate and trying to pass callback into fireDelegate but always getting error: "function fireDelegate has invalid signature and cannot be called".

    Does anyone have any thoungts/advice about this?

    Wednesday, March 21, 2012 1:58 PM

All replies

  • Hi,

    You need to add delegate key word to define a delegate for WinRT/CX.

    You can check a sample from Walkthrough: Creating a basic Windows Runtime component in C++ and calling it from JavaScript       

    Regards,

    Yi


    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, March 22, 2012 7:47 AM
  • Hi,

    thanks for your replay... i've mistyped when inserted the code, now it looks as the following:

    public delegate void MyDelegate (Windows::Data::Json::JsonObject^);
    
    public void fireDelegate(MyDelegate^ del)
    {
       del(Windows::Data::Json::JsonObject::parse("{ 'a' : '3' }");
    }

    also it compiles and the whole project starts well but i'm getting the following error in runtime:

    "function fireDelegate has invalid signature and cannot be called".

    Could you help me with this issue please?




    • Edited by nicklebedev Thursday, March 22, 2012 10:23 AM
    Thursday, March 22, 2012 10:21 AM
  • could anyone answer please (up)
    Friday, March 23, 2012 1:06 PM
  • Hi Nick

    Based on the error message, the delegate object, del, you passed into the fireDelegate fuction is not match the defination of Mydelegate. You'd better check if it is defined the correctly. The following example create a delegate with a lembda and pass the delegate object as you like.

    public delegate void MyDelegate (Windows::Data::Json::JsonObject^);
           void fireDelegate(MyDelegate^ del)
    		{
    			Windows::Data::Json::JsonObject^ jsonobj =  ref new Windows::Data::Json::JsonObject();
    			
    			auto canParse = Windows::Data::Json::JsonObject::TryParse("{\"a\":\"3\"}", &jsonobj);
    			
    			if (canParse)
    					del(jsonobj);
    		};
    
    
    void Application2::BlankPage::Button_Click_1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    // the function to launch for the demo.
    
    	MyDelegate ^delegate1 = ref new MyDelegate([](Windows::Data::Json::JsonObject ^jason) {
        // handle jason task
    		String ^ str = jason->ToString();
        });
    
    	fireDelegate(delegate1);
    
    
    
    }

    More over, we suggest you use TryParse to avoid the input error about json data.

    Regards,

    Yi

     

    Yi Feng Li [MSFT]
    MSDN Community Support | Feedback to us

    Sunday, March 25, 2012 8:44 AM
  • Hi Yi,

    thanks for your reply...

    your example works, but I have a little different situation: i have a metro app with two projects C++ lib and js application, i would like to create json object from string at the C++ side and then pass it to javascript application, nearly like this:
    at C++ side:  string: "{ 'propertyA' : '3' }", it is converted to json object and passed into javascript app, at js side: access to object's properties like this: var x = jsObj.propertyA;

    could you tell me is there any way to implement it like this? (also, string parsing at javascript side (via jquery or smth else) is not an option here).

    thanks,

    Nick


    Monday, March 26, 2012 2:33 PM
  • could anyone answer please   (up)
    Tuesday, March 27, 2012 1:59 PM