I am devising communication protocols for an online app using Unity.
For each RPC (to the server), to convert payload data to (or from) the JSON format, I created several classes that looked similar to this:
namespace SomeApp
{
public class RpcResponse
{
public int error_code;
public string data;
}
}
However, there are several RPCs, so the number of classes like these keeps increasing.
I am wondering if such classes cost the DEX maximum method limit count, since they have a default constructor? Or maybe this is a bad practice due to other reasons?
Does using structs instead of classes help alleviate the problem?
I can’t say much about the DEX count, however you can avoid those classes in the first place by simply parsing the result as some generic structure.either with Newtonsofts Json.NET library which should be abel to deserialize objects to generic dictionaries, or you could use my SimplyJSON library. It actually parses the JSON into generic Dictionaries and Lists which are wrapped in corresponding classes (JSONObject, JSONArray). The whole framework is strongly typed and implemented for ease usage that almost never requires a manual explicit cast. Everything could be treated like a JSONNode
JSONNode n = JSON.Parse(yourJsonText);
int errorNo = n["error_code"];
string data = n["data"];
Especially when the returned json could represent different results, this allows a much more dynamic parsing to distinguish between an error or success response.
Note: All classes of the SimpleJSON framework are implemented as “partial” classes which makes it easy to add / extend the functionality. There are several extension files in the repository which add implicit type conversion for certain common Unity types (vectors, colors, quaternion, etc). As I said I designed it to make it easy to parse any valid json and to easily construct json as well.