Options

dotnet interop : serialize

MnDMnD Member Posts: 2
edited 2014-04-25 in NAV Three Tier
Hello all ! :)

I'm just making an app that will contact a JSON webservice and return an odject to NAV 2013 R2.

But an issue is occuring when i try to use it in NAV : unable to serialize an instance of the object .NET Framework....

My assembly is in the GAC, and in the Add-in folder, signed.

I don't have this issue while trying to debug in VS2012.

I think the problem comes from my NAV C/AL Code... but can't find a solution :(

I hope someone will be able to help me :)

I'm using a dotnet component which one returns a collection of spécific objects created in the dev :

Objects :

public class Trade
{
public string time { get; set; }
public int type { get; set; }
public string price { get; set; }
public string amount { get; set; }
public string total { get; set; }
}

public class Trades
{
public int count { get; set; }
public List<Trade> trades { get; set; }
//public TradeList trades{get;set;}
public Trade GetTradeItem(int P_Index)
{
return trades[P_Index];
}
}


Method that return the Trade collection object after JSON serialization :

public class Stat
{
public Trades GetMarket()
{
try
{
string url = GetURL();
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
WebResponse ws = request.GetResponse();

Stream s = ws.GetResponseStream();

DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Trades));
Trades retour = (Trades)jsonSerializer.ReadObject(s);

//DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(StatItem));
//StatItem retour = (StatItem)jsonSerializer.ReadObject(s);

return retour;
}
************************ (others methods not described here) *************
}
}

NAV Code :

/////// Vars /////
Stat ==> My C# Stat Class
Item ==> My C# Trade Class
ListItem ==> My C# Trades Class

//// End var ////

Stat := Stat.Stat;
Item := Item.Trade;
ListItem := ListItem.Trades;


ListItem := Stat.GetMarket;
FOR I:=0 TO (ListItem.count-1) DO
BEGIN
Item := ListItem.GetTradeItem(I);
MESSAGE(Item.amount);
END;


But always receive the same error message :

Unable to serialize an instance of this .NET Framework object : Assembly : tradetest ......... , type Tradetest.Trades

Many thanks to my future helpers :)

PS : apologize for my english... it could be better :)

Comments

  • Options
    undy0602undy0602 Member Posts: 67
    Hello MnD,
    How can I put values from C/AL into Json string type assembley?
    My "BridgePos.Class1" assembley has put() function and it has below json string fields.
    put(ustring)
    {"amount": String, "vat": String, "cashAmount": String, "nonCashAmount": String,
    "stocks": [{ "code": String, "name": String,"measureUnit": String, "qty": String,}],
    I want to pass value into this put() function from C/AL (vat entries).

    I tried on below example:
    MyString:='123.45';
    BridgePos:=BridgePos.Class1;
    Type:=BridgePos.GetType();
    JsonSerializerSettings:=JsonSerializerSettings.JsonSerializerSettings();
    Result:=JsonConvert.SerializeObject(MyJsonString,Type,JsonSerializerSettings);

    MESSAGE('BridgePos result %1',BridgePos.put(Result));

    But it appears wrong type data and cant pass value

    Regards,
    Undy
Sign In or Register to comment.