Newtonsoft.Json TypeNameHandling problem. Linqpad cannot find a custom class assembly
I have this code
void Main()
{
var points = new Point[5];
var rnd = new Random();
for (int i = 0; i < points.Length; i++)
{
points[i] = rnd.Next(3) % 2 == 0 ? new Point() : new Point3D();
points[i].GetType().Dump();
}
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
};
string json = JsonConvert.SerializeObject(points, Newtonsoft.Json.Formatting.Indented,settings);
points = JsonConvert.DeserializeObject<Point[]>(json, settings);
}
public class Point3D : Point
{
public int Z { get; private set;}
public Point3D() : base()
{
var rnd = new Random();
Z = rnd.Next(10);
}
[JsonConstructor]
public Point3D(int X, int Y, int Z) : base(X, Y)
{
this.Z = Z;
}
}
public class Point
{
public int X { get; private set; }
public int Y { get; private set; }
public Point() { }
[JsonConstructor]
public Point(int X, int Y)
{
this.X = X;
this.Y = Y;
}
}
everything is working fine, I can serialize points object, but when I try to deserialize (the last line of code) I get this error message
JsonSerializationException: Error resolving type specified in JSON 'UserQuery+Point3D, LINQPadQuery'. Path '[0].$type', line 3, position 46.
Could not load file or assembly 'LINQPadQuery, Culture=neutral, PublicKeyToken=null'. Operation is not supported. (0x80131515)
this happens only if I have Point3D objects in my array. If array consists from only Point objects, everything is fine. Before the last version of Linqpad, the code like this worked properly. I think that Linqpad cannot find a custom class for the reflection.
Comments
-
I forgot to mention in my post that this code was working properly in Visual Studio.
When I added this code
"var assembly = Assembly.GetAssembly(typeof(Point3D));
assembly.Dump();"
I got this output "LINQPadQuery, Version=1.0.0.52, Culture=neutral, PublicKeyToken=null". But why Linqpad couldn't find it? -
Press F4, click the Advanced tab, and check "Use non-collectible load context for this query".
-
@JoeAlbahari said:
Press F4, click the Advanced tab, and check "Use non-collectible load context for this query".Thank you very much for your prompt response. I really appreciate it. It gave an another error message at first even after check, but I added **#LINQPad nonest ** directive as you advised in another post, and it started to work properly. But after a while, the code was broken again. Now I am getting message "JsonSerializationException: Type specified in JSON 'Point3D, LINQPadQuery, Version=1.0.0.2222, Culture=neutral, PublicKeyToken=null' is not compatible with 'Point, LINQPadQuery, Version=1.0.0.2241, Culture=neutral, PublicKeyToken=null'. Path '[1].$type' "
After I closed linqpad and open again it started to work properly. IMHO #LINQPad nonest directive have to be tested more. There are problems if delete and restore this directive again.I work a lot with json. Please help. Thank you in advance.
-
If you're serializing/deserializing data cached between executions, you'll get this error. Use Util.NewProcess = true; to ensure that nothing is cached.
-
Thank you very much! It works now.
