After days searching I have finally decided to come from help because every solution I've found is not working.
Basically, I'm creating an TCP/IP server that receives a Json string with the name of the instruction to perform and the parameters of the function, for example: ["Nameofinstruction",{"dx":"4000"},{"dy":"160"},{"dz":"120"},{"mat":"test_material"}], but the name of the function and the parameters will be different each time so I need to add the different fields of the string to a dictionary. So far I have tried different things, the one that worked best being this, but I still get some errors, which I don't get if I use a simple string.
The class that deserializes the data is this one:
public class InstructionProc
{
[JsonConverter(typeof(InstructionConverter))]
public class ArrivingMessage
{
public ArrivingMessage()
{
arguments = new Dictionary<string, dynamic>();
}
public Dictionary<string, dynamic> arguments { get; set; }
}
public class InstructionConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(ArrivingMessage);
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var instruction = value as ArrivingMessage;
foreach (var item in instruction.arguments)
{
writer.WritePropertyName(item.Key);
writer.WriteValue(item.Value);
}
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var instruction = existingValue as ArrivingMessage ?? new ArrivingMessage();
while (reader.Read())
{
if (reader.TokenType == JsonToken.EndObject) continue;
var path = reader.Path;
var val = reader.ReadAsString();
instruction.arguments.Add(path, val);
}
return instruction;
}
}
}
So far what is always giving me problems is whatever operation involving the JsonReader reader. Also, this class is executed on a thread spawned whenever a new client connects to the server. Last, the error I get is always
Exception thrown: 'System.Threading.ThreadAbortException' in Newtonsoft.Json.dll
I am aware that this has something to do with my program being multithread, but as I said if I send a simple string and print it on the same thread as this class is executing, I don't have any problems, so I should lock something that I don't know of here.